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) 2003, 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 
37 /**
38  * This file deals with various client/target related logic including recovery.
39  *
40  * TODO: This code more logically belongs in the ptlrpc module than in ldlm and
41  * should be moved.
42  */
43 
44 #define DEBUG_SUBSYSTEM S_LDLM
45 
46 #include "../../include/linux/libcfs/libcfs.h"
47 #include "../include/obd.h"
48 #include "../include/obd_class.h"
49 #include "../include/lustre_dlm.h"
50 #include "../include/lustre_net.h"
51 #include "../include/lustre_sec.h"
52 #include "ldlm_internal.h"
53 
54 /* @priority: If non-zero, move the selected connection to the list head.
55  * @create: If zero, only search in existing connections.
56  */
import_set_conn(struct obd_import * imp,struct obd_uuid * uuid,int priority,int create)57 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
58 			   int priority, int create)
59 {
60 	struct ptlrpc_connection *ptlrpc_conn;
61 	struct obd_import_conn *imp_conn = NULL, *item;
62 	int rc = 0;
63 
64 	if (!create && !priority) {
65 		CDEBUG(D_HA, "Nothing to do\n");
66 		return -EINVAL;
67 	}
68 
69 	ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
70 	if (!ptlrpc_conn) {
71 		CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
72 		return -ENOENT;
73 	}
74 
75 	if (create) {
76 		OBD_ALLOC(imp_conn, sizeof(*imp_conn));
77 		if (!imp_conn) {
78 			rc = -ENOMEM;
79 			goto out_put;
80 		}
81 	}
82 
83 	spin_lock(&imp->imp_lock);
84 	list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
85 		if (obd_uuid_equals(uuid, &item->oic_uuid)) {
86 			if (priority) {
87 				list_del(&item->oic_item);
88 				list_add(&item->oic_item,
89 					     &imp->imp_conn_list);
90 				item->oic_last_attempt = 0;
91 			}
92 			CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
93 			       imp, imp->imp_obd->obd_name, uuid->uuid,
94 			       (priority ? ", moved to head" : ""));
95 			spin_unlock(&imp->imp_lock);
96 			rc = 0;
97 			goto out_free;
98 		}
99 	}
100 	/* No existing import connection found for \a uuid. */
101 	if (create) {
102 		imp_conn->oic_conn = ptlrpc_conn;
103 		imp_conn->oic_uuid = *uuid;
104 		imp_conn->oic_last_attempt = 0;
105 		if (priority)
106 			list_add(&imp_conn->oic_item, &imp->imp_conn_list);
107 		else
108 			list_add_tail(&imp_conn->oic_item,
109 					  &imp->imp_conn_list);
110 		CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
111 		       imp, imp->imp_obd->obd_name, uuid->uuid,
112 		       (priority ? "head" : "tail"));
113 	} else {
114 		spin_unlock(&imp->imp_lock);
115 		rc = -ENOENT;
116 		goto out_free;
117 	}
118 
119 	spin_unlock(&imp->imp_lock);
120 	return 0;
121 out_free:
122 	if (imp_conn)
123 		OBD_FREE(imp_conn, sizeof(*imp_conn));
124 out_put:
125 	ptlrpc_connection_put(ptlrpc_conn);
126 	return rc;
127 }
128 
import_set_conn_priority(struct obd_import * imp,struct obd_uuid * uuid)129 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
130 {
131 	return import_set_conn(imp, uuid, 1, 0);
132 }
133 
client_import_add_conn(struct obd_import * imp,struct obd_uuid * uuid,int priority)134 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
135 			   int priority)
136 {
137 	return import_set_conn(imp, uuid, priority, 1);
138 }
139 EXPORT_SYMBOL(client_import_add_conn);
140 
client_import_del_conn(struct obd_import * imp,struct obd_uuid * uuid)141 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
142 {
143 	struct obd_import_conn *imp_conn;
144 	struct obd_export *dlmexp;
145 	int rc = -ENOENT;
146 
147 	spin_lock(&imp->imp_lock);
148 	if (list_empty(&imp->imp_conn_list)) {
149 		LASSERT(!imp->imp_connection);
150 		goto out;
151 	}
152 
153 	list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
154 		if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
155 			continue;
156 		LASSERT(imp_conn->oic_conn);
157 
158 		if (imp_conn == imp->imp_conn_current) {
159 			LASSERT(imp_conn->oic_conn == imp->imp_connection);
160 
161 			if (imp->imp_state != LUSTRE_IMP_CLOSED &&
162 			    imp->imp_state != LUSTRE_IMP_DISCON) {
163 				CERROR("can't remove current connection\n");
164 				rc = -EBUSY;
165 				goto out;
166 			}
167 
168 			ptlrpc_connection_put(imp->imp_connection);
169 			imp->imp_connection = NULL;
170 
171 			dlmexp = class_conn2export(&imp->imp_dlm_handle);
172 			if (dlmexp && dlmexp->exp_connection) {
173 				LASSERT(dlmexp->exp_connection ==
174 					imp_conn->oic_conn);
175 				ptlrpc_connection_put(dlmexp->exp_connection);
176 				dlmexp->exp_connection = NULL;
177 			}
178 		}
179 
180 		list_del(&imp_conn->oic_item);
181 		ptlrpc_connection_put(imp_conn->oic_conn);
182 		OBD_FREE(imp_conn, sizeof(*imp_conn));
183 		CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
184 		       imp, imp->imp_obd->obd_name, uuid->uuid);
185 		rc = 0;
186 		break;
187 	}
188 out:
189 	spin_unlock(&imp->imp_lock);
190 	if (rc == -ENOENT)
191 		CERROR("connection %s not found\n", uuid->uuid);
192 	return rc;
193 }
194 EXPORT_SYMBOL(client_import_del_conn);
195 
196 /**
197  * Find conn UUID by peer NID. \a peer is a server NID. This function is used
198  * to find a conn uuid of \a imp which can reach \a peer.
199  */
client_import_find_conn(struct obd_import * imp,lnet_nid_t peer,struct obd_uuid * uuid)200 int client_import_find_conn(struct obd_import *imp, lnet_nid_t peer,
201 			    struct obd_uuid *uuid)
202 {
203 	struct obd_import_conn *conn;
204 	int rc = -ENOENT;
205 
206 	spin_lock(&imp->imp_lock);
207 	list_for_each_entry(conn, &imp->imp_conn_list, oic_item) {
208 		/* Check if conn UUID does have this peer NID. */
209 		if (class_check_uuid(&conn->oic_uuid, peer)) {
210 			*uuid = conn->oic_uuid;
211 			rc = 0;
212 			break;
213 		}
214 	}
215 	spin_unlock(&imp->imp_lock);
216 	return rc;
217 }
218 EXPORT_SYMBOL(client_import_find_conn);
219 
client_destroy_import(struct obd_import * imp)220 void client_destroy_import(struct obd_import *imp)
221 {
222 	/* Drop security policy instance after all RPCs have finished/aborted
223 	 * to let all busy contexts be released. */
224 	class_import_get(imp);
225 	class_destroy_import(imp);
226 	sptlrpc_import_sec_put(imp);
227 	class_import_put(imp);
228 }
229 EXPORT_SYMBOL(client_destroy_import);
230 
231 /**
232  * Check whether or not the OSC is on MDT.
233  * In the config log,
234  * osc on MDT
235  *	setup 0:{fsname}-OSTxxxx-osc[-MDTxxxx] 1:lustre-OST0000_UUID 2:NID
236  * osc on client
237  *	setup 0:{fsname}-OSTxxxx-osc 1:lustre-OST0000_UUID 2:NID
238  *
239  **/
osc_on_mdt(char * obdname)240 static int osc_on_mdt(char *obdname)
241 {
242 	char *ptr;
243 
244 	ptr = strrchr(obdname, '-');
245 	if (ptr == NULL)
246 		return 0;
247 
248 	if (strncmp(ptr + 1, "MDT", 3) == 0)
249 		return 1;
250 
251 	return 0;
252 }
253 
254 /* Configure an RPC client OBD device.
255  *
256  * lcfg parameters:
257  * 1 - client UUID
258  * 2 - server UUID
259  * 3 - inactive-on-startup
260  */
client_obd_setup(struct obd_device * obddev,struct lustre_cfg * lcfg)261 int client_obd_setup(struct obd_device *obddev, struct lustre_cfg *lcfg)
262 {
263 	struct client_obd *cli = &obddev->u.cli;
264 	struct obd_import *imp;
265 	struct obd_uuid server_uuid;
266 	int rq_portal, rp_portal, connect_op;
267 	char *name = obddev->obd_type->typ_name;
268 	ldlm_ns_type_t ns_type = LDLM_NS_TYPE_UNKNOWN;
269 	int rc;
270 
271 	/* In a more perfect world, we would hang a ptlrpc_client off of
272 	 * obd_type and just use the values from there. */
273 	if (!strcmp(name, LUSTRE_OSC_NAME)) {
274 		rq_portal = OST_REQUEST_PORTAL;
275 		rp_portal = OSC_REPLY_PORTAL;
276 		connect_op = OST_CONNECT;
277 		cli->cl_sp_me = LUSTRE_SP_CLI;
278 		cli->cl_sp_to = LUSTRE_SP_OST;
279 		ns_type = LDLM_NS_TYPE_OSC;
280 	} else if (!strcmp(name, LUSTRE_MDC_NAME) ||
281 		   !strcmp(name, LUSTRE_LWP_NAME)) {
282 		rq_portal = MDS_REQUEST_PORTAL;
283 		rp_portal = MDC_REPLY_PORTAL;
284 		connect_op = MDS_CONNECT;
285 		cli->cl_sp_me = LUSTRE_SP_CLI;
286 		cli->cl_sp_to = LUSTRE_SP_MDT;
287 		ns_type = LDLM_NS_TYPE_MDC;
288 	} else if (!strcmp(name, LUSTRE_OSP_NAME)) {
289 		if (strstr(lustre_cfg_buf(lcfg, 1), "OST") == NULL) {
290 			/* OSP_on_MDT for other MDTs */
291 			connect_op = MDS_CONNECT;
292 			cli->cl_sp_to = LUSTRE_SP_MDT;
293 			ns_type = LDLM_NS_TYPE_MDC;
294 			rq_portal = OUT_PORTAL;
295 		} else {
296 			/* OSP on MDT for OST */
297 			connect_op = OST_CONNECT;
298 			cli->cl_sp_to = LUSTRE_SP_OST;
299 			ns_type = LDLM_NS_TYPE_OSC;
300 			rq_portal = OST_REQUEST_PORTAL;
301 		}
302 		rp_portal = OSC_REPLY_PORTAL;
303 		cli->cl_sp_me = LUSTRE_SP_CLI;
304 	} else if (!strcmp(name, LUSTRE_MGC_NAME)) {
305 		rq_portal = MGS_REQUEST_PORTAL;
306 		rp_portal = MGC_REPLY_PORTAL;
307 		connect_op = MGS_CONNECT;
308 		cli->cl_sp_me = LUSTRE_SP_MGC;
309 		cli->cl_sp_to = LUSTRE_SP_MGS;
310 		cli->cl_flvr_mgc.sf_rpc = SPTLRPC_FLVR_INVALID;
311 		ns_type = LDLM_NS_TYPE_MGC;
312 	} else {
313 		CERROR("unknown client OBD type \"%s\", can't setup\n",
314 		       name);
315 		return -EINVAL;
316 	}
317 
318 	if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
319 		CERROR("requires a TARGET UUID\n");
320 		return -EINVAL;
321 	}
322 
323 	if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
324 		CERROR("client UUID must be less than 38 characters\n");
325 		return -EINVAL;
326 	}
327 
328 	if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
329 		CERROR("setup requires a SERVER UUID\n");
330 		return -EINVAL;
331 	}
332 
333 	if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
334 		CERROR("target UUID must be less than 38 characters\n");
335 		return -EINVAL;
336 	}
337 
338 	init_rwsem(&cli->cl_sem);
339 	mutex_init(&cli->cl_mgc_mutex);
340 	cli->cl_conn_count = 0;
341 	memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
342 	       min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
343 		     sizeof(server_uuid)));
344 
345 	cli->cl_dirty = 0;
346 	cli->cl_avail_grant = 0;
347 	/* FIXME: Should limit this for the sum of all cl_dirty_max. */
348 	cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
349 	if (cli->cl_dirty_max >> PAGE_CACHE_SHIFT > totalram_pages / 8)
350 		cli->cl_dirty_max = totalram_pages << (PAGE_CACHE_SHIFT - 3);
351 	INIT_LIST_HEAD(&cli->cl_cache_waiters);
352 	INIT_LIST_HEAD(&cli->cl_loi_ready_list);
353 	INIT_LIST_HEAD(&cli->cl_loi_hp_ready_list);
354 	INIT_LIST_HEAD(&cli->cl_loi_write_list);
355 	INIT_LIST_HEAD(&cli->cl_loi_read_list);
356 	client_obd_list_lock_init(&cli->cl_loi_list_lock);
357 	atomic_set(&cli->cl_pending_w_pages, 0);
358 	atomic_set(&cli->cl_pending_r_pages, 0);
359 	cli->cl_r_in_flight = 0;
360 	cli->cl_w_in_flight = 0;
361 
362 	spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
363 	spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
364 	spin_lock_init(&cli->cl_read_page_hist.oh_lock);
365 	spin_lock_init(&cli->cl_write_page_hist.oh_lock);
366 	spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
367 	spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
368 
369 	/* lru for osc. */
370 	INIT_LIST_HEAD(&cli->cl_lru_osc);
371 	atomic_set(&cli->cl_lru_shrinkers, 0);
372 	atomic_set(&cli->cl_lru_busy, 0);
373 	atomic_set(&cli->cl_lru_in_list, 0);
374 	INIT_LIST_HEAD(&cli->cl_lru_list);
375 	client_obd_list_lock_init(&cli->cl_lru_list_lock);
376 
377 	init_waitqueue_head(&cli->cl_destroy_waitq);
378 	atomic_set(&cli->cl_destroy_in_flight, 0);
379 	/* Turn on checksumming by default. */
380 	cli->cl_checksum = 1;
381 	/*
382 	 * The supported checksum types will be worked out at connect time
383 	 * Set cl_chksum* to CRC32 for now to avoid returning screwed info
384 	 * through procfs.
385 	 */
386 	cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
387 	atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
388 
389 	/* This value may be reduced at connect time in
390 	 * ptlrpc_connect_interpret() . We initialize it to only
391 	 * 1MB until we know what the performance looks like.
392 	 * In the future this should likely be increased. LU-1431 */
393 	cli->cl_max_pages_per_rpc = min_t(int, PTLRPC_MAX_BRW_PAGES,
394 					  LNET_MTU >> PAGE_CACHE_SHIFT);
395 
396 	if (!strcmp(name, LUSTRE_MDC_NAME)) {
397 		cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
398 	} else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 128 /* MB */) {
399 		cli->cl_max_rpcs_in_flight = 2;
400 	} else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 256 /* MB */) {
401 		cli->cl_max_rpcs_in_flight = 3;
402 	} else if (totalram_pages >> (20 - PAGE_CACHE_SHIFT) <= 512 /* MB */) {
403 		cli->cl_max_rpcs_in_flight = 4;
404 	} else {
405 		if (osc_on_mdt(obddev->obd_name))
406 			cli->cl_max_rpcs_in_flight = MDS_OSC_MAX_RIF_DEFAULT;
407 		else
408 			cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
409 	}
410 	rc = ldlm_get_ref();
411 	if (rc) {
412 		CERROR("ldlm_get_ref failed: %d\n", rc);
413 		goto err;
414 	}
415 
416 	ptlrpc_init_client(rq_portal, rp_portal, name,
417 			   &obddev->obd_ldlm_client);
418 
419 	imp = class_new_import(obddev);
420 	if (imp == NULL) {
421 		rc = -ENOENT;
422 		goto err_ldlm;
423 	}
424 	imp->imp_client = &obddev->obd_ldlm_client;
425 	imp->imp_connect_op = connect_op;
426 	memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
427 	       LUSTRE_CFG_BUFLEN(lcfg, 1));
428 	class_import_put(imp);
429 
430 	rc = client_import_add_conn(imp, &server_uuid, 1);
431 	if (rc) {
432 		CERROR("can't add initial connection\n");
433 		goto err_import;
434 	}
435 
436 	cli->cl_import = imp;
437 	/* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
438 	cli->cl_max_mds_easize = sizeof(struct lov_mds_md_v3);
439 	cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
440 
441 	if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
442 		if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
443 			CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
444 			       name, obddev->obd_name,
445 			       cli->cl_target_uuid.uuid);
446 			spin_lock(&imp->imp_lock);
447 			imp->imp_deactive = 1;
448 			spin_unlock(&imp->imp_lock);
449 		}
450 	}
451 
452 	obddev->obd_namespace = ldlm_namespace_new(obddev, obddev->obd_name,
453 						   LDLM_NAMESPACE_CLIENT,
454 						   LDLM_NAMESPACE_GREEDY,
455 						   ns_type);
456 	if (obddev->obd_namespace == NULL) {
457 		CERROR("Unable to create client namespace - %s\n",
458 		       obddev->obd_name);
459 		rc = -ENOMEM;
460 		goto err_import;
461 	}
462 
463 	cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
464 
465 	return rc;
466 
467 err_import:
468 	class_destroy_import(imp);
469 err_ldlm:
470 	ldlm_put_ref();
471 err:
472 	return rc;
473 
474 }
475 EXPORT_SYMBOL(client_obd_setup);
476 
client_obd_cleanup(struct obd_device * obddev)477 int client_obd_cleanup(struct obd_device *obddev)
478 {
479 	ldlm_namespace_free_post(obddev->obd_namespace);
480 	obddev->obd_namespace = NULL;
481 
482 	LASSERT(obddev->u.cli.cl_import == NULL);
483 
484 	ldlm_put_ref();
485 	return 0;
486 }
487 EXPORT_SYMBOL(client_obd_cleanup);
488 
489 /* ->o_connect() method for client side (OSC and MDC and MGC) */
client_connect_import(const struct lu_env * env,struct obd_export ** exp,struct obd_device * obd,struct obd_uuid * cluuid,struct obd_connect_data * data,void * localdata)490 int client_connect_import(const struct lu_env *env,
491 			  struct obd_export **exp,
492 			  struct obd_device *obd, struct obd_uuid *cluuid,
493 			  struct obd_connect_data *data, void *localdata)
494 {
495 	struct client_obd       *cli    = &obd->u.cli;
496 	struct obd_import       *imp    = cli->cl_import;
497 	struct obd_connect_data *ocd;
498 	struct lustre_handle    conn    = { 0 };
499 	int		     rc;
500 
501 	*exp = NULL;
502 	down_write(&cli->cl_sem);
503 	if (cli->cl_conn_count > 0) {
504 		rc = -EALREADY;
505 		goto out_sem;
506 	}
507 
508 	rc = class_connect(&conn, obd, cluuid);
509 	if (rc)
510 		goto out_sem;
511 
512 	cli->cl_conn_count++;
513 	*exp = class_conn2export(&conn);
514 
515 	LASSERT(obd->obd_namespace);
516 
517 	imp->imp_dlm_handle = conn;
518 	rc = ptlrpc_init_import(imp);
519 	if (rc != 0)
520 		goto out_ldlm;
521 
522 	ocd = &imp->imp_connect_data;
523 	if (data) {
524 		*ocd = *data;
525 		imp->imp_connect_flags_orig = data->ocd_connect_flags;
526 	}
527 
528 	rc = ptlrpc_connect_import(imp);
529 	if (rc != 0) {
530 		LASSERT(imp->imp_state == LUSTRE_IMP_DISCON);
531 		goto out_ldlm;
532 	}
533 	LASSERT(*exp != NULL && (*exp)->exp_connection);
534 
535 	if (data) {
536 		LASSERTF((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
537 			 ocd->ocd_connect_flags, "old %#llx, new %#llx\n",
538 			 data->ocd_connect_flags, ocd->ocd_connect_flags);
539 		data->ocd_connect_flags = ocd->ocd_connect_flags;
540 	}
541 
542 	ptlrpc_pinger_add_import(imp);
543 
544 	if (rc) {
545 out_ldlm:
546 		cli->cl_conn_count--;
547 		class_disconnect(*exp);
548 		*exp = NULL;
549 	}
550 out_sem:
551 	up_write(&cli->cl_sem);
552 
553 	return rc;
554 }
555 EXPORT_SYMBOL(client_connect_import);
556 
client_disconnect_export(struct obd_export * exp)557 int client_disconnect_export(struct obd_export *exp)
558 {
559 	struct obd_device *obd = class_exp2obd(exp);
560 	struct client_obd *cli;
561 	struct obd_import *imp;
562 	int rc = 0, err;
563 
564 	if (!obd) {
565 		CERROR("invalid export for disconnect: exp %p cookie %#llx\n",
566 		       exp, exp ? exp->exp_handle.h_cookie : -1);
567 		return -EINVAL;
568 	}
569 
570 	cli = &obd->u.cli;
571 	imp = cli->cl_import;
572 
573 	down_write(&cli->cl_sem);
574 	CDEBUG(D_INFO, "disconnect %s - %d\n", obd->obd_name,
575 	       cli->cl_conn_count);
576 
577 	if (!cli->cl_conn_count) {
578 		CERROR("disconnecting disconnected device (%s)\n",
579 		       obd->obd_name);
580 		rc = -EINVAL;
581 		goto out_disconnect;
582 	}
583 
584 	cli->cl_conn_count--;
585 	if (cli->cl_conn_count) {
586 		rc = 0;
587 		goto out_disconnect;
588 	}
589 
590 	/* Mark import deactivated now, so we don't try to reconnect if any
591 	 * of the cleanup RPCs fails (e.g. LDLM cancel, etc).  We don't
592 	 * fully deactivate the import, or that would drop all requests. */
593 	spin_lock(&imp->imp_lock);
594 	imp->imp_deactive = 1;
595 	spin_unlock(&imp->imp_lock);
596 
597 	/* Some non-replayable imports (MDS's OSCs) are pinged, so just
598 	 * delete it regardless.  (It's safe to delete an import that was
599 	 * never added.) */
600 	(void)ptlrpc_pinger_del_import(imp);
601 
602 	if (obd->obd_namespace != NULL) {
603 		/* obd_force == local only */
604 		ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
605 				       obd->obd_force ? LCF_LOCAL : 0, NULL);
606 		ldlm_namespace_free_prior(obd->obd_namespace, imp,
607 					  obd->obd_force);
608 	}
609 
610 	/* There's no need to hold sem while disconnecting an import,
611 	 * and it may actually cause deadlock in GSS. */
612 	up_write(&cli->cl_sem);
613 	rc = ptlrpc_disconnect_import(imp, 0);
614 	down_write(&cli->cl_sem);
615 
616 	ptlrpc_invalidate_import(imp);
617 
618 out_disconnect:
619 	/* Use server style - class_disconnect should be always called for
620 	 * o_disconnect. */
621 	err = class_disconnect(exp);
622 	if (!rc && err)
623 		rc = err;
624 
625 	up_write(&cli->cl_sem);
626 
627 	return rc;
628 }
629 EXPORT_SYMBOL(client_disconnect_export);
630 
631 
632 /**
633  * Packs current SLV and Limit into \a req.
634  */
target_pack_pool_reply(struct ptlrpc_request * req)635 int target_pack_pool_reply(struct ptlrpc_request *req)
636 {
637 	struct obd_device *obd;
638 
639 	/* Check that we still have all structures alive as this may
640 	 * be some late RPC at shutdown time. */
641 	if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
642 		     !exp_connect_lru_resize(req->rq_export))) {
643 		lustre_msg_set_slv(req->rq_repmsg, 0);
644 		lustre_msg_set_limit(req->rq_repmsg, 0);
645 		return 0;
646 	}
647 
648 	/* OBD is alive here as export is alive, which we checked above. */
649 	obd = req->rq_export->exp_obd;
650 
651 	read_lock(&obd->obd_pool_lock);
652 	lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
653 	lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
654 	read_unlock(&obd->obd_pool_lock);
655 
656 	return 0;
657 }
658 EXPORT_SYMBOL(target_pack_pool_reply);
659 
target_send_reply_msg(struct ptlrpc_request * req,int rc,int fail_id)660 int target_send_reply_msg(struct ptlrpc_request *req, int rc, int fail_id)
661 {
662 	if (OBD_FAIL_CHECK_ORSET(fail_id & ~OBD_FAIL_ONCE, OBD_FAIL_ONCE)) {
663 		DEBUG_REQ(D_ERROR, req, "dropping reply");
664 		return -ECOMM;
665 	}
666 
667 	if (unlikely(rc)) {
668 		DEBUG_REQ(D_NET, req, "processing error (%d)", rc);
669 		req->rq_status = rc;
670 		return ptlrpc_send_error(req, 1);
671 	} else {
672 		DEBUG_REQ(D_NET, req, "sending reply");
673 	}
674 
675 	return ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT);
676 }
677 
target_send_reply(struct ptlrpc_request * req,int rc,int fail_id)678 void target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
679 {
680 	struct ptlrpc_service_part *svcpt;
681 	int			netrc;
682 	struct ptlrpc_reply_state *rs;
683 	struct obd_export	 *exp;
684 
685 	if (req->rq_no_reply)
686 		return;
687 
688 	svcpt = req->rq_rqbd->rqbd_svcpt;
689 	rs = req->rq_reply_state;
690 	if (rs == NULL || !rs->rs_difficult) {
691 		/* no notifiers */
692 		target_send_reply_msg(req, rc, fail_id);
693 		return;
694 	}
695 
696 	/* must be an export if locks saved */
697 	LASSERT(req->rq_export != NULL);
698 	/* req/reply consistent */
699 	LASSERT(rs->rs_svcpt == svcpt);
700 
701 	/* "fresh" reply */
702 	LASSERT(!rs->rs_scheduled);
703 	LASSERT(!rs->rs_scheduled_ever);
704 	LASSERT(!rs->rs_handled);
705 	LASSERT(!rs->rs_on_net);
706 	LASSERT(rs->rs_export == NULL);
707 	LASSERT(list_empty(&rs->rs_obd_list));
708 	LASSERT(list_empty(&rs->rs_exp_list));
709 
710 	exp = class_export_get(req->rq_export);
711 
712 	/* disable reply scheduling while I'm setting up */
713 	rs->rs_scheduled = 1;
714 	rs->rs_on_net    = 1;
715 	rs->rs_xid       = req->rq_xid;
716 	rs->rs_transno   = req->rq_transno;
717 	rs->rs_export    = exp;
718 	rs->rs_opc       = lustre_msg_get_opc(req->rq_reqmsg);
719 
720 	spin_lock(&exp->exp_uncommitted_replies_lock);
721 	CDEBUG(D_NET, "rs transno = %llu, last committed = %llu\n",
722 	       rs->rs_transno, exp->exp_last_committed);
723 	if (rs->rs_transno > exp->exp_last_committed) {
724 		/* not committed already */
725 		list_add_tail(&rs->rs_obd_list,
726 				  &exp->exp_uncommitted_replies);
727 	}
728 	spin_unlock(&exp->exp_uncommitted_replies_lock);
729 
730 	spin_lock(&exp->exp_lock);
731 	list_add_tail(&rs->rs_exp_list, &exp->exp_outstanding_replies);
732 	spin_unlock(&exp->exp_lock);
733 
734 	netrc = target_send_reply_msg(req, rc, fail_id);
735 
736 	spin_lock(&svcpt->scp_rep_lock);
737 
738 	atomic_inc(&svcpt->scp_nreps_difficult);
739 
740 	if (netrc != 0) {
741 		/* error sending: reply is off the net.  Also we need +1
742 		 * reply ref until ptlrpc_handle_rs() is done
743 		 * with the reply state (if the send was successful, there
744 		 * would have been +1 ref for the net, which
745 		 * reply_out_callback leaves alone) */
746 		rs->rs_on_net = 0;
747 		ptlrpc_rs_addref(rs);
748 	}
749 
750 	spin_lock(&rs->rs_lock);
751 	if (rs->rs_transno <= exp->exp_last_committed ||
752 	    (!rs->rs_on_net && !rs->rs_no_ack) ||
753 	    list_empty(&rs->rs_exp_list) ||     /* completed already */
754 	    list_empty(&rs->rs_obd_list)) {
755 		CDEBUG(D_HA, "Schedule reply immediately\n");
756 		ptlrpc_dispatch_difficult_reply(rs);
757 	} else {
758 		list_add(&rs->rs_list, &svcpt->scp_rep_active);
759 		rs->rs_scheduled = 0;	/* allow notifier to schedule */
760 	}
761 	spin_unlock(&rs->rs_lock);
762 	spin_unlock(&svcpt->scp_rep_lock);
763 }
764 EXPORT_SYMBOL(target_send_reply);
765 
766 ldlm_mode_t lck_compat_array[] = {
767 	[LCK_EX]	= LCK_COMPAT_EX,
768 	[LCK_PW]	= LCK_COMPAT_PW,
769 	[LCK_PR]	= LCK_COMPAT_PR,
770 	[LCK_CW]	= LCK_COMPAT_CW,
771 	[LCK_CR]	= LCK_COMPAT_CR,
772 	[LCK_NL]	= LCK_COMPAT_NL,
773 	[LCK_GROUP]	= LCK_COMPAT_GROUP,
774 	[LCK_COS]	= LCK_COMPAT_COS,
775 };
776 
777 /**
778  * Rather arbitrary mapping from LDLM error codes to errno values. This should
779  * not escape to the user level.
780  */
ldlm_error2errno(ldlm_error_t error)781 int ldlm_error2errno(ldlm_error_t error)
782 {
783 	int result;
784 
785 	switch (error) {
786 	case ELDLM_OK:
787 		result = 0;
788 		break;
789 	case ELDLM_LOCK_CHANGED:
790 		result = -ESTALE;
791 		break;
792 	case ELDLM_LOCK_ABORTED:
793 		result = -ENAVAIL;
794 		break;
795 	case ELDLM_LOCK_REPLACED:
796 		result = -ESRCH;
797 		break;
798 	case ELDLM_NO_LOCK_DATA:
799 		result = -ENOENT;
800 		break;
801 	case ELDLM_NAMESPACE_EXISTS:
802 		result = -EEXIST;
803 		break;
804 	case ELDLM_BAD_NAMESPACE:
805 		result = -EBADF;
806 		break;
807 	default:
808 		if (((int)error) < 0)  /* cast to signed type */
809 			result = error; /* as ldlm_error_t can be unsigned */
810 		else {
811 			CERROR("Invalid DLM result code: %d\n", error);
812 			result = -EPROTO;
813 		}
814 	}
815 	return result;
816 }
817 EXPORT_SYMBOL(ldlm_error2errno);
818 
819 /**
820  * Dual to ldlm_error2errno(): maps errno values back to ldlm_error_t.
821  */
ldlm_errno2error(int err_no)822 ldlm_error_t ldlm_errno2error(int err_no)
823 {
824 	int error;
825 
826 	switch (err_no) {
827 	case 0:
828 		error = ELDLM_OK;
829 		break;
830 	case -ESTALE:
831 		error = ELDLM_LOCK_CHANGED;
832 		break;
833 	case -ENAVAIL:
834 		error = ELDLM_LOCK_ABORTED;
835 		break;
836 	case -ESRCH:
837 		error = ELDLM_LOCK_REPLACED;
838 		break;
839 	case -ENOENT:
840 		error = ELDLM_NO_LOCK_DATA;
841 		break;
842 	case -EEXIST:
843 		error = ELDLM_NAMESPACE_EXISTS;
844 		break;
845 	case -EBADF:
846 		error = ELDLM_BAD_NAMESPACE;
847 		break;
848 	default:
849 		error = err_no;
850 	}
851 	return error;
852 }
853 EXPORT_SYMBOL(ldlm_errno2error);
854 
855 #if LUSTRE_TRACKS_LOCK_EXP_REFS
ldlm_dump_export_locks(struct obd_export * exp)856 void ldlm_dump_export_locks(struct obd_export *exp)
857 {
858 	spin_lock(&exp->exp_locks_list_guard);
859 	if (!list_empty(&exp->exp_locks_list)) {
860 		struct ldlm_lock *lock;
861 
862 		CERROR("dumping locks for export %p,ignore if the unmount doesn't hang\n",
863 		       exp);
864 		list_for_each_entry(lock, &exp->exp_locks_list,
865 					l_exp_refs_link)
866 			LDLM_ERROR(lock, "lock:");
867 	}
868 	spin_unlock(&exp->exp_locks_list_guard);
869 }
870 #endif
871