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