Whamcloud - gitweb
merging all noncontroversial pieces of b_symlink into 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 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 #define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_LDLM
26
27 #include <linux/module.h>
28 #include <linux/slab.h>
29 #include <linux/lustre_dlm.h>
30 #include <linux/init.h>
31 #include <linux/obd_class.h>
32
33 extern kmem_cache_t *ldlm_resource_slab;
34 extern kmem_cache_t *ldlm_lock_slab;
35 extern struct list_head ldlm_namespace_list;
36 extern int (*mds_reint_p)(int offset, struct ptlrpc_request *req);
37 extern int (*mds_getattr_name_p)(int offset, struct ptlrpc_request *req);
38
39 inline unsigned long round_timeout(unsigned long timeout)
40 {
41         return ((timeout / HZ) + 1) * HZ;
42 }
43
44 static struct list_head waiting_locks_list;
45 static spinlock_t waiting_locks_spinlock;
46 static struct timer_list waiting_locks_timer;
47 static int ldlm_already_setup = 0;
48
49 static void waiting_locks_callback(unsigned long unused)
50 {
51         struct list_head *liter, *n;
52
53         spin_lock_bh(&waiting_locks_spinlock);
54         list_for_each_safe(liter, n, &waiting_locks_list) {
55                 struct ldlm_lock *l = list_entry(liter, struct ldlm_lock,
56                                                  l_pending_chain);
57                 if (l->l_callback_timeout > jiffies)
58                         break;
59                 LDLM_DEBUG(l, "timer expired, recovering exp %p on conn %p",
60                            l->l_export, l->l_export->exp_connection);
61                 recovd_conn_fail(l->l_export->exp_connection);
62         }
63         spin_unlock_bh(&waiting_locks_spinlock);
64 }
65
66 /*
67  * Indicate that we're waiting for a client to call us back cancelling a given
68  * lock.  We add it to the pending-callback chain, and schedule the lock-timeout
69  * timer to fire appropriately.  (We round up to the next second, to avoid
70  * floods of timer firings during periods of high lock contention and traffic).
71  */
72 static int ldlm_add_waiting_lock(struct ldlm_lock *lock)
73 {
74         unsigned long timeout_rounded;
75         ENTRY;
76
77         LASSERT(list_empty(&lock->l_pending_chain));
78
79         spin_lock_bh(&waiting_locks_spinlock);
80         lock->l_callback_timeout = jiffies + (obd_timeout * HZ);
81
82         timeout_rounded = round_timeout(lock->l_callback_timeout);
83
84         if (timeout_rounded < waiting_locks_timer.expires ||
85             !timer_pending(&waiting_locks_timer)) {
86                 mod_timer(&waiting_locks_timer, timeout_rounded);
87         }
88         list_add_tail(&lock->l_pending_chain, &waiting_locks_list); /* FIFO */
89         spin_unlock_bh(&waiting_locks_spinlock);
90         RETURN(1);
91 }
92
93 /*
94  * Remove a lock from the pending list, likely because it had its cancellation
95  * callback arrive without incident.  This adjusts the lock-timeout timer if
96  * needed.  Returns 0 if the lock wasn't pending after all, 1 if it was.
97  */
98 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
99 {
100         struct list_head *list_next;
101
102         ENTRY;
103
104         spin_lock_bh(&waiting_locks_spinlock);
105
106         if (list_empty(&lock->l_pending_chain)) {
107                 spin_unlock_bh(&waiting_locks_spinlock);
108                 RETURN(0);
109         }
110
111         list_next = lock->l_pending_chain.next;
112         if (lock->l_pending_chain.prev == &waiting_locks_list) {
113                 /* Removing the head of the list, adjust timer. */
114                 if (list_next == &waiting_locks_list) {
115                         /* No more, just cancel. */
116                         del_timer(&waiting_locks_timer);
117                 } else {
118                         struct ldlm_lock *next;
119                         next = list_entry(list_next, struct ldlm_lock,
120                                           l_pending_chain);
121                         mod_timer(&waiting_locks_timer,
122                                   round_timeout(next->l_callback_timeout));
123                 }
124         }
125         list_del_init(&lock->l_pending_chain);
126         spin_unlock_bh(&waiting_locks_spinlock);
127         RETURN(1);
128 }
129
130 static int ldlm_server_blocking_ast(struct ldlm_lock *lock,
131                                     struct ldlm_lock_desc *desc,
132                                     void *data, __u32 data_len, int flag)
133 {
134         struct ldlm_request *body;
135         struct ptlrpc_request *req;
136         int rc = 0, size = sizeof(*body);
137         ENTRY;
138
139         if (flag == LDLM_CB_CANCELING) {
140                 /* Don't need to do anything here. */
141                 RETURN(0);
142         }
143
144         req = ptlrpc_prep_req(&lock->l_export->exp_ldlm_data.led_import,
145                               LDLM_BL_CALLBACK, 1, &size, NULL);
146         if (!req)
147                 RETURN(-ENOMEM);
148
149         body = lustre_msg_buf(req->rq_reqmsg, 0);
150         memcpy(&body->lock_handle1, &lock->l_remote_handle,
151                sizeof(body->lock_handle1));
152         memcpy(&body->lock_desc, desc, sizeof(*desc));
153
154         LDLM_DEBUG(lock, "server preparing blocking AST");
155         req->rq_replen = 0; /* no reply needed */
156
157         ldlm_add_waiting_lock(lock);
158         (void)ptl_send_rpc(req);
159
160         /* no commit, and no waiting for reply, so 2x decref now */
161         ptlrpc_req_finished(req);
162         ptlrpc_req_finished(req);
163
164         RETURN(rc);
165 }
166
167 static int ldlm_server_completion_ast(struct ldlm_lock *lock, int flags)
168 {
169         struct ldlm_request *body;
170         struct ptlrpc_request *req;
171         int rc = 0, size = sizeof(*body);
172         ENTRY;
173
174         if (lock == NULL) {
175                 LBUG();
176                 RETURN(-EINVAL);
177         }
178
179         req = ptlrpc_prep_req(&lock->l_export->exp_ldlm_data.led_import,
180                               LDLM_CP_CALLBACK, 1, &size, NULL);
181         if (!req)
182                 RETURN(-ENOMEM);
183
184         body = lustre_msg_buf(req->rq_reqmsg, 0);
185         memcpy(&body->lock_handle1, &lock->l_remote_handle,
186                sizeof(body->lock_handle1));
187         body->lock_flags = flags;
188         ldlm_lock2desc(lock, &body->lock_desc);
189
190         LDLM_DEBUG(lock, "server preparing completion AST");
191         req->rq_replen = 0; /* no reply needed */
192
193         (void)ptl_send_rpc(req);
194         /* no commit, and no waiting for reply, so 2x decref now */
195         ptlrpc_req_finished(req);
196         ptlrpc_req_finished(req);
197
198         RETURN(rc);
199 }
200
201 int ldlm_handle_enqueue(struct ptlrpc_request *req)
202 {
203         struct obd_device *obddev = req->rq_export->exp_obd;
204         struct ldlm_reply *dlm_rep;
205         struct ldlm_request *dlm_req;
206         int rc, size = sizeof(*dlm_rep), cookielen = 0;
207         __u32 flags;
208         ldlm_error_t err;
209         struct ldlm_lock *lock = NULL;
210         void *cookie = NULL;
211         ENTRY;
212
213         LDLM_DEBUG_NOLOCK("server-side enqueue handler START");
214
215         dlm_req = lustre_msg_buf(req->rq_reqmsg, 0);
216         flags = dlm_req->lock_flags;
217         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_PLAIN &&
218             (flags & LDLM_FL_HAS_INTENT)) {
219                 /* In this case, the reply buffer is allocated deep in
220                  * local_lock_enqueue by the policy function. */
221                 cookie = req;
222                 cookielen = sizeof(*req);
223         } else {
224                 rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen,
225                                      &req->rq_repmsg);
226                 if (rc) {
227                         CERROR("out of memory\n");
228                         RETURN(-ENOMEM);
229                 }
230                 if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT) {
231                         cookie = &dlm_req->lock_desc.l_extent;
232                         cookielen = sizeof(struct ldlm_extent);
233                 }
234         }
235
236         /* XXX notice that this lock has no callback data: of course the
237            export would be exactly what we may want to use here... */
238         lock = ldlm_lock_create(obddev->obd_namespace,
239                                 &dlm_req->lock_handle2,
240                                 dlm_req->lock_desc.l_resource.lr_name,
241                                 dlm_req->lock_desc.l_resource.lr_type,
242                                 dlm_req->lock_desc.l_req_mode, NULL, 0);
243         if (!lock)
244                 GOTO(out, err = -ENOMEM);
245
246         memcpy(&lock->l_remote_handle, &dlm_req->lock_handle1,
247                sizeof(lock->l_remote_handle));
248         LDLM_DEBUG(lock, "server-side enqueue handler, new lock created");
249
250         err = ldlm_lock_enqueue(lock, cookie, cookielen, &flags,
251                                 ldlm_server_completion_ast,
252                                 ldlm_server_blocking_ast);
253         if (err != ELDLM_OK)
254                 GOTO(out, err);
255
256         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
257         dlm_rep->lock_flags = flags;
258
259         ldlm_lock2handle(lock, &dlm_rep->lock_handle);
260         if (dlm_req->lock_desc.l_resource.lr_type == LDLM_EXTENT)
261                 memcpy(&dlm_rep->lock_extent, &lock->l_extent,
262                        sizeof(lock->l_extent));
263         if (dlm_rep->lock_flags & LDLM_FL_LOCK_CHANGED) {
264                 memcpy(dlm_rep->lock_resource_name, lock->l_resource->lr_name,
265                        sizeof(dlm_rep->lock_resource_name));
266                 dlm_rep->lock_mode = lock->l_req_mode;
267         }
268
269         lock->l_export = req->rq_export;
270         if (lock->l_export) {
271                 l_lock(&lock->l_resource->lr_namespace->ns_lock);
272                 list_add(&lock->l_export_chain,
273                          &lock->l_export->exp_ldlm_data.led_held_locks);
274                 l_unlock(&lock->l_resource->lr_namespace->ns_lock);
275         }
276
277         EXIT;
278  out:
279         if (lock)
280                 LDLM_DEBUG(lock, "server-side enqueue handler, sending reply"
281                            "(err=%d)", err);
282         req->rq_status = err;
283
284         if (ptlrpc_reply(req->rq_svc, req))
285                 LBUG();
286
287         if (lock) {
288                 if (!err)
289                         ldlm_reprocess_all(lock->l_resource);
290                 LDLM_LOCK_PUT(lock);
291         }
292         LDLM_DEBUG_NOLOCK("server-side enqueue handler END (lock %p)", lock);
293
294         return 0;
295 }
296
297 int ldlm_handle_convert(struct ptlrpc_request *req)
298 {
299         struct ldlm_request *dlm_req;
300         struct ldlm_reply *dlm_rep;
301         struct ldlm_lock *lock;
302         int rc, size = sizeof(*dlm_rep);
303         ENTRY;
304
305         rc = lustre_pack_msg(1, &size, NULL, &req->rq_replen, &req->rq_repmsg);
306         if (rc) {
307                 CERROR("out of memory\n");
308                 RETURN(-ENOMEM);
309         }
310         dlm_req = lustre_msg_buf(req->rq_reqmsg, 0);
311         dlm_rep = lustre_msg_buf(req->rq_repmsg, 0);
312         dlm_rep->lock_flags = dlm_req->lock_flags;
313
314         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
315         if (!lock) {
316                 req->rq_status = EINVAL;
317         } else {
318                 LDLM_DEBUG(lock, "server-side convert handler START");
319                 ldlm_lock_convert(lock, dlm_req->lock_desc.l_req_mode,
320                                   &dlm_rep->lock_flags);
321                 if (ldlm_del_waiting_lock(lock))
322                         CDEBUG(D_DLMTRACE, "converted waiting lock %p\n", lock);
323                 req->rq_status = 0;
324         }
325         if (ptlrpc_reply(req->rq_svc, req) != 0)
326                 LBUG();
327
328         if (lock) {
329                 ldlm_reprocess_all(lock->l_resource);
330                 LDLM_DEBUG(lock, "server-side convert handler END");
331                 LDLM_LOCK_PUT(lock);
332         } else
333                 LDLM_DEBUG_NOLOCK("server-side convert handler END");
334
335         RETURN(0);
336 }
337
338 int ldlm_handle_cancel(struct ptlrpc_request *req)
339 {
340         struct ldlm_request *dlm_req;
341         struct ldlm_lock *lock;
342         int rc;
343         ENTRY;
344
345         rc = lustre_pack_msg(0, NULL, NULL, &req->rq_replen, &req->rq_repmsg);
346         if (rc) {
347                 CERROR("out of memory\n");
348                 RETURN(-ENOMEM);
349         }
350         dlm_req = lustre_msg_buf(req->rq_reqmsg, 0);
351         if (!dlm_req) {
352                 CERROR("bad request buffer for cancel\n");
353                 RETURN(-EINVAL);
354         }
355
356         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
357         if (!lock) {
358                 CERROR("bad lock handle\n");
359                 LDLM_DEBUG_NOLOCK("server-side cancel handler stale lock (lock "
360                                   "%p)", (void *)(unsigned long)
361                                   dlm_req->lock_handle1.addr);
362                 req->rq_status = ESTALE;
363         } else {
364                 LDLM_DEBUG(lock, "server-side cancel handler START");
365                 ldlm_lock_cancel(lock);
366                 if (ldlm_del_waiting_lock(lock))
367                         CDEBUG(D_DLMTRACE, "cancelled waiting lock %p\n", lock);
368                 req->rq_status = 0;
369         }
370
371         if (ptlrpc_reply(req->rq_svc, req) != 0)
372                 LBUG();
373
374         if (lock) {
375                 ldlm_reprocess_all(lock->l_resource);
376                 LDLM_DEBUG(lock, "server-side cancel handler END");
377                 LDLM_LOCK_PUT(lock);
378         }
379
380         RETURN(0);
381 }
382
383 static int ldlm_handle_bl_callback(struct ptlrpc_request *req)
384 {
385         struct ldlm_request *dlm_req;
386         struct ldlm_lock *lock;
387         int do_ast;
388         ENTRY;
389
390         OBD_FAIL_RETURN(OBD_FAIL_OSC_LOCK_BL_AST, 0);
391
392         dlm_req = lustre_msg_buf(req->rq_reqmsg, 0);
393
394         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
395         if (!lock) {
396                 CERROR("blocking callback on lock "LPX64" - lock disappeared\n",
397                        dlm_req->lock_handle1.addr);
398                 RETURN(0);
399         }
400
401         LDLM_DEBUG(lock, "client blocking AST callback handler START");
402
403         l_lock(&lock->l_resource->lr_namespace->ns_lock);
404         lock->l_flags |= LDLM_FL_CBPENDING;
405         do_ast = (!lock->l_readers && !lock->l_writers);
406         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
407
408         if (do_ast) {
409                 LDLM_DEBUG(lock, "already unused, calling "
410                            "callback (%p)", lock->l_blocking_ast);
411                 if (lock->l_blocking_ast != NULL) {
412                         lock->l_blocking_ast(lock, &dlm_req->lock_desc,
413                                              lock->l_data, lock->l_data_len,
414                                              LDLM_CB_BLOCKING);
415                 }
416         } else
417                 LDLM_DEBUG(lock, "Lock still has references, will be"
418                            " cancelled later");
419
420         LDLM_DEBUG(lock, "client blocking callback handler END");
421         LDLM_LOCK_PUT(lock);
422         RETURN(0);
423 }
424
425 static int ldlm_handle_cp_callback(struct ptlrpc_request *req)
426 {
427         struct list_head ast_list = LIST_HEAD_INIT(ast_list);
428         struct ldlm_request *dlm_req;
429         struct ldlm_lock *lock;
430         ENTRY;
431
432         OBD_FAIL_RETURN(OBD_FAIL_OSC_LOCK_CP_AST, 0);
433
434         dlm_req = lustre_msg_buf(req->rq_reqmsg, 0);
435
436         lock = ldlm_handle2lock(&dlm_req->lock_handle1);
437         if (!lock) {
438                 CERROR("completion callback on lock "LPX64" - lock "
439                        "disappeared\n", dlm_req->lock_handle1.addr);
440                 RETURN(0);
441         }
442
443         LDLM_DEBUG(lock, "client completion callback handler START");
444
445         l_lock(&lock->l_resource->lr_namespace->ns_lock);
446
447         /* If we receive the completion AST before the actual enqueue returned,
448          * then we might need to switch lock modes, resources, or extents. */
449         if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
450                 lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
451                 LDLM_DEBUG(lock, "completion AST, new lock mode");
452         }
453         if (lock->l_resource->lr_type == LDLM_EXTENT)
454                 memcpy(&lock->l_extent, &dlm_req->lock_desc.l_extent,
455                        sizeof(lock->l_extent));
456         ldlm_resource_unlink_lock(lock);
457         if (memcmp(dlm_req->lock_desc.l_resource.lr_name,
458                    lock->l_resource->lr_name,
459                    sizeof(__u64) * RES_NAME_SIZE) != 0) {
460                 ldlm_lock_change_resource(lock,
461                                          dlm_req->lock_desc.l_resource.lr_name);
462                 LDLM_DEBUG(lock, "completion AST, new resource");
463         }
464         lock->l_resource->lr_tmp = &ast_list;
465         ldlm_grant_lock(lock);
466         lock->l_resource->lr_tmp = NULL;
467         l_unlock(&lock->l_resource->lr_namespace->ns_lock);
468         LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
469         LDLM_LOCK_PUT(lock);
470
471         ldlm_run_ast_work(&ast_list);
472
473         LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
474                           lock);
475         RETURN(0);
476 }
477
478 static int ldlm_callback_handler(struct ptlrpc_request *req)
479 {
480         int rc;
481         ENTRY;
482
483         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
484         if (rc) {
485                 CERROR("lustre_ldlm: Invalid request: %d\n", rc);
486                 RETURN(rc);
487         }
488
489         if (req->rq_export == NULL) {
490                 CERROR("lustre_dlm: operation %d with missing/invalid export\n",
491                        req->rq_reqmsg->opc);
492                 RETURN(-ENOTCONN);
493         }
494
495         switch (req->rq_reqmsg->opc) {
496         case LDLM_BL_CALLBACK:
497                 CDEBUG(D_INODE, "blocking ast\n");
498                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_BL_CALLBACK, 0);
499                 rc = ldlm_handle_bl_callback(req);
500                 RETURN(rc);
501         case LDLM_CP_CALLBACK:
502                 CDEBUG(D_INODE, "completion ast\n");
503                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CP_CALLBACK, 0);
504                 rc = ldlm_handle_cp_callback(req);
505                 RETURN(rc);
506
507         default:
508                 CERROR("invalid opcode %d\n", req->rq_reqmsg->opc);
509                 RETURN(-EINVAL);
510         }
511
512         RETURN(0);
513 }
514
515
516 static int ldlm_cancel_handler(struct ptlrpc_request *req)
517 {
518         int rc;
519         ENTRY;
520
521         rc = lustre_unpack_msg(req->rq_reqmsg, req->rq_reqlen);
522         if (rc) {
523                 CERROR("lustre_ldlm: Invalid request: %d\n", rc);
524                 RETURN(rc);
525         }
526
527         if (req->rq_export == NULL) {
528                 CERROR("lustre_dlm: operation %d with missing/invalid export\n",
529                        req->rq_reqmsg->opc);
530                 RETURN(-ENOTCONN);
531         }
532
533         switch (req->rq_reqmsg->opc) {
534
535         /* XXX FIXME move this back to mds/handler.c, bug 625069 */
536         case LDLM_CANCEL:
537                 CDEBUG(D_INODE, "cancel\n");
538                 OBD_FAIL_RETURN(OBD_FAIL_LDLM_CANCEL, 0);
539                 rc = ldlm_handle_cancel(req);
540                 if (rc)
541                         break;
542                 RETURN(0);
543
544         default:
545                 CERROR("invalid opcode %d\n", req->rq_reqmsg->opc);
546                 RETURN(-EINVAL);
547         }
548
549         RETURN(0);
550 }
551
552
553 static int ldlm_iocontrol(long cmd, struct lustre_handle *conn, int len,
554                           void *karg, void *uarg)
555 {
556         struct obd_device *obddev = class_conn2obd(conn);
557         struct ptlrpc_connection *connection;
558         int err = 0;
559         ENTRY;
560
561         if (_IOC_TYPE(cmd) != IOC_LDLM_TYPE || _IOC_NR(cmd) < IOC_LDLM_MIN_NR ||
562             _IOC_NR(cmd) > IOC_LDLM_MAX_NR) {
563                 CDEBUG(D_IOCTL, "invalid ioctl (type %ld, nr %ld, size %ld)\n",
564                        _IOC_TYPE(cmd), _IOC_NR(cmd), _IOC_SIZE(cmd));
565                 RETURN(-EINVAL);
566         }
567
568         OBD_ALLOC(obddev->u.ldlm.ldlm_client,
569                   sizeof(*obddev->u.ldlm.ldlm_client));
570         connection = ptlrpc_uuid_to_connection("ldlm");
571         if (!connection)
572                 CERROR("No LDLM UUID found: assuming ldlm is local.\n");
573
574         switch (cmd) {
575         case IOC_LDLM_TEST:
576                 err = ldlm_test(obddev, conn);
577                 CERROR("-- done err %d\n", err);
578                 GOTO(out, err);
579         case IOC_LDLM_DUMP:
580                 ldlm_dump_all_namespaces();
581                 GOTO(out, err);
582         default:
583                 GOTO(out, err = -EINVAL);
584         }
585
586  out:
587         if (connection)
588                 ptlrpc_put_connection(connection);
589         OBD_FREE(obddev->u.ldlm.ldlm_client,
590                  sizeof(*obddev->u.ldlm.ldlm_client));
591         return err;
592 }
593
594 #define LDLM_NUM_THREADS        4
595
596 static int ldlm_setup(struct obd_device *obddev, obd_count len, void *buf)
597 {
598         struct ldlm_obd *ldlm = &obddev->u.ldlm;
599         int rc, i;
600         ENTRY;
601
602         if (ldlm_already_setup)
603                 RETURN(-EALREADY);
604
605         MOD_INC_USE_COUNT;
606
607         rc = ldlm_proc_setup(obddev);
608         if (rc != 0)
609                 GOTO(out_dec, rc);
610
611         ldlm->ldlm_cb_service =
612                 ptlrpc_init_svc(LDLM_NEVENTS, LDLM_NBUFS, LDLM_BUFSIZE,
613                                 LDLM_MAXREQSIZE, LDLM_CB_REQUEST_PORTAL,
614                                 LDLM_CB_REPLY_PORTAL, "self",
615                                 ldlm_callback_handler, "ldlm_cbd");
616
617         if (!ldlm->ldlm_cb_service)
618                 GOTO(out_proc, rc = -ENOMEM);
619
620         ldlm->ldlm_cancel_service =
621                 ptlrpc_init_svc(LDLM_NEVENTS, LDLM_NBUFS, LDLM_BUFSIZE,
622                                 LDLM_MAXREQSIZE, LDLM_CANCEL_REQUEST_PORTAL,
623                                 LDLM_CANCEL_REPLY_PORTAL, "self",
624                                 ldlm_cancel_handler, "ldlm_canceld");
625
626         if (!ldlm->ldlm_cancel_service)
627                 GOTO(out_proc, rc = -ENOMEM);
628
629         for (i = 0; i < LDLM_NUM_THREADS; i++) {
630                 char name[32];
631                 sprintf(name, "lustre_dlm_%02d", i);
632                 rc = ptlrpc_start_thread(obddev, ldlm->ldlm_cancel_service,
633                                          name);
634                 if (rc) {
635                         CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
636                         LBUG();
637                         GOTO(out_thread, rc);
638                 }
639         }
640
641         for (i = 0; i < LDLM_NUM_THREADS; i++) {
642                 char name[32];
643                 sprintf(name, "lustre_dlm_%02d", i);
644                 rc = ptlrpc_start_thread(obddev, ldlm->ldlm_cb_service, name);
645                 if (rc) {
646                         CERROR("cannot start LDLM thread #%d: rc %d\n", i, rc);
647                         LBUG();
648                         GOTO(out_thread, rc);
649                 }
650         }
651
652         INIT_LIST_HEAD(&waiting_locks_list);
653         spin_lock_init(&waiting_locks_spinlock);
654         waiting_locks_timer.function = waiting_locks_callback;
655         waiting_locks_timer.data = 0;
656         init_timer(&waiting_locks_timer);
657
658         ldlm_already_setup = 1;
659
660         RETURN(0);
661
662  out_thread:
663         ptlrpc_stop_all_threads(ldlm->ldlm_cancel_service);
664         ptlrpc_unregister_service(ldlm->ldlm_cancel_service);
665         ptlrpc_stop_all_threads(ldlm->ldlm_cb_service);
666         ptlrpc_unregister_service(ldlm->ldlm_cb_service);
667
668  out_proc:
669         ldlm_proc_cleanup(obddev);
670
671  out_dec:
672         MOD_DEC_USE_COUNT;
673         return rc;
674 }
675
676 static int ldlm_cleanup(struct obd_device *obddev)
677 {
678         struct ldlm_obd *ldlm = &obddev->u.ldlm;
679         ENTRY;
680
681         if (!list_empty(&ldlm_namespace_list)) {
682                 CERROR("ldlm still has namespaces; clean these up first.\n");
683                 RETURN(-EBUSY);
684         }
685
686         ptlrpc_stop_all_threads(ldlm->ldlm_cb_service);
687         ptlrpc_unregister_service(ldlm->ldlm_cb_service);
688         ptlrpc_stop_all_threads(ldlm->ldlm_cancel_service);
689         ptlrpc_unregister_service(ldlm->ldlm_cancel_service);
690         ldlm_proc_cleanup(obddev);
691
692         ldlm_already_setup = 0;
693         MOD_DEC_USE_COUNT;
694         RETURN(0);
695 }
696
697 static int ldlm_connect(struct lustre_handle *conn, struct obd_device *src,
698                         obd_uuid_t cluuid, struct recovd_obd *recovd,
699                         ptlrpc_recovery_cb_t recover)
700 {
701         return class_connect(conn, src, cluuid);
702 }
703
704 struct obd_ops ldlm_obd_ops = {
705         o_iocontrol:   ldlm_iocontrol,
706         o_setup:       ldlm_setup,
707         o_cleanup:     ldlm_cleanup,
708         o_connect:     ldlm_connect,
709         o_disconnect:  class_disconnect
710 };
711
712 static int __init ldlm_init(void)
713 {
714         int rc = class_register_type(&ldlm_obd_ops, 0, OBD_LDLM_DEVICENAME);
715         if (rc != 0)
716                 return rc;
717
718         ldlm_resource_slab = kmem_cache_create("ldlm_resources",
719                                                sizeof(struct ldlm_resource), 0,
720                                                SLAB_HWCACHE_ALIGN, NULL, NULL);
721         if (ldlm_resource_slab == NULL)
722                 return -ENOMEM;
723
724         ldlm_lock_slab = kmem_cache_create("ldlm_locks",
725                                            sizeof(struct ldlm_lock), 0,
726                                            SLAB_HWCACHE_ALIGN, NULL, NULL);
727         if (ldlm_lock_slab == NULL) {
728                 kmem_cache_destroy(ldlm_resource_slab);
729                 return -ENOMEM;
730         }
731
732         return 0;
733 }
734
735 static void __exit ldlm_exit(void)
736 {
737         class_unregister_type(OBD_LDLM_DEVICENAME);
738         if (kmem_cache_destroy(ldlm_resource_slab) != 0)
739                 CERROR("couldn't free ldlm resource slab\n");
740         if (kmem_cache_destroy(ldlm_lock_slab) != 0)
741                 CERROR("couldn't free ldlm lock slab\n");
742 }
743
744 EXPORT_SYMBOL(ldlm_completion_ast);
745 EXPORT_SYMBOL(ldlm_handle_enqueue);
746 EXPORT_SYMBOL(ldlm_handle_cancel);
747 EXPORT_SYMBOL(ldlm_handle_convert);
748 EXPORT_SYMBOL(ldlm_register_intent);
749 EXPORT_SYMBOL(ldlm_unregister_intent);
750 EXPORT_SYMBOL(ldlm_lockname);
751 EXPORT_SYMBOL(ldlm_typename);
752 EXPORT_SYMBOL(ldlm_handle2lock);
753 EXPORT_SYMBOL(ldlm_lock2handle);
754 EXPORT_SYMBOL(ldlm_lock_put);
755 EXPORT_SYMBOL(ldlm_lock_match);
756 EXPORT_SYMBOL(ldlm_lock_addref);
757 EXPORT_SYMBOL(ldlm_lock_decref);
758 EXPORT_SYMBOL(ldlm_lock_change_resource);
759 EXPORT_SYMBOL(ldlm_lock_set_data);
760 EXPORT_SYMBOL(ldlm_cli_convert);
761 EXPORT_SYMBOL(ldlm_cli_enqueue);
762 EXPORT_SYMBOL(ldlm_cli_cancel);
763 EXPORT_SYMBOL(ldlm_cli_cancel_unused);
764 EXPORT_SYMBOL(ldlm_match_or_enqueue);
765 EXPORT_SYMBOL(ldlm_it2str);
766 EXPORT_SYMBOL(ldlm_test);
767 EXPORT_SYMBOL(ldlm_regression_start);
768 EXPORT_SYMBOL(ldlm_regression_stop);
769 EXPORT_SYMBOL(ldlm_lock_dump);
770 EXPORT_SYMBOL(ldlm_namespace_new);
771 EXPORT_SYMBOL(ldlm_namespace_cleanup);
772 EXPORT_SYMBOL(ldlm_namespace_free);
773 EXPORT_SYMBOL(ldlm_namespace_dump);
774 EXPORT_SYMBOL(ldlm_cancel_locks_for_export);
775 EXPORT_SYMBOL(l_lock);
776 EXPORT_SYMBOL(l_unlock);
777
778 MODULE_AUTHOR("Cluster File Systems, Inc. <info@clusterfs.com>");
779 MODULE_DESCRIPTION("Lustre Lock Management Module v0.1");
780 MODULE_LICENSE("GPL");
781
782 module_init(ldlm_init);
783 module_exit(ldlm_exit);