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