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