1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2010, 2012, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ldlm/ldlm_lockd.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Phil Schwan <phil@clusterfs.com>
40  */
41 
42 #define DEBUG_SUBSYSTEM S_LDLM
43 
44 #include "../../include/linux/libcfs/libcfs.h"
45 #include "../include/lustre_dlm.h"
46 #include "../include/obd_class.h"
47 #include <linux/list.h>
48 #include "ldlm_internal.h"
49 
50 static int ldlm_num_threads;
51 module_param(ldlm_num_threads, int, 0444);
52 MODULE_PARM_DESC(ldlm_num_threads, "number of DLM service threads to start");
53 
54 static char *ldlm_cpts;
55 module_param(ldlm_cpts, charp, 0444);
56 MODULE_PARM_DESC(ldlm_cpts, "CPU partitions ldlm threads should run on");
57 
58 static struct mutex	ldlm_ref_mutex;
59 static int ldlm_refcount;
60 
61 struct ldlm_cb_async_args {
62 	struct ldlm_cb_set_arg *ca_set_arg;
63 	struct ldlm_lock       *ca_lock;
64 };
65 
66 /* LDLM state */
67 
68 static struct ldlm_state *ldlm_state;
69 
round_timeout(unsigned long timeout)70 inline unsigned long round_timeout(unsigned long timeout)
71 {
72 	return cfs_time_seconds((int)cfs_duration_sec(cfs_time_sub(timeout, 0)) + 1);
73 }
74 
75 /* timeout for initial callback (AST) reply (bz10399) */
ldlm_get_rq_timeout(void)76 static inline unsigned int ldlm_get_rq_timeout(void)
77 {
78 	/* Non-AT value */
79 	unsigned int timeout = min(ldlm_timeout, obd_timeout / 3);
80 
81 	return timeout < 1 ? 1 : timeout;
82 }
83 
84 #define ELT_STOPPED   0
85 #define ELT_READY     1
86 #define ELT_TERMINATE 2
87 
88 struct ldlm_bl_pool {
89 	spinlock_t		blp_lock;
90 
91 	/*
92 	 * blp_prio_list is used for callbacks that should be handled
93 	 * as a priority. It is used for LDLM_FL_DISCARD_DATA requests.
94 	 * see bug 13843
95 	 */
96 	struct list_head	      blp_prio_list;
97 
98 	/*
99 	 * blp_list is used for all other callbacks which are likely
100 	 * to take longer to process.
101 	 */
102 	struct list_head	      blp_list;
103 
104 	wait_queue_head_t	     blp_waitq;
105 	struct completion	blp_comp;
106 	atomic_t	    blp_num_threads;
107 	atomic_t	    blp_busy_threads;
108 	int		     blp_min_threads;
109 	int		     blp_max_threads;
110 };
111 
112 struct ldlm_bl_work_item {
113 	struct list_head	      blwi_entry;
114 	struct ldlm_namespace  *blwi_ns;
115 	struct ldlm_lock_desc   blwi_ld;
116 	struct ldlm_lock       *blwi_lock;
117 	struct list_head	      blwi_head;
118 	int		     blwi_count;
119 	struct completion	blwi_comp;
120 	ldlm_cancel_flags_t     blwi_flags;
121 	int		     blwi_mem_pressure;
122 };
123 
124 
ldlm_del_waiting_lock(struct ldlm_lock * lock)125 int ldlm_del_waiting_lock(struct ldlm_lock *lock)
126 {
127 	return 0;
128 }
129 
ldlm_refresh_waiting_lock(struct ldlm_lock * lock,int timeout)130 int ldlm_refresh_waiting_lock(struct ldlm_lock *lock, int timeout)
131 {
132 	return 0;
133 }
134 
135 
136 
137 /**
138  * Callback handler for receiving incoming blocking ASTs.
139  *
140  * This can only happen on client side.
141  */
ldlm_handle_bl_callback(struct ldlm_namespace * ns,struct ldlm_lock_desc * ld,struct ldlm_lock * lock)142 void ldlm_handle_bl_callback(struct ldlm_namespace *ns,
143 			     struct ldlm_lock_desc *ld, struct ldlm_lock *lock)
144 {
145 	int do_ast;
146 
147 	LDLM_DEBUG(lock, "client blocking AST callback handler");
148 
149 	lock_res_and_lock(lock);
150 	lock->l_flags |= LDLM_FL_CBPENDING;
151 
152 	if (lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)
153 		lock->l_flags |= LDLM_FL_CANCEL;
154 
155 	do_ast = !lock->l_readers && !lock->l_writers;
156 	unlock_res_and_lock(lock);
157 
158 	if (do_ast) {
159 		CDEBUG(D_DLMTRACE,
160 		       "Lock %p already unused, calling callback (%p)\n", lock,
161 		       lock->l_blocking_ast);
162 		if (lock->l_blocking_ast != NULL)
163 			lock->l_blocking_ast(lock, ld, lock->l_ast_data,
164 					     LDLM_CB_BLOCKING);
165 	} else {
166 		CDEBUG(D_DLMTRACE,
167 		       "Lock %p is referenced, will be cancelled later\n",
168 		       lock);
169 	}
170 
171 	LDLM_DEBUG(lock, "client blocking callback handler END");
172 	LDLM_LOCK_RELEASE(lock);
173 }
174 
175 /**
176  * Callback handler for receiving incoming completion ASTs.
177  *
178  * This only can happen on client side.
179  */
ldlm_handle_cp_callback(struct ptlrpc_request * req,struct ldlm_namespace * ns,struct ldlm_request * dlm_req,struct ldlm_lock * lock)180 static void ldlm_handle_cp_callback(struct ptlrpc_request *req,
181 				    struct ldlm_namespace *ns,
182 				    struct ldlm_request *dlm_req,
183 				    struct ldlm_lock *lock)
184 {
185 	int lvb_len;
186 	LIST_HEAD(ast_list);
187 	int rc = 0;
188 
189 	LDLM_DEBUG(lock, "client completion callback handler START");
190 
191 	if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE)) {
192 		int to = cfs_time_seconds(1);
193 
194 		while (to > 0) {
195 			set_current_state(TASK_INTERRUPTIBLE);
196 			schedule_timeout(to);
197 			if (lock->l_granted_mode == lock->l_req_mode ||
198 			    lock->l_flags & LDLM_FL_DESTROYED)
199 				break;
200 		}
201 	}
202 
203 	lvb_len = req_capsule_get_size(&req->rq_pill, &RMF_DLM_LVB, RCL_CLIENT);
204 	if (lvb_len < 0) {
205 		LDLM_ERROR(lock, "Fail to get lvb_len, rc = %d", lvb_len);
206 		rc = lvb_len;
207 		goto out;
208 	} else if (lvb_len > 0) {
209 		if (lock->l_lvb_len > 0) {
210 			/* for extent lock, lvb contains ost_lvb{}. */
211 			LASSERT(lock->l_lvb_data != NULL);
212 
213 			if (unlikely(lock->l_lvb_len < lvb_len)) {
214 				LDLM_ERROR(lock, "Replied LVB is larger than expectation, expected = %d, replied = %d",
215 					   lock->l_lvb_len, lvb_len);
216 				rc = -EINVAL;
217 				goto out;
218 			}
219 		} else if (ldlm_has_layout(lock)) { /* for layout lock, lvb has
220 						     * variable length */
221 			void *lvb_data;
222 
223 			OBD_ALLOC(lvb_data, lvb_len);
224 			if (lvb_data == NULL) {
225 				LDLM_ERROR(lock, "No memory: %d.\n", lvb_len);
226 				rc = -ENOMEM;
227 				goto out;
228 			}
229 
230 			lock_res_and_lock(lock);
231 			LASSERT(lock->l_lvb_data == NULL);
232 			lock->l_lvb_type = LVB_T_LAYOUT;
233 			lock->l_lvb_data = lvb_data;
234 			lock->l_lvb_len = lvb_len;
235 			unlock_res_and_lock(lock);
236 		}
237 	}
238 
239 	lock_res_and_lock(lock);
240 	if ((lock->l_flags & LDLM_FL_DESTROYED) ||
241 	    lock->l_granted_mode == lock->l_req_mode) {
242 		/* bug 11300: the lock has already been granted */
243 		unlock_res_and_lock(lock);
244 		LDLM_DEBUG(lock, "Double grant race happened");
245 		rc = 0;
246 		goto out;
247 	}
248 
249 	/* If we receive the completion AST before the actual enqueue returned,
250 	 * then we might need to switch lock modes, resources, or extents. */
251 	if (dlm_req->lock_desc.l_granted_mode != lock->l_req_mode) {
252 		lock->l_req_mode = dlm_req->lock_desc.l_granted_mode;
253 		LDLM_DEBUG(lock, "completion AST, new lock mode");
254 	}
255 
256 	if (lock->l_resource->lr_type != LDLM_PLAIN) {
257 		ldlm_convert_policy_to_local(req->rq_export,
258 					  dlm_req->lock_desc.l_resource.lr_type,
259 					  &dlm_req->lock_desc.l_policy_data,
260 					  &lock->l_policy_data);
261 		LDLM_DEBUG(lock, "completion AST, new policy data");
262 	}
263 
264 	ldlm_resource_unlink_lock(lock);
265 	if (memcmp(&dlm_req->lock_desc.l_resource.lr_name,
266 		   &lock->l_resource->lr_name,
267 		   sizeof(lock->l_resource->lr_name)) != 0) {
268 		unlock_res_and_lock(lock);
269 		rc = ldlm_lock_change_resource(ns, lock,
270 				&dlm_req->lock_desc.l_resource.lr_name);
271 		if (rc < 0) {
272 			LDLM_ERROR(lock, "Failed to allocate resource");
273 			goto out;
274 		}
275 		LDLM_DEBUG(lock, "completion AST, new resource");
276 		CERROR("change resource!\n");
277 		lock_res_and_lock(lock);
278 	}
279 
280 	if (dlm_req->lock_flags & LDLM_FL_AST_SENT) {
281 		/* BL_AST locks are not needed in LRU.
282 		 * Let ldlm_cancel_lru() be fast. */
283 		ldlm_lock_remove_from_lru(lock);
284 		lock->l_flags |= LDLM_FL_CBPENDING | LDLM_FL_BL_AST;
285 		LDLM_DEBUG(lock, "completion AST includes blocking AST");
286 	}
287 
288 	if (lock->l_lvb_len > 0) {
289 		rc = ldlm_fill_lvb(lock, &req->rq_pill, RCL_CLIENT,
290 				   lock->l_lvb_data, lvb_len);
291 		if (rc < 0) {
292 			unlock_res_and_lock(lock);
293 			goto out;
294 		}
295 	}
296 
297 	ldlm_grant_lock(lock, &ast_list);
298 	unlock_res_and_lock(lock);
299 
300 	LDLM_DEBUG(lock, "callback handler finished, about to run_ast_work");
301 
302 	/* Let Enqueue to call osc_lock_upcall() and initialize
303 	 * l_ast_data */
304 	OBD_FAIL_TIMEOUT(OBD_FAIL_OSC_CP_ENQ_RACE, 2);
305 
306 	ldlm_run_ast_work(ns, &ast_list, LDLM_WORK_CP_AST);
307 
308 	LDLM_DEBUG_NOLOCK("client completion callback handler END (lock %p)",
309 			  lock);
310 	goto out;
311 
312 out:
313 	if (rc < 0) {
314 		lock_res_and_lock(lock);
315 		lock->l_flags |= LDLM_FL_FAILED;
316 		unlock_res_and_lock(lock);
317 		wake_up(&lock->l_waitq);
318 	}
319 	LDLM_LOCK_RELEASE(lock);
320 }
321 
322 /**
323  * Callback handler for receiving incoming glimpse ASTs.
324  *
325  * This only can happen on client side.  After handling the glimpse AST
326  * we also consider dropping the lock here if it is unused locally for a
327  * long time.
328  */
ldlm_handle_gl_callback(struct ptlrpc_request * req,struct ldlm_namespace * ns,struct ldlm_request * dlm_req,struct ldlm_lock * lock)329 static void ldlm_handle_gl_callback(struct ptlrpc_request *req,
330 				    struct ldlm_namespace *ns,
331 				    struct ldlm_request *dlm_req,
332 				    struct ldlm_lock *lock)
333 {
334 	int rc = -ENOSYS;
335 
336 	LDLM_DEBUG(lock, "client glimpse AST callback handler");
337 
338 	if (lock->l_glimpse_ast != NULL)
339 		rc = lock->l_glimpse_ast(lock, req);
340 
341 	if (req->rq_repmsg != NULL) {
342 		ptlrpc_reply(req);
343 	} else {
344 		req->rq_status = rc;
345 		ptlrpc_error(req);
346 	}
347 
348 	lock_res_and_lock(lock);
349 	if (lock->l_granted_mode == LCK_PW &&
350 	    !lock->l_readers && !lock->l_writers &&
351 	    cfs_time_after(cfs_time_current(),
352 			   cfs_time_add(lock->l_last_used,
353 					cfs_time_seconds(10)))) {
354 		unlock_res_and_lock(lock);
355 		if (ldlm_bl_to_thread_lock(ns, NULL, lock))
356 			ldlm_handle_bl_callback(ns, NULL, lock);
357 
358 		return;
359 	}
360 	unlock_res_and_lock(lock);
361 	LDLM_LOCK_RELEASE(lock);
362 }
363 
ldlm_callback_reply(struct ptlrpc_request * req,int rc)364 static int ldlm_callback_reply(struct ptlrpc_request *req, int rc)
365 {
366 	if (req->rq_no_reply)
367 		return 0;
368 
369 	req->rq_status = rc;
370 	if (!req->rq_packed_final) {
371 		rc = lustre_pack_reply(req, 1, NULL, NULL);
372 		if (rc)
373 			return rc;
374 	}
375 	return ptlrpc_reply(req);
376 }
377 
__ldlm_bl_to_thread(struct ldlm_bl_work_item * blwi,ldlm_cancel_flags_t cancel_flags)378 static int __ldlm_bl_to_thread(struct ldlm_bl_work_item *blwi,
379 			       ldlm_cancel_flags_t cancel_flags)
380 {
381 	struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
382 
383 	spin_lock(&blp->blp_lock);
384 	if (blwi->blwi_lock &&
385 	    blwi->blwi_lock->l_flags & LDLM_FL_DISCARD_DATA) {
386 		/* add LDLM_FL_DISCARD_DATA requests to the priority list */
387 		list_add_tail(&blwi->blwi_entry, &blp->blp_prio_list);
388 	} else {
389 		/* other blocking callbacks are added to the regular list */
390 		list_add_tail(&blwi->blwi_entry, &blp->blp_list);
391 	}
392 	spin_unlock(&blp->blp_lock);
393 
394 	wake_up(&blp->blp_waitq);
395 
396 	/* can not check blwi->blwi_flags as blwi could be already freed in
397 	   LCF_ASYNC mode */
398 	if (!(cancel_flags & LCF_ASYNC))
399 		wait_for_completion(&blwi->blwi_comp);
400 
401 	return 0;
402 }
403 
init_blwi(struct ldlm_bl_work_item * blwi,struct ldlm_namespace * ns,struct ldlm_lock_desc * ld,struct list_head * cancels,int count,struct ldlm_lock * lock,ldlm_cancel_flags_t cancel_flags)404 static inline void init_blwi(struct ldlm_bl_work_item *blwi,
405 			     struct ldlm_namespace *ns,
406 			     struct ldlm_lock_desc *ld,
407 			     struct list_head *cancels, int count,
408 			     struct ldlm_lock *lock,
409 			     ldlm_cancel_flags_t cancel_flags)
410 {
411 	init_completion(&blwi->blwi_comp);
412 	INIT_LIST_HEAD(&blwi->blwi_head);
413 
414 	if (memory_pressure_get())
415 		blwi->blwi_mem_pressure = 1;
416 
417 	blwi->blwi_ns = ns;
418 	blwi->blwi_flags = cancel_flags;
419 	if (ld != NULL)
420 		blwi->blwi_ld = *ld;
421 	if (count) {
422 		list_add(&blwi->blwi_head, cancels);
423 		list_del_init(cancels);
424 		blwi->blwi_count = count;
425 	} else {
426 		blwi->blwi_lock = lock;
427 	}
428 }
429 
430 /**
431  * Queues a list of locks \a cancels containing \a count locks
432  * for later processing by a blocking thread.  If \a count is zero,
433  * then the lock referenced as \a lock is queued instead.
434  *
435  * The blocking thread would then call ->l_blocking_ast callback in the lock.
436  * If list addition fails an error is returned and caller is supposed to
437  * call ->l_blocking_ast itself.
438  */
ldlm_bl_to_thread(struct ldlm_namespace * ns,struct ldlm_lock_desc * ld,struct ldlm_lock * lock,struct list_head * cancels,int count,ldlm_cancel_flags_t cancel_flags)439 static int ldlm_bl_to_thread(struct ldlm_namespace *ns,
440 			     struct ldlm_lock_desc *ld,
441 			     struct ldlm_lock *lock,
442 			     struct list_head *cancels, int count,
443 			     ldlm_cancel_flags_t cancel_flags)
444 {
445 	if (cancels && count == 0)
446 		return 0;
447 
448 	if (cancel_flags & LCF_ASYNC) {
449 		struct ldlm_bl_work_item *blwi;
450 
451 		OBD_ALLOC(blwi, sizeof(*blwi));
452 		if (blwi == NULL)
453 			return -ENOMEM;
454 		init_blwi(blwi, ns, ld, cancels, count, lock, cancel_flags);
455 
456 		return __ldlm_bl_to_thread(blwi, cancel_flags);
457 	} else {
458 		/* if it is synchronous call do minimum mem alloc, as it could
459 		 * be triggered from kernel shrinker
460 		 */
461 		struct ldlm_bl_work_item blwi;
462 
463 		memset(&blwi, 0, sizeof(blwi));
464 		init_blwi(&blwi, ns, ld, cancels, count, lock, cancel_flags);
465 		return __ldlm_bl_to_thread(&blwi, cancel_flags);
466 	}
467 }
468 
469 
ldlm_bl_to_thread_lock(struct ldlm_namespace * ns,struct ldlm_lock_desc * ld,struct ldlm_lock * lock)470 int ldlm_bl_to_thread_lock(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
471 			   struct ldlm_lock *lock)
472 {
473 	return ldlm_bl_to_thread(ns, ld, lock, NULL, 0, LCF_ASYNC);
474 }
475 
ldlm_bl_to_thread_list(struct ldlm_namespace * ns,struct ldlm_lock_desc * ld,struct list_head * cancels,int count,ldlm_cancel_flags_t cancel_flags)476 int ldlm_bl_to_thread_list(struct ldlm_namespace *ns, struct ldlm_lock_desc *ld,
477 			   struct list_head *cancels, int count,
478 			   ldlm_cancel_flags_t cancel_flags)
479 {
480 	return ldlm_bl_to_thread(ns, ld, NULL, cancels, count, cancel_flags);
481 }
482 
483 /* Setinfo coming from Server (eg MDT) to Client (eg MDC)! */
ldlm_handle_setinfo(struct ptlrpc_request * req)484 static int ldlm_handle_setinfo(struct ptlrpc_request *req)
485 {
486 	struct obd_device *obd = req->rq_export->exp_obd;
487 	char *key;
488 	void *val;
489 	int keylen, vallen;
490 	int rc = -ENOSYS;
491 
492 	DEBUG_REQ(D_HSM, req, "%s: handle setinfo\n", obd->obd_name);
493 
494 	req_capsule_set(&req->rq_pill, &RQF_OBD_SET_INFO);
495 
496 	key = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_KEY);
497 	if (key == NULL) {
498 		DEBUG_REQ(D_IOCTL, req, "no set_info key");
499 		return -EFAULT;
500 	}
501 	keylen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_KEY,
502 				      RCL_CLIENT);
503 	val = req_capsule_client_get(&req->rq_pill, &RMF_SETINFO_VAL);
504 	if (val == NULL) {
505 		DEBUG_REQ(D_IOCTL, req, "no set_info val");
506 		return -EFAULT;
507 	}
508 	vallen = req_capsule_get_size(&req->rq_pill, &RMF_SETINFO_VAL,
509 				      RCL_CLIENT);
510 
511 	/* We are responsible for swabbing contents of val */
512 
513 	if (KEY_IS(KEY_HSM_COPYTOOL_SEND))
514 		/* Pass it on to mdc (the "export" in this case) */
515 		rc = obd_set_info_async(req->rq_svc_thread->t_env,
516 					req->rq_export,
517 					sizeof(KEY_HSM_COPYTOOL_SEND),
518 					KEY_HSM_COPYTOOL_SEND,
519 					vallen, val, NULL);
520 	else
521 		DEBUG_REQ(D_WARNING, req, "ignoring unknown key %s", key);
522 
523 	return rc;
524 }
525 
ldlm_callback_errmsg(struct ptlrpc_request * req,const char * msg,int rc,struct lustre_handle * handle)526 static inline void ldlm_callback_errmsg(struct ptlrpc_request *req,
527 					const char *msg, int rc,
528 					struct lustre_handle *handle)
529 {
530 	DEBUG_REQ((req->rq_no_reply || rc) ? D_WARNING : D_DLMTRACE, req,
531 		  "%s: [nid %s] [rc %d] [lock %#llx]",
532 		  msg, libcfs_id2str(req->rq_peer), rc,
533 		  handle ? handle->cookie : 0);
534 	if (req->rq_no_reply)
535 		CWARN("No reply was sent, maybe cause bug 21636.\n");
536 	else if (rc)
537 		CWARN("Send reply failed, maybe cause bug 21636.\n");
538 }
539 
ldlm_handle_qc_callback(struct ptlrpc_request * req)540 static int ldlm_handle_qc_callback(struct ptlrpc_request *req)
541 {
542 	struct obd_quotactl *oqctl;
543 	struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
544 
545 	oqctl = req_capsule_client_get(&req->rq_pill, &RMF_OBD_QUOTACTL);
546 	if (oqctl == NULL) {
547 		CERROR("Can't unpack obd_quotactl\n");
548 		return -EPROTO;
549 	}
550 
551 	oqctl->qc_stat = ptlrpc_status_ntoh(oqctl->qc_stat);
552 
553 	cli->cl_qchk_stat = oqctl->qc_stat;
554 	return 0;
555 }
556 
557 /* TODO: handle requests in a similar way as MDT: see mdt_handle_common() */
ldlm_callback_handler(struct ptlrpc_request * req)558 static int ldlm_callback_handler(struct ptlrpc_request *req)
559 {
560 	struct ldlm_namespace *ns;
561 	struct ldlm_request *dlm_req;
562 	struct ldlm_lock *lock;
563 	int rc;
564 
565 	/* Requests arrive in sender's byte order.  The ptlrpc service
566 	 * handler has already checked and, if necessary, byte-swapped the
567 	 * incoming request message body, but I am responsible for the
568 	 * message buffers. */
569 
570 	/* do nothing for sec context finalize */
571 	if (lustre_msg_get_opc(req->rq_reqmsg) == SEC_CTX_FINI)
572 		return 0;
573 
574 	req_capsule_init(&req->rq_pill, req, RCL_SERVER);
575 
576 	if (req->rq_export == NULL) {
577 		rc = ldlm_callback_reply(req, -ENOTCONN);
578 		ldlm_callback_errmsg(req, "Operate on unconnected server",
579 				     rc, NULL);
580 		return 0;
581 	}
582 
583 	LASSERT(req->rq_export != NULL);
584 	LASSERT(req->rq_export->exp_obd != NULL);
585 
586 	switch (lustre_msg_get_opc(req->rq_reqmsg)) {
587 	case LDLM_BL_CALLBACK:
588 		if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_BL_CALLBACK_NET))
589 			return 0;
590 		break;
591 	case LDLM_CP_CALLBACK:
592 		if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CP_CALLBACK_NET))
593 			return 0;
594 		break;
595 	case LDLM_GL_CALLBACK:
596 		if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_GL_CALLBACK_NET))
597 			return 0;
598 		break;
599 	case LDLM_SET_INFO:
600 		rc = ldlm_handle_setinfo(req);
601 		ldlm_callback_reply(req, rc);
602 		return 0;
603 	case OBD_QC_CALLBACK:
604 		req_capsule_set(&req->rq_pill, &RQF_QC_CALLBACK);
605 		if (OBD_FAIL_CHECK(OBD_FAIL_OBD_QC_CALLBACK_NET))
606 			return 0;
607 		rc = ldlm_handle_qc_callback(req);
608 		ldlm_callback_reply(req, rc);
609 		return 0;
610 	default:
611 		CERROR("unknown opcode %u\n",
612 		       lustre_msg_get_opc(req->rq_reqmsg));
613 		ldlm_callback_reply(req, -EPROTO);
614 		return 0;
615 	}
616 
617 	ns = req->rq_export->exp_obd->obd_namespace;
618 	LASSERT(ns != NULL);
619 
620 	req_capsule_set(&req->rq_pill, &RQF_LDLM_CALLBACK);
621 
622 	dlm_req = req_capsule_client_get(&req->rq_pill, &RMF_DLM_REQ);
623 	if (dlm_req == NULL) {
624 		rc = ldlm_callback_reply(req, -EPROTO);
625 		ldlm_callback_errmsg(req, "Operate without parameter", rc,
626 				     NULL);
627 		return 0;
628 	}
629 
630 	/* Force a known safe race, send a cancel to the server for a lock
631 	 * which the server has already started a blocking callback on. */
632 	if (OBD_FAIL_CHECK(OBD_FAIL_LDLM_CANCEL_BL_CB_RACE) &&
633 	    lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
634 		rc = ldlm_cli_cancel(&dlm_req->lock_handle[0], 0);
635 		if (rc < 0)
636 			CERROR("ldlm_cli_cancel: %d\n", rc);
637 	}
638 
639 	lock = ldlm_handle2lock_long(&dlm_req->lock_handle[0], 0);
640 	if (!lock) {
641 		CDEBUG(D_DLMTRACE, "callback on lock %#llx - lock disappeared\n",
642 		       dlm_req->lock_handle[0].cookie);
643 		rc = ldlm_callback_reply(req, -EINVAL);
644 		ldlm_callback_errmsg(req, "Operate with invalid parameter", rc,
645 				     &dlm_req->lock_handle[0]);
646 		return 0;
647 	}
648 
649 	if ((lock->l_flags & LDLM_FL_FAIL_LOC) &&
650 	    lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK)
651 		OBD_RACE(OBD_FAIL_LDLM_CP_BL_RACE);
652 
653 	/* Copy hints/flags (e.g. LDLM_FL_DISCARD_DATA) from AST. */
654 	lock_res_and_lock(lock);
655 	lock->l_flags |= ldlm_flags_from_wire(dlm_req->lock_flags &
656 					      LDLM_AST_FLAGS);
657 	if (lustre_msg_get_opc(req->rq_reqmsg) == LDLM_BL_CALLBACK) {
658 		/* If somebody cancels lock and cache is already dropped,
659 		 * or lock is failed before cp_ast received on client,
660 		 * we can tell the server we have no lock. Otherwise, we
661 		 * should send cancel after dropping the cache. */
662 		if (((lock->l_flags & LDLM_FL_CANCELING) &&
663 		    (lock->l_flags & LDLM_FL_BL_DONE)) ||
664 		    (lock->l_flags & LDLM_FL_FAILED)) {
665 			LDLM_DEBUG(lock, "callback on lock %#llx - lock disappeared\n",
666 				   dlm_req->lock_handle[0].cookie);
667 			unlock_res_and_lock(lock);
668 			LDLM_LOCK_RELEASE(lock);
669 			rc = ldlm_callback_reply(req, -EINVAL);
670 			ldlm_callback_errmsg(req, "Operate on stale lock", rc,
671 					     &dlm_req->lock_handle[0]);
672 			return 0;
673 		}
674 		/* BL_AST locks are not needed in LRU.
675 		 * Let ldlm_cancel_lru() be fast. */
676 		ldlm_lock_remove_from_lru(lock);
677 		lock->l_flags |= LDLM_FL_BL_AST;
678 	}
679 	unlock_res_and_lock(lock);
680 
681 	/* We want the ost thread to get this reply so that it can respond
682 	 * to ost requests (write cache writeback) that might be triggered
683 	 * in the callback.
684 	 *
685 	 * But we'd also like to be able to indicate in the reply that we're
686 	 * cancelling right now, because it's unused, or have an intent result
687 	 * in the reply, so we might have to push the responsibility for sending
688 	 * the reply down into the AST handlers, alas. */
689 
690 	switch (lustre_msg_get_opc(req->rq_reqmsg)) {
691 	case LDLM_BL_CALLBACK:
692 		CDEBUG(D_INODE, "blocking ast\n");
693 		req_capsule_extend(&req->rq_pill, &RQF_LDLM_BL_CALLBACK);
694 		if (!(lock->l_flags & LDLM_FL_CANCEL_ON_BLOCK)) {
695 			rc = ldlm_callback_reply(req, 0);
696 			if (req->rq_no_reply || rc)
697 				ldlm_callback_errmsg(req, "Normal process", rc,
698 						     &dlm_req->lock_handle[0]);
699 		}
700 		if (ldlm_bl_to_thread_lock(ns, &dlm_req->lock_desc, lock))
701 			ldlm_handle_bl_callback(ns, &dlm_req->lock_desc, lock);
702 		break;
703 	case LDLM_CP_CALLBACK:
704 		CDEBUG(D_INODE, "completion ast\n");
705 		req_capsule_extend(&req->rq_pill, &RQF_LDLM_CP_CALLBACK);
706 		ldlm_callback_reply(req, 0);
707 		ldlm_handle_cp_callback(req, ns, dlm_req, lock);
708 		break;
709 	case LDLM_GL_CALLBACK:
710 		CDEBUG(D_INODE, "glimpse ast\n");
711 		req_capsule_extend(&req->rq_pill, &RQF_LDLM_GL_CALLBACK);
712 		ldlm_handle_gl_callback(req, ns, dlm_req, lock);
713 		break;
714 	default:
715 		LBUG();			 /* checked above */
716 	}
717 
718 	return 0;
719 }
720 
721 
ldlm_bl_get_work(struct ldlm_bl_pool * blp)722 static struct ldlm_bl_work_item *ldlm_bl_get_work(struct ldlm_bl_pool *blp)
723 {
724 	struct ldlm_bl_work_item *blwi = NULL;
725 	static unsigned int num_bl;
726 
727 	spin_lock(&blp->blp_lock);
728 	/* process a request from the blp_list at least every blp_num_threads */
729 	if (!list_empty(&blp->blp_list) &&
730 	    (list_empty(&blp->blp_prio_list) || num_bl == 0))
731 		blwi = list_entry(blp->blp_list.next,
732 				      struct ldlm_bl_work_item, blwi_entry);
733 	else
734 		if (!list_empty(&blp->blp_prio_list))
735 			blwi = list_entry(blp->blp_prio_list.next,
736 					      struct ldlm_bl_work_item,
737 					      blwi_entry);
738 
739 	if (blwi) {
740 		if (++num_bl >= atomic_read(&blp->blp_num_threads))
741 			num_bl = 0;
742 		list_del(&blwi->blwi_entry);
743 	}
744 	spin_unlock(&blp->blp_lock);
745 
746 	return blwi;
747 }
748 
749 /* This only contains temporary data until the thread starts */
750 struct ldlm_bl_thread_data {
751 	char			bltd_name[CFS_CURPROC_COMM_MAX];
752 	struct ldlm_bl_pool	*bltd_blp;
753 	struct completion	bltd_comp;
754 	int			bltd_num;
755 };
756 
757 static int ldlm_bl_thread_main(void *arg);
758 
ldlm_bl_thread_start(struct ldlm_bl_pool * blp)759 static int ldlm_bl_thread_start(struct ldlm_bl_pool *blp)
760 {
761 	struct ldlm_bl_thread_data bltd = { .bltd_blp = blp };
762 	struct task_struct *task;
763 
764 	init_completion(&bltd.bltd_comp);
765 	bltd.bltd_num = atomic_read(&blp->blp_num_threads);
766 	snprintf(bltd.bltd_name, sizeof(bltd.bltd_name),
767 		"ldlm_bl_%02d", bltd.bltd_num);
768 	task = kthread_run(ldlm_bl_thread_main, &bltd, "%s", bltd.bltd_name);
769 	if (IS_ERR(task)) {
770 		CERROR("cannot start LDLM thread ldlm_bl_%02d: rc %ld\n",
771 		       atomic_read(&blp->blp_num_threads), PTR_ERR(task));
772 		return PTR_ERR(task);
773 	}
774 	wait_for_completion(&bltd.bltd_comp);
775 
776 	return 0;
777 }
778 
779 /**
780  * Main blocking requests processing thread.
781  *
782  * Callers put locks into its queue by calling ldlm_bl_to_thread.
783  * This thread in the end ends up doing actual call to ->l_blocking_ast
784  * for queued locks.
785  */
ldlm_bl_thread_main(void * arg)786 static int ldlm_bl_thread_main(void *arg)
787 {
788 	struct ldlm_bl_pool *blp;
789 
790 	{
791 		struct ldlm_bl_thread_data *bltd = arg;
792 
793 		blp = bltd->bltd_blp;
794 
795 		atomic_inc(&blp->blp_num_threads);
796 		atomic_inc(&blp->blp_busy_threads);
797 
798 		complete(&bltd->bltd_comp);
799 		/* cannot use bltd after this, it is only on caller's stack */
800 	}
801 
802 	while (1) {
803 		struct l_wait_info lwi = { 0 };
804 		struct ldlm_bl_work_item *blwi = NULL;
805 		int busy;
806 
807 		blwi = ldlm_bl_get_work(blp);
808 
809 		if (blwi == NULL) {
810 			atomic_dec(&blp->blp_busy_threads);
811 			l_wait_event_exclusive(blp->blp_waitq,
812 					 (blwi = ldlm_bl_get_work(blp)) != NULL,
813 					 &lwi);
814 			busy = atomic_inc_return(&blp->blp_busy_threads);
815 		} else {
816 			busy = atomic_read(&blp->blp_busy_threads);
817 		}
818 
819 		if (blwi->blwi_ns == NULL)
820 			/* added by ldlm_cleanup() */
821 			break;
822 
823 		/* Not fatal if racy and have a few too many threads */
824 		if (unlikely(busy < blp->blp_max_threads &&
825 			     busy >= atomic_read(&blp->blp_num_threads) &&
826 			     !blwi->blwi_mem_pressure))
827 			/* discard the return value, we tried */
828 			ldlm_bl_thread_start(blp);
829 
830 		if (blwi->blwi_mem_pressure)
831 			memory_pressure_set();
832 
833 		if (blwi->blwi_count) {
834 			int count;
835 			/* The special case when we cancel locks in LRU
836 			 * asynchronously, we pass the list of locks here.
837 			 * Thus locks are marked LDLM_FL_CANCELING, but NOT
838 			 * canceled locally yet. */
839 			count = ldlm_cli_cancel_list_local(&blwi->blwi_head,
840 							   blwi->blwi_count,
841 							   LCF_BL_AST);
842 			ldlm_cli_cancel_list(&blwi->blwi_head, count, NULL,
843 					     blwi->blwi_flags);
844 		} else {
845 			ldlm_handle_bl_callback(blwi->blwi_ns, &blwi->blwi_ld,
846 						blwi->blwi_lock);
847 		}
848 		if (blwi->blwi_mem_pressure)
849 			memory_pressure_clr();
850 
851 		if (blwi->blwi_flags & LCF_ASYNC)
852 			OBD_FREE(blwi, sizeof(*blwi));
853 		else
854 			complete(&blwi->blwi_comp);
855 	}
856 
857 	atomic_dec(&blp->blp_busy_threads);
858 	atomic_dec(&blp->blp_num_threads);
859 	complete(&blp->blp_comp);
860 	return 0;
861 }
862 
863 
864 static int ldlm_setup(void);
865 static int ldlm_cleanup(void);
866 
ldlm_get_ref(void)867 int ldlm_get_ref(void)
868 {
869 	int rc = 0;
870 
871 	mutex_lock(&ldlm_ref_mutex);
872 	if (++ldlm_refcount == 1) {
873 		rc = ldlm_setup();
874 		if (rc)
875 			ldlm_refcount--;
876 	}
877 	mutex_unlock(&ldlm_ref_mutex);
878 
879 	return rc;
880 }
881 EXPORT_SYMBOL(ldlm_get_ref);
882 
ldlm_put_ref(void)883 void ldlm_put_ref(void)
884 {
885 	mutex_lock(&ldlm_ref_mutex);
886 	if (ldlm_refcount == 1) {
887 		int rc = ldlm_cleanup();
888 
889 		if (rc)
890 			CERROR("ldlm_cleanup failed: %d\n", rc);
891 		else
892 			ldlm_refcount--;
893 	} else {
894 		ldlm_refcount--;
895 	}
896 	mutex_unlock(&ldlm_ref_mutex);
897 }
898 EXPORT_SYMBOL(ldlm_put_ref);
899 
900 /*
901  * Export handle<->lock hash operations.
902  */
903 static unsigned
ldlm_export_lock_hash(struct cfs_hash * hs,const void * key,unsigned mask)904 ldlm_export_lock_hash(struct cfs_hash *hs, const void *key, unsigned mask)
905 {
906 	return cfs_hash_u64_hash(((struct lustre_handle *)key)->cookie, mask);
907 }
908 
909 static void *
ldlm_export_lock_key(struct hlist_node * hnode)910 ldlm_export_lock_key(struct hlist_node *hnode)
911 {
912 	struct ldlm_lock *lock;
913 
914 	lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
915 	return &lock->l_remote_handle;
916 }
917 
918 static void
ldlm_export_lock_keycpy(struct hlist_node * hnode,void * key)919 ldlm_export_lock_keycpy(struct hlist_node *hnode, void *key)
920 {
921 	struct ldlm_lock     *lock;
922 
923 	lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
924 	lock->l_remote_handle = *(struct lustre_handle *)key;
925 }
926 
927 static int
ldlm_export_lock_keycmp(const void * key,struct hlist_node * hnode)928 ldlm_export_lock_keycmp(const void *key, struct hlist_node *hnode)
929 {
930 	return lustre_handle_equal(ldlm_export_lock_key(hnode), key);
931 }
932 
933 static void *
ldlm_export_lock_object(struct hlist_node * hnode)934 ldlm_export_lock_object(struct hlist_node *hnode)
935 {
936 	return hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
937 }
938 
939 static void
ldlm_export_lock_get(struct cfs_hash * hs,struct hlist_node * hnode)940 ldlm_export_lock_get(struct cfs_hash *hs, struct hlist_node *hnode)
941 {
942 	struct ldlm_lock *lock;
943 
944 	lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
945 	LDLM_LOCK_GET(lock);
946 }
947 
948 static void
ldlm_export_lock_put(struct cfs_hash * hs,struct hlist_node * hnode)949 ldlm_export_lock_put(struct cfs_hash *hs, struct hlist_node *hnode)
950 {
951 	struct ldlm_lock *lock;
952 
953 	lock = hlist_entry(hnode, struct ldlm_lock, l_exp_hash);
954 	LDLM_LOCK_RELEASE(lock);
955 }
956 
957 static cfs_hash_ops_t ldlm_export_lock_ops = {
958 	.hs_hash	= ldlm_export_lock_hash,
959 	.hs_key	 = ldlm_export_lock_key,
960 	.hs_keycmp      = ldlm_export_lock_keycmp,
961 	.hs_keycpy      = ldlm_export_lock_keycpy,
962 	.hs_object      = ldlm_export_lock_object,
963 	.hs_get	 = ldlm_export_lock_get,
964 	.hs_put	 = ldlm_export_lock_put,
965 	.hs_put_locked  = ldlm_export_lock_put,
966 };
967 
ldlm_init_export(struct obd_export * exp)968 int ldlm_init_export(struct obd_export *exp)
969 {
970 	int rc;
971 
972 	exp->exp_lock_hash =
973 		cfs_hash_create(obd_uuid2str(&exp->exp_client_uuid),
974 				HASH_EXP_LOCK_CUR_BITS,
975 				HASH_EXP_LOCK_MAX_BITS,
976 				HASH_EXP_LOCK_BKT_BITS, 0,
977 				CFS_HASH_MIN_THETA, CFS_HASH_MAX_THETA,
978 				&ldlm_export_lock_ops,
979 				CFS_HASH_DEFAULT | CFS_HASH_REHASH_KEY |
980 				CFS_HASH_NBLK_CHANGE);
981 
982 	if (!exp->exp_lock_hash)
983 		return -ENOMEM;
984 
985 	rc = ldlm_init_flock_export(exp);
986 	if (rc)
987 		goto err;
988 
989 	return 0;
990 err:
991 	ldlm_destroy_export(exp);
992 	return rc;
993 }
994 EXPORT_SYMBOL(ldlm_init_export);
995 
ldlm_destroy_export(struct obd_export * exp)996 void ldlm_destroy_export(struct obd_export *exp)
997 {
998 	cfs_hash_putref(exp->exp_lock_hash);
999 	exp->exp_lock_hash = NULL;
1000 
1001 	ldlm_destroy_flock_export(exp);
1002 }
1003 EXPORT_SYMBOL(ldlm_destroy_export);
1004 
ldlm_setup(void)1005 static int ldlm_setup(void)
1006 {
1007 	static struct ptlrpc_service_conf	conf;
1008 	struct ldlm_bl_pool			*blp = NULL;
1009 	int rc = 0;
1010 	int i;
1011 
1012 	if (ldlm_state != NULL)
1013 		return -EALREADY;
1014 
1015 	OBD_ALLOC(ldlm_state, sizeof(*ldlm_state));
1016 	if (ldlm_state == NULL)
1017 		return -ENOMEM;
1018 
1019 	rc = ldlm_proc_setup();
1020 	if (rc != 0)
1021 		goto out;
1022 
1023 	memset(&conf, 0, sizeof(conf));
1024 	conf = (typeof(conf)) {
1025 		.psc_name		= "ldlm_cbd",
1026 		.psc_watchdog_factor	= 2,
1027 		.psc_buf		= {
1028 			.bc_nbufs		= LDLM_CLIENT_NBUFS,
1029 			.bc_buf_size		= LDLM_BUFSIZE,
1030 			.bc_req_max_size	= LDLM_MAXREQSIZE,
1031 			.bc_rep_max_size	= LDLM_MAXREPSIZE,
1032 			.bc_req_portal		= LDLM_CB_REQUEST_PORTAL,
1033 			.bc_rep_portal		= LDLM_CB_REPLY_PORTAL,
1034 		},
1035 		.psc_thr		= {
1036 			.tc_thr_name		= "ldlm_cb",
1037 			.tc_thr_factor		= LDLM_THR_FACTOR,
1038 			.tc_nthrs_init		= LDLM_NTHRS_INIT,
1039 			.tc_nthrs_base		= LDLM_NTHRS_BASE,
1040 			.tc_nthrs_max		= LDLM_NTHRS_MAX,
1041 			.tc_nthrs_user		= ldlm_num_threads,
1042 			.tc_cpu_affinity	= 1,
1043 			.tc_ctx_tags		= LCT_MD_THREAD | LCT_DT_THREAD,
1044 		},
1045 		.psc_cpt		= {
1046 			.cc_pattern		= ldlm_cpts,
1047 		},
1048 		.psc_ops		= {
1049 			.so_req_handler		= ldlm_callback_handler,
1050 		},
1051 	};
1052 	ldlm_state->ldlm_cb_service =
1053 			ptlrpc_register_service(&conf, ldlm_svc_proc_dir);
1054 	if (IS_ERR(ldlm_state->ldlm_cb_service)) {
1055 		CERROR("failed to start service\n");
1056 		rc = PTR_ERR(ldlm_state->ldlm_cb_service);
1057 		ldlm_state->ldlm_cb_service = NULL;
1058 		goto out;
1059 	}
1060 
1061 
1062 	OBD_ALLOC(blp, sizeof(*blp));
1063 	if (blp == NULL) {
1064 		rc = -ENOMEM;
1065 		goto out;
1066 	}
1067 	ldlm_state->ldlm_bl_pool = blp;
1068 
1069 	spin_lock_init(&blp->blp_lock);
1070 	INIT_LIST_HEAD(&blp->blp_list);
1071 	INIT_LIST_HEAD(&blp->blp_prio_list);
1072 	init_waitqueue_head(&blp->blp_waitq);
1073 	atomic_set(&blp->blp_num_threads, 0);
1074 	atomic_set(&blp->blp_busy_threads, 0);
1075 
1076 	if (ldlm_num_threads == 0) {
1077 		blp->blp_min_threads = LDLM_NTHRS_INIT;
1078 		blp->blp_max_threads = LDLM_NTHRS_MAX;
1079 	} else {
1080 		blp->blp_min_threads = blp->blp_max_threads =
1081 			min_t(int, LDLM_NTHRS_MAX, max_t(int, LDLM_NTHRS_INIT,
1082 							 ldlm_num_threads));
1083 	}
1084 
1085 	for (i = 0; i < blp->blp_min_threads; i++) {
1086 		rc = ldlm_bl_thread_start(blp);
1087 		if (rc < 0)
1088 			goto out;
1089 	}
1090 
1091 
1092 	rc = ldlm_pools_init();
1093 	if (rc) {
1094 		CERROR("Failed to initialize LDLM pools: %d\n", rc);
1095 		goto out;
1096 	}
1097 	return 0;
1098 
1099  out:
1100 	ldlm_cleanup();
1101 	return rc;
1102 }
1103 
ldlm_cleanup(void)1104 static int ldlm_cleanup(void)
1105 {
1106 	if (!list_empty(ldlm_namespace_list(LDLM_NAMESPACE_SERVER)) ||
1107 	    !list_empty(ldlm_namespace_list(LDLM_NAMESPACE_CLIENT))) {
1108 		CERROR("ldlm still has namespaces; clean these up first.\n");
1109 		ldlm_dump_all_namespaces(LDLM_NAMESPACE_SERVER, D_DLMTRACE);
1110 		ldlm_dump_all_namespaces(LDLM_NAMESPACE_CLIENT, D_DLMTRACE);
1111 		return -EBUSY;
1112 	}
1113 
1114 	ldlm_pools_fini();
1115 
1116 	if (ldlm_state->ldlm_bl_pool != NULL) {
1117 		struct ldlm_bl_pool *blp = ldlm_state->ldlm_bl_pool;
1118 
1119 		while (atomic_read(&blp->blp_num_threads) > 0) {
1120 			struct ldlm_bl_work_item blwi = { .blwi_ns = NULL };
1121 
1122 			init_completion(&blp->blp_comp);
1123 
1124 			spin_lock(&blp->blp_lock);
1125 			list_add_tail(&blwi.blwi_entry, &blp->blp_list);
1126 			wake_up(&blp->blp_waitq);
1127 			spin_unlock(&blp->blp_lock);
1128 
1129 			wait_for_completion(&blp->blp_comp);
1130 		}
1131 
1132 		OBD_FREE(blp, sizeof(*blp));
1133 	}
1134 
1135 	if (ldlm_state->ldlm_cb_service != NULL)
1136 		ptlrpc_unregister_service(ldlm_state->ldlm_cb_service);
1137 
1138 	ldlm_proc_cleanup();
1139 
1140 
1141 	OBD_FREE(ldlm_state, sizeof(*ldlm_state));
1142 	ldlm_state = NULL;
1143 
1144 	return 0;
1145 }
1146 
ldlm_init(void)1147 int ldlm_init(void)
1148 {
1149 	mutex_init(&ldlm_ref_mutex);
1150 	mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_SERVER));
1151 	mutex_init(ldlm_namespace_lock(LDLM_NAMESPACE_CLIENT));
1152 	ldlm_resource_slab = kmem_cache_create("ldlm_resources",
1153 					       sizeof(struct ldlm_resource), 0,
1154 					       SLAB_HWCACHE_ALIGN, NULL);
1155 	if (ldlm_resource_slab == NULL)
1156 		return -ENOMEM;
1157 
1158 	ldlm_lock_slab = kmem_cache_create("ldlm_locks",
1159 			      sizeof(struct ldlm_lock), 0,
1160 			      SLAB_HWCACHE_ALIGN | SLAB_DESTROY_BY_RCU, NULL);
1161 	if (ldlm_lock_slab == NULL) {
1162 		kmem_cache_destroy(ldlm_resource_slab);
1163 		return -ENOMEM;
1164 	}
1165 
1166 	ldlm_interval_slab = kmem_cache_create("interval_node",
1167 					sizeof(struct ldlm_interval),
1168 					0, SLAB_HWCACHE_ALIGN, NULL);
1169 	if (ldlm_interval_slab == NULL) {
1170 		kmem_cache_destroy(ldlm_resource_slab);
1171 		kmem_cache_destroy(ldlm_lock_slab);
1172 		return -ENOMEM;
1173 	}
1174 #if LUSTRE_TRACKS_LOCK_EXP_REFS
1175 	class_export_dump_hook = ldlm_dump_export_locks;
1176 #endif
1177 	return 0;
1178 }
1179 
ldlm_exit(void)1180 void ldlm_exit(void)
1181 {
1182 	if (ldlm_refcount)
1183 		CERROR("ldlm_refcount is %d in ldlm_exit!\n", ldlm_refcount);
1184 	kmem_cache_destroy(ldlm_resource_slab);
1185 	/* ldlm_lock_put() use RCU to call ldlm_lock_free, so need call
1186 	 * synchronize_rcu() to wait a grace period elapsed, so that
1187 	 * ldlm_lock_free() get a chance to be called. */
1188 	synchronize_rcu();
1189 	kmem_cache_destroy(ldlm_lock_slab);
1190 	kmem_cache_destroy(ldlm_interval_slab);
1191 }
1192