Whamcloud - gitweb
fab1c40c02a093a99f2947fd1b5c89288da77a61
[fs/lustre-release.git] / lustre / ldlm / ldlm_lib.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (c) 2003 Cluster File Systems, Inc.
5  *
6  *   This file is part of the Lustre file system, http://www.lustre.org
7  *   Lustre is a trademark of Cluster File Systems, Inc.
8  *
9  *   You may have signed or agreed to another license before downloading
10  *   this software.  If so, you are bound by the terms and conditions
11  *   of that agreement, and the following does not apply to you.  See the
12  *   LICENSE file included with this distribution for more information.
13  *
14  *   If you did not agree to a different license, then this copy of Lustre
15  *   is open source software; you can redistribute it and/or modify it
16  *   under the terms of version 2 of the GNU General Public License as
17  *   published by the Free Software Foundation.
18  *
19  *   In either case, Lustre is distributed in the hope that it will be
20  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
21  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  *   license text for more details.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 # define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_LDLM
29
30 #ifdef __KERNEL__
31 # include <linux/module.h>
32 #else
33 # include <liblustre.h>
34 #endif
35 #include <linux/obd.h>
36 #include <linux/obd_class.h>
37 #include <linux/lustre_dlm.h>
38 #include <linux/lustre_ver.h>
39 #include <linux/lustre_net.h>
40
41 /* @priority: if non-zero, move the selected to the list head
42  * @create: if zero, only search in existed connections
43  */
44 static int import_set_conn(struct obd_import *imp, struct obd_uuid *uuid,
45                            int priority, int create)
46 {
47         struct ptlrpc_connection *ptlrpc_conn;
48         struct obd_import_conn *imp_conn = NULL, *item;
49         int rc = 0;
50         ENTRY;
51
52         if (!create && !priority) {
53                 CDEBUG(D_HA, "Nothing to do\n");
54                 RETURN(-EINVAL);
55         }
56
57         ptlrpc_conn = ptlrpc_uuid_to_connection(uuid);
58         if (!ptlrpc_conn) {
59                 CDEBUG(D_HA, "can't find connection %s\n", uuid->uuid);
60                 RETURN (-ENOENT);
61         }
62
63         if (create) {
64                 OBD_ALLOC(imp_conn, sizeof(*imp_conn));
65                 if (!imp_conn) {
66                         GOTO(out_put, rc = -ENOMEM);
67                 }
68         }
69
70         spin_lock(&imp->imp_lock);
71         list_for_each_entry(item, &imp->imp_conn_list, oic_item) {
72                 if (obd_uuid_equals(uuid, &item->oic_uuid)) {
73                         if (priority) {
74                                 list_del(&item->oic_item);
75                                 list_add(&item->oic_item, &imp->imp_conn_list);
76                         }
77                         CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
78                                imp, imp->imp_obd->obd_name, uuid->uuid,
79                                (priority ? ", moved to head" : ""));
80                         spin_unlock(&imp->imp_lock);
81                         GOTO(out_free, rc = 0);
82                 }
83         }
84         /* not found */
85         if (create) {
86                 imp_conn->oic_conn = ptlrpc_conn;
87                 imp_conn->oic_uuid = *uuid;
88                 if (priority)
89                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
90                 else
91                         list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
92                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
93                        imp, imp->imp_obd->obd_name, uuid->uuid,
94                        (priority ? "head" : "tail"));
95         } else {
96                 spin_unlock(&imp->imp_lock);
97                 GOTO(out_free, rc = -ENOENT);
98
99         }
100
101         spin_unlock(&imp->imp_lock);
102         RETURN(0);
103 out_free:
104         if (imp_conn)
105                 OBD_FREE(imp_conn, sizeof(*imp_conn));
106 out_put:
107         ptlrpc_put_connection(ptlrpc_conn);
108         RETURN(rc);
109 }
110
111 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
112 {
113         return import_set_conn(imp, uuid, 1, 0);
114 }
115
116 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
117                            int priority)
118 {
119         return import_set_conn(imp, uuid, priority, 1);
120 }
121
122 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
123 {
124         struct obd_import_conn *imp_conn;
125         struct obd_import_conn *cur_conn;
126         struct obd_export *dlmexp;
127         int rc = -ENOENT;
128         ENTRY;
129
130         spin_lock(&imp->imp_lock);
131         if (list_empty(&imp->imp_conn_list)) {
132                 LASSERT(!imp->imp_connection);
133                 GOTO(out, rc);
134         }
135
136         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
137                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
138                         continue;
139                 LASSERT(imp_conn->oic_conn);
140
141                 cur_conn = list_entry(imp->imp_conn_list.next,
142                                       struct obd_import_conn,
143                                       oic_item);
144
145                 /* is current conn? */
146                 if (imp_conn == cur_conn) {
147                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
148
149                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
150                             imp->imp_state != LUSTRE_IMP_DISCON) {
151                                 CERROR("can't remove current connection\n");
152                                 GOTO(out, rc = -EBUSY);
153                         }
154
155                         ptlrpc_put_connection(imp->imp_connection);
156                         imp->imp_connection = NULL;
157
158                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
159                         if (dlmexp && dlmexp->exp_connection) {
160                                 LASSERT(dlmexp->exp_connection ==
161                                         imp_conn->oic_conn);
162                                 ptlrpc_put_connection(dlmexp->exp_connection);
163                                 dlmexp->exp_connection = NULL;
164                         }
165                 }
166
167                 list_del(&imp_conn->oic_item);
168                 ptlrpc_put_connection(imp_conn->oic_conn);
169                 OBD_FREE(imp_conn, sizeof(*imp_conn));
170                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
171                        imp, imp->imp_obd->obd_name, uuid->uuid);
172                 rc = 0;
173                 break;
174         }
175 out:
176         spin_unlock(&imp->imp_lock);
177         if (rc == -ENOENT)
178                 CERROR("connection %s not found\n", uuid->uuid);
179         RETURN(rc);
180 }
181
182 /* configure an RPC client OBD device
183  *
184  * lcfg parameters:
185  * 1 - client UUID
186  * 2 - server UUID
187  * 3 - inactive-on-startup
188  */
189 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
190 {
191         struct lustre_cfg* lcfg = buf;
192         struct client_obd *cli = &obddev->u.cli;
193         struct obd_import *imp;
194         struct obd_uuid server_uuid;
195         int rq_portal, rp_portal, connect_op;
196         char *name = obddev->obd_type->typ_name;
197         int rc;
198         ENTRY;
199
200         /* In a more perfect world, we would hang a ptlrpc_client off of
201          * obd_type and just use the values from there. */
202         if (!strcmp(name, LUSTRE_OSC_NAME)) {
203                 rq_portal = OST_IO_PORTAL;
204                 rp_portal = OSC_REPLY_PORTAL;
205                 connect_op = OST_CONNECT;
206         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
207                 rq_portal = MDS_REQUEST_PORTAL;
208                 rp_portal = MDC_REPLY_PORTAL;
209                 connect_op = MDS_CONNECT;
210         } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
211                 rq_portal = MGS_REQUEST_PORTAL;
212                 rp_portal = MGC_REPLY_PORTAL;
213                 connect_op = MGS_CONNECT;
214         } else {
215                 CERROR("unknown client OBD type \"%s\", can't setup\n",
216                        name);
217                 RETURN(-EINVAL);
218         }
219
220         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
221                 CERROR("requires a TARGET UUID\n");
222                 RETURN(-EINVAL);
223         }
224
225         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
226                 CERROR("client UUID must be less than 38 characters\n");
227                 RETURN(-EINVAL);
228         }
229
230         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
231                 CERROR("setup requires a SERVER UUID\n");
232                 RETURN(-EINVAL);
233         }
234
235         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
236                 CERROR("target UUID must be less than 38 characters\n");
237                 RETURN(-EINVAL);
238         }
239
240         sema_init(&cli->cl_sem, 1);
241         sema_init(&cli->cl_mgc_sem, 1);
242         cli->cl_conn_count = 0;
243         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
244                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
245                      sizeof(server_uuid)));
246
247         cli->cl_dirty = 0;
248         cli->cl_avail_grant = 0;
249         /* FIXME: should limit this for the sum of all cl_dirty_max */
250         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
251         if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
252                 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
253         INIT_LIST_HEAD(&cli->cl_cache_waiters);
254         INIT_LIST_HEAD(&cli->cl_loi_ready_list);
255         INIT_LIST_HEAD(&cli->cl_loi_write_list);
256         INIT_LIST_HEAD(&cli->cl_loi_read_list);
257         spin_lock_init(&cli->cl_loi_list_lock);
258         cli->cl_r_in_flight = 0;
259         cli->cl_w_in_flight = 0;
260         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
261         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
262         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
263         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
264         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
265         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
266         if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
267                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
268                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
269         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 256) { /* <= 256 MB */
270                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
271                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
272         } else {
273                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
274                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
275         }
276
277         rc = ldlm_get_ref();
278         if (rc) {
279                 CERROR("ldlm_get_ref failed: %d\n", rc);
280                 GOTO(err, rc);
281         }
282
283         ptlrpc_init_client(rq_portal, rp_portal, name,
284                            &obddev->obd_ldlm_client);
285
286         imp = class_new_import();
287         if (imp == NULL)
288                 GOTO(err_ldlm, rc = -ENOENT);
289         imp->imp_client = &obddev->obd_ldlm_client;
290         imp->imp_obd = obddev;
291         imp->imp_connect_op = connect_op;
292         imp->imp_generation = 0;
293         imp->imp_initial_recov = 1;
294         imp->imp_initial_recov_bk = 0;
295         INIT_LIST_HEAD(&imp->imp_pinger_chain);
296         memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
297                LUSTRE_CFG_BUFLEN(lcfg, 1));
298         class_import_put(imp);
299
300         rc = client_import_add_conn(imp, &server_uuid, 1);
301         if (rc) {
302                 CERROR("can't add initial connection\n");
303                 GOTO(err_import, rc);
304         }
305
306         cli->cl_import = imp;
307         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
308         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
309         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
310         cli->cl_sandev = to_kdev_t(0);
311
312         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
313                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
314                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
315                                name, obddev->obd_name,
316                                imp->imp_target_uuid.uuid);
317                         imp->imp_invalid = 1;
318                 }
319         }
320
321         cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
322
323         RETURN(rc);
324
325 err_import:
326         class_destroy_import(imp);
327 err_ldlm:
328         ldlm_put_ref(0);
329 err:
330         RETURN(rc);
331
332 }
333
334 int client_obd_cleanup(struct obd_device *obddev)
335 {
336         struct client_obd *cli = &obddev->u.cli;
337
338         if (!cli->cl_import)
339                 RETURN(-EINVAL);
340         class_destroy_import(cli->cl_import);
341         cli->cl_import = NULL;
342
343         ldlm_put_ref(obddev->obd_force);
344
345         RETURN(0);
346 }
347
348 /* ->o_connect() method for client side (OSC and MDC and MGC) */
349 int client_connect_import(struct lustre_handle *dlm_handle,
350                           struct obd_device *obd, struct obd_uuid *cluuid,
351                           struct obd_connect_data *data)
352 {
353         struct client_obd *cli = &obd->u.cli;
354         struct obd_import *imp = cli->cl_import;
355         struct obd_export *exp;
356         struct obd_connect_data *ocd;
357         int rc;
358         ENTRY;
359
360         down(&cli->cl_sem);
361         rc = class_connect(dlm_handle, obd, cluuid);
362         if (rc)
363                 GOTO(out_sem, rc);
364
365         cli->cl_conn_count++;
366         if (cli->cl_conn_count > 1)
367                 GOTO(out_sem, rc);
368         exp = class_conn2export(dlm_handle);
369
370         if (obd->obd_namespace != NULL)
371                 CERROR("already have namespace!\n");
372         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
373                                                 LDLM_NAMESPACE_CLIENT);
374         if (obd->obd_namespace == NULL)
375                 GOTO(out_disco, rc = -ENOMEM);
376
377         imp->imp_dlm_handle = *dlm_handle;
378         rc = ptlrpc_init_import(imp);
379         if (rc != 0)
380                 GOTO(out_ldlm, rc);
381
382         ocd = &imp->imp_connect_data;
383         if (data) {
384                 *ocd = *data;
385                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
386         }
387
388         rc = ptlrpc_connect_import(imp, NULL);
389         if (rc != 0) {
390                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
391                 GOTO(out_ldlm, rc);
392         }
393         LASSERT(exp->exp_connection);
394
395         if (data) {
396                 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
397                         ocd->ocd_connect_flags);
398                 data->ocd_connect_flags = ocd->ocd_connect_flags;
399         }
400
401         ptlrpc_pinger_add_import(imp);
402         EXIT;
403
404         if (rc) {
405 out_ldlm:
406                 ldlm_namespace_free(obd->obd_namespace, 0);
407                 obd->obd_namespace = NULL;
408 out_disco:
409                 cli->cl_conn_count--;
410                 class_disconnect(exp);
411         } else {
412                 class_export_put(exp);
413         }
414 out_sem:
415         up(&cli->cl_sem);
416         return rc;
417 }
418
419 int client_disconnect_export(struct obd_export *exp)
420 {
421         struct obd_device *obd = class_exp2obd(exp);
422         struct client_obd *cli;
423         struct obd_import *imp;
424         int rc = 0, err;
425         ENTRY;
426
427         if (!obd) {
428                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
429                        exp, exp ? exp->exp_handle.h_cookie : -1);
430                 RETURN(-EINVAL);
431         }
432
433         cli = &obd->u.cli;
434         imp = cli->cl_import;
435
436         down(&cli->cl_sem);
437         if (!cli->cl_conn_count) {
438                 CERROR("disconnecting disconnected device (%s)\n",
439                        obd->obd_name);
440                 GOTO(out_sem, rc = -EINVAL);
441         }
442
443         cli->cl_conn_count--;
444         if (cli->cl_conn_count)
445                 GOTO(out_no_disconnect, rc = 0);
446
447         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
448          * delete it regardless.  (It's safe to delete an import that was
449          * never added.) */
450         (void)ptlrpc_pinger_del_import(imp);
451
452         if (obd->obd_namespace != NULL) {
453                 /* obd_no_recov == local only */
454                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
455                                        obd->obd_no_recov, NULL);
456                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
457                 obd->obd_namespace = NULL;
458         }
459
460         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
461         if (obd->obd_no_recov)
462                 ptlrpc_invalidate_import(imp);
463         else
464                 rc = ptlrpc_disconnect_import(imp);
465
466         EXIT;
467  out_no_disconnect:
468         err = class_disconnect(exp);
469         if (!rc && err)
470                 rc = err;
471  out_sem:
472         up(&cli->cl_sem);
473         RETURN(rc);
474 }
475
476 /* --------------------------------------------------------------------------
477  * from old lib/target.c
478  * -------------------------------------------------------------------------- */
479
480 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
481                             struct obd_uuid *cluuid)
482 {
483         if (exp->exp_connection) {
484                 struct lustre_handle *hdl;
485                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
486                 /* Might be a re-connect after a partition. */
487                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
488                         CWARN("%s reconnecting\n", cluuid->uuid);
489                         conn->cookie = exp->exp_handle.h_cookie;
490                         /* target_handle_connect() treats EALREADY and
491                          * -EALREADY differently.  EALREADY means we are
492                          * doing a valid reconnect from the same client. */
493                         RETURN(EALREADY);
494                 } else {
495                         CERROR("%s reconnecting from %s, "
496                                "handle mismatch (ours "LPX64", theirs "
497                                LPX64")\n", cluuid->uuid,
498                                exp->exp_connection->c_remote_uuid.uuid,
499                                hdl->cookie, conn->cookie);
500                         memset(conn, 0, sizeof *conn);
501                         /* target_handle_connect() treats EALREADY and
502                          * -EALREADY differently.  -EALREADY is an error
503                          * (same UUID, different handle). */
504                         RETURN(-EALREADY);
505                 }
506         }
507
508         conn->cookie = exp->exp_handle.h_cookie;
509         CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
510                cluuid->uuid, exp, conn->cookie);
511         RETURN(0);
512 }
513
514 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
515 {
516         struct obd_device *target;
517         struct obd_export *export = NULL;
518         struct obd_import *revimp;
519         struct lustre_handle conn;
520         struct obd_uuid tgtuuid;
521         struct obd_uuid cluuid;
522         struct obd_uuid remote_uuid;
523         struct list_head *p;
524         char *str, *tmp;
525         int rc = 0, abort_recovery;
526         unsigned long flags;
527         struct obd_connect_data *data;
528         int size = sizeof(*data);
529         ENTRY;
530
531         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
532
533         LASSERT_REQSWAB (req, 0);
534         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
535         if (str == NULL) {
536                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
537                 GOTO(out, rc = -EINVAL);
538         }
539
540         obd_str2uuid (&tgtuuid, str);
541         target = class_uuid2obd(&tgtuuid);
542         /* COMPAT_146 */
543         /* old (pre 1.6) lustre_process_log tries to connect to mdsname
544            (eg. mdsA) instead of uuid. */
545         if (!target) {
546                 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
547                 target = class_uuid2obd(&tgtuuid);
548         }
549         if (!target)
550                 target = class_name2obd(str);
551         /* end COMPAT_146 */
552
553         if (!target || target->obd_stopping || !target->obd_set_up) {
554                 DEBUG_REQ(D_ERROR, req, "UUID '%s' is not available "
555                           " for connect (%s)", str,
556                           !target ? "no target" :
557                           (target->obd_stopping ? "stopping" : "not set up"));
558                 GOTO(out, rc = -ENODEV);
559         }
560
561         LASSERT_REQSWAB (req, 1);
562         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
563         if (str == NULL) {
564                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
565                 GOTO(out, rc = -EINVAL);
566         }
567
568         obd_str2uuid (&cluuid, str);
569
570         /* XXX extract a nettype and format accordingly */
571         switch (sizeof(lnet_nid_t)) {
572                 /* NB the casts only avoid compiler warnings */
573         case 8:
574                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
575                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
576                 break;
577         case 4:
578                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
579                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
580                 break;
581         default:
582                 LBUG();
583         }
584
585         spin_lock_bh(&target->obd_processing_task_lock);
586         abort_recovery = target->obd_abort_recovery;
587         spin_unlock_bh(&target->obd_processing_task_lock);
588         if (abort_recovery)
589                 target_abort_recovery(target);
590
591         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
592         if (tmp == NULL)
593                 GOTO(out, rc = -EPROTO);
594
595         memcpy(&conn, tmp, sizeof conn);
596
597         data = lustre_swab_reqbuf(req, 3, sizeof(*data), lustre_swab_connect);
598         rc = lustre_pack_reply(req, 1, &size, NULL);
599         if (rc)
600                 GOTO(out, rc);
601
602         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
603                 if (!data) {
604                         DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
605                                   "libclient connection attempt\n");
606                         GOTO(out, rc = -EPROTO);
607                 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
608                                                LUSTRE_VERSION_ALLOWED_OFFSET) {
609                         DEBUG_REQ(D_WARNING, req, "Refusing old (%d.%d.%d.%d) "
610                                   "libclient connection attempt\n",
611                                   OBD_OCD_VERSION_MAJOR(data->ocd_version),
612                                   OBD_OCD_VERSION_MINOR(data->ocd_version),
613                                   OBD_OCD_VERSION_PATCH(data->ocd_version),
614                                   OBD_OCD_VERSION_FIX(data->ocd_version));
615                         data = lustre_msg_buf(req->rq_repmsg, 0,
616                                               offsetof(typeof(*data),
617                                                        ocd_version) +
618                                               sizeof(data->ocd_version));
619                         if (data) {
620                                 data->ocd_connect_flags = OBD_CONNECT_VERSION;
621                                 data->ocd_version = LUSTRE_VERSION_CODE;
622                         }
623                         GOTO(out, rc = -EPROTO);
624                 }
625         }
626
627         /* lctl gets a backstage, all-access pass. */
628         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
629                 goto dont_check_exports;
630
631         spin_lock(&target->obd_dev_lock);
632         list_for_each(p, &target->obd_exports) {
633                 export = list_entry(p, struct obd_export, exp_obd_chain);
634                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
635                         spin_unlock(&target->obd_dev_lock);
636                         LASSERT(export->exp_obd == target);
637
638                         rc = target_handle_reconnect(&conn, export, &cluuid);
639                         break;
640                 }
641                 export = NULL;
642         }
643         /* If we found an export, we already unlocked. */
644         if (!export) {
645                 spin_unlock(&target->obd_dev_lock);
646         } else if (req->rq_reqmsg->conn_cnt == 1) {
647                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
648                        "cookies not random?\n", target->obd_name,
649                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
650                 GOTO(out, rc = -EALREADY);
651         }
652
653         /* We indicate the reconnection in a flag, not an error code. */
654         if (rc == EALREADY) {
655                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
656                 rc = 0;
657         }
658
659         /* Tell the client if we're in recovery. */
660         /* If this is the first client, start the recovery timer */
661         if (target->obd_recovering) {
662                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
663                 target_start_recovery_timer(target, handler);
664         }
665
666         /* Tell the client if we support replayable requests */
667         if (target->obd_replayable)
668                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
669
670         if (export == NULL) {
671                 if (target->obd_recovering) {
672                         CERROR("%s: denying connection for new client %s (%s): "
673                                "%d clients in recovery for %lds\n",
674                                target->obd_name,
675                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
676                                target->obd_recoverable_clients,
677                                (target->obd_recovery_timer.expires-jiffies)/HZ);
678                         rc = -EBUSY;
679                 } else {
680  dont_check_exports:
681                         rc = obd_connect(&conn, target, &cluuid, data);
682                 }
683         } else {
684                 rc = obd_reconnect(export, target, &cluuid, data);
685         }
686
687         /* we want to handle EALREADY but *not* -EALREADY from
688          * target_handle_reconnect() */
689         if (rc && rc != EALREADY)
690                 GOTO(out, rc);
691
692         /* Return only the parts of obd_connect_data that we understand, so the
693          * client knows that we don't understand the rest. */
694         if (data)
695                 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data)), data,
696                        sizeof(*data));
697
698         /* If all else goes well, this is our RPC return code. */
699         req->rq_status = 0;
700
701         req->rq_repmsg->handle = conn;
702
703         /* ownership of this export ref transfers to the request AFTER we
704          * drop any previous reference the request had, but we don't want
705          * that to go to zero before we get our new export reference. */
706         export = class_conn2export(&conn);
707         LASSERT(export != NULL);
708
709         /* If the client and the server are the same node, we will already
710          * have an export that really points to the client's DLM export,
711          * because we have a shared handles table.
712          *
713          * XXX this will go away when shaver stops sending the "connect" handle
714          * in the real "remote handle" field of the request --phik 24 Apr 2003
715          */
716         if (req->rq_export != NULL)
717                 class_export_put(req->rq_export);
718
719         req->rq_export = export;
720
721         spin_lock_irqsave(&export->exp_lock, flags);
722         if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
723                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
724                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
725                        export->exp_conn_cnt, req->rq_reqmsg->conn_cnt);
726                 spin_unlock_irqrestore(&export->exp_lock, flags);
727                 GOTO(out, rc = -EALREADY);
728         }
729         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
730         spin_unlock_irqrestore(&export->exp_lock, flags);
731
732         /* request from liblustre?  Don't evict it for not pinging. */
733         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
734                 export->exp_libclient = 1;
735                 spin_lock(&target->obd_dev_lock);
736                 list_del_init(&export->exp_obd_chain_timed);
737                 spin_unlock(&target->obd_dev_lock);
738         }
739
740         if (export->exp_connection != NULL)
741                 ptlrpc_put_connection(export->exp_connection);
742         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
743                                                        req->rq_self,
744                                                        &remote_uuid);
745
746         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT)
747                 GOTO(out, rc = 0);
748
749         if (target->obd_recovering)
750                 target->obd_connected_clients++;
751
752         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
753                sizeof conn);
754
755         if (export->exp_imp_reverse != NULL)
756                 class_destroy_import(export->exp_imp_reverse);
757         revimp = export->exp_imp_reverse = class_new_import();
758         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
759         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
760         revimp->imp_remote_handle = conn;
761         revimp->imp_obd = target;
762         revimp->imp_dlm_fake = 1;
763         revimp->imp_state = LUSTRE_IMP_FULL;
764         class_import_put(revimp);
765 out:
766         if (rc)
767                 req->rq_status = rc;
768         RETURN(rc);
769 }
770
771 int target_handle_disconnect(struct ptlrpc_request *req)
772 {
773         int rc;
774         ENTRY;
775
776         rc = lustre_pack_reply(req, 0, NULL, NULL);
777         if (rc)
778                 RETURN(rc);
779
780         /* keep the rq_export around so we can send the reply */
781         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
782         RETURN(0);
783 }
784
785 void target_destroy_export(struct obd_export *exp)
786 {
787         /* exports created from last_rcvd data, and "fake"
788            exports created by lctl don't have an import */
789         if (exp->exp_imp_reverse != NULL)
790                 class_destroy_import(exp->exp_imp_reverse);
791
792         /* We cancel locks at disconnect time, but this will catch any locks
793          * granted in a race with recovery-induced disconnect. */
794         if (exp->exp_obd->obd_namespace != NULL)
795                 ldlm_cancel_locks_for_export(exp);
796 }
797
798 /*
799  * Recovery functions
800  */
801
802
803 static void target_release_saved_req(struct ptlrpc_request *req)
804 {
805         if (req->rq_reply_state != NULL) {
806                 ptlrpc_rs_decref(req->rq_reply_state);
807                 /* req->rq_reply_state = NULL; */
808         }
809
810         class_export_put(req->rq_export);
811         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
812         OBD_FREE(req, sizeof *req);
813 }
814
815 static void target_finish_recovery(struct obd_device *obd)
816 {
817         struct list_head *tmp, *n;
818
819         CWARN("%s: sending delayed replies to recovered clients\n",
820               obd->obd_name);
821
822         ldlm_reprocess_all_ns(obd->obd_namespace);
823
824         /* when recovery finished, cleanup orphans on mds and ost */
825         if (OBT(obd) && OBP(obd, postrecov)) {
826                 int rc = OBP(obd, postrecov)(obd);
827                 CWARN("%s: recovery %s: rc %d\n", obd->obd_name,
828                       rc < 0 ? "failed" : "complete", rc);
829         }
830
831         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
832                 struct ptlrpc_request *req;
833                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
834                 list_del(&req->rq_list);
835                 DEBUG_REQ(D_WARNING, req, "delayed:");
836                 ptlrpc_reply(req);
837                 target_release_saved_req(req);
838         }
839         obd->obd_recovery_end = CURRENT_SECONDS;
840 }
841
842 static void abort_recovery_queue(struct obd_device *obd)
843 {
844         struct ptlrpc_request *req;
845         struct list_head *tmp, *n;
846         int rc;
847
848         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
849                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
850                 list_del(&req->rq_list);
851                 DEBUG_REQ(D_ERROR, req, "aborted:");
852                 req->rq_status = -ENOTCONN;
853                 req->rq_type = PTL_RPC_MSG_ERR;
854                 rc = lustre_pack_reply(req, 0, NULL, NULL);
855                 if (rc == 0) {
856                         ptlrpc_reply(req);
857                 } else {
858                         DEBUG_REQ(D_ERROR, req,
859                                   "packing failed for abort-reply; skipping");
860                 }
861                 target_release_saved_req(req);
862         }
863 }
864
865 /* Called from a cleanup function if the device is being cleaned up
866    forcefully.  The exports should all have been disconnected already,
867    the only thing left to do is
868      - clear the recovery flags
869      - cancel the timer
870      - free queued requests and replies, but don't send replies
871    Because the obd_stopping flag is set, no new requests should be received.
872
873 */
874 void target_cleanup_recovery(struct obd_device *obd)
875 {
876         struct list_head *tmp, *n;
877         struct ptlrpc_request *req;
878         ENTRY;
879
880         LASSERT(obd->obd_stopping);
881
882         spin_lock_bh(&obd->obd_processing_task_lock);
883         if (!obd->obd_recovering) {
884                 spin_unlock_bh(&obd->obd_processing_task_lock);
885                 EXIT;
886                 return;
887         }
888         obd->obd_recovering = obd->obd_abort_recovery = 0;
889         target_cancel_recovery_timer(obd);
890         spin_unlock_bh(&obd->obd_processing_task_lock);
891
892         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
893                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
894                 list_del(&req->rq_list);
895                 target_release_saved_req(req);
896         }
897
898         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
899                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
900                 list_del(&req->rq_list);
901                 target_release_saved_req(req);
902         }
903         EXIT;
904 }
905
906 void target_abort_recovery(void *data)
907 {
908         struct obd_device *obd = data;
909
910         spin_lock_bh(&obd->obd_processing_task_lock);
911         if (!obd->obd_recovering) {
912                 spin_unlock_bh(&obd->obd_processing_task_lock);
913                 EXIT;
914                 return;
915         }
916         obd->obd_recovering = obd->obd_abort_recovery = 0;
917         obd->obd_recoverable_clients = 0;
918         target_cancel_recovery_timer(obd);
919         spin_unlock_bh(&obd->obd_processing_task_lock);
920
921         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
922                obd->obd_name);
923         class_disconnect_stale_exports(obd);
924         abort_recovery_queue(obd);
925
926         target_finish_recovery(obd);
927
928         ptlrpc_run_recovery_over_upcall(obd);
929 }
930
931 static void target_recovery_expired(unsigned long castmeharder)
932 {
933         struct obd_device *obd = (struct obd_device *)castmeharder;
934         CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
935         spin_lock_bh(&obd->obd_processing_task_lock);
936         if (obd->obd_recovering)
937                 obd->obd_abort_recovery = 1;
938         wake_up(&obd->obd_next_transno_waitq);
939         spin_unlock_bh(&obd->obd_processing_task_lock);
940 }
941
942
943 /* obd_processing_task_lock should be held */
944 void target_cancel_recovery_timer(struct obd_device *obd)
945 {
946         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
947         del_timer(&obd->obd_recovery_timer);
948 }
949
950 static void reset_recovery_timer(struct obd_device *obd)
951 {
952         spin_lock_bh(&obd->obd_processing_task_lock);
953         if (!obd->obd_recovering) {
954                 spin_unlock_bh(&obd->obd_processing_task_lock);
955                 return;
956         }
957         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
958         spin_unlock_bh(&obd->obd_processing_task_lock);
959         CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
960                (int)(OBD_RECOVERY_TIMEOUT / HZ));
961         /* Only used for lprocfs_status */
962         obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT/HZ;
963 }
964
965
966 /* Only start it the first time called */
967 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
968 {
969         spin_lock_bh(&obd->obd_processing_task_lock);
970         if (obd->obd_recovery_handler) {
971                 spin_unlock_bh(&obd->obd_processing_task_lock);
972                 return;
973         }
974         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
975               (int)(OBD_RECOVERY_TIMEOUT / HZ));
976         obd->obd_recovery_handler = handler;
977         obd->obd_recovery_timer.function = target_recovery_expired;
978         obd->obd_recovery_timer.data = (unsigned long)obd;
979         spin_unlock_bh(&obd->obd_processing_task_lock);
980
981         reset_recovery_timer(obd);
982 }
983
984 static int check_for_next_transno(struct obd_device *obd)
985 {
986         struct ptlrpc_request *req;
987         int wake_up = 0, connected, completed, queue_len, max;
988         __u64 next_transno, req_transno;
989
990         spin_lock_bh(&obd->obd_processing_task_lock);
991         req = list_entry(obd->obd_recovery_queue.next,
992                          struct ptlrpc_request, rq_list);
993         max = obd->obd_max_recoverable_clients;
994         req_transno = req->rq_reqmsg->transno;
995         connected = obd->obd_connected_clients;
996         completed = max - obd->obd_recoverable_clients;
997         queue_len = obd->obd_requests_queued_for_recovery;
998         next_transno = obd->obd_next_recovery_transno;
999
1000         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1001                "req_transno: "LPU64", next_transno: "LPU64"\n",
1002                max, connected, completed, queue_len, req_transno, next_transno);
1003         if (obd->obd_abort_recovery) {
1004                 CDEBUG(D_HA, "waking for aborted recovery\n");
1005                 wake_up = 1;
1006         } else if (!obd->obd_recovering) {
1007                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1008                 wake_up = 1;
1009         } else if (req_transno == next_transno) {
1010                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1011                 wake_up = 1;
1012         } else if (queue_len + completed == max) {
1013                 CDEBUG(D_ERROR,
1014                        "waking for skipped transno (skip: "LPD64
1015                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1016                        next_transno, queue_len, completed, max, req_transno);
1017                 obd->obd_next_recovery_transno = req_transno;
1018                 wake_up = 1;
1019         }
1020         spin_unlock_bh(&obd->obd_processing_task_lock);
1021         LASSERT(req->rq_reqmsg->transno >= next_transno);
1022         return wake_up;
1023 }
1024
1025 static void process_recovery_queue(struct obd_device *obd)
1026 {
1027         struct ptlrpc_request *req;
1028         int abort_recovery = 0;
1029         struct l_wait_info lwi = { 0 };
1030         ENTRY;
1031
1032         for (;;) {
1033                 spin_lock_bh(&obd->obd_processing_task_lock);
1034                 LASSERT(obd->obd_processing_task == current->pid);
1035                 req = list_entry(obd->obd_recovery_queue.next,
1036                                  struct ptlrpc_request, rq_list);
1037
1038                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
1039                         spin_unlock_bh(&obd->obd_processing_task_lock);
1040                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1041                                LPD64")\n",
1042                                obd->obd_next_recovery_transno,
1043                                req->rq_reqmsg->transno);
1044                         l_wait_event(obd->obd_next_transno_waitq,
1045                                      check_for_next_transno(obd), &lwi);
1046                         spin_lock_bh(&obd->obd_processing_task_lock);
1047                         abort_recovery = obd->obd_abort_recovery;
1048                         spin_unlock_bh(&obd->obd_processing_task_lock);
1049                         if (abort_recovery) {
1050                                 target_abort_recovery(obd);
1051                                 return;
1052                         }
1053                         continue;
1054                 }
1055                 list_del_init(&req->rq_list);
1056                 obd->obd_requests_queued_for_recovery--;
1057                 spin_unlock_bh(&obd->obd_processing_task_lock);
1058
1059                 DEBUG_REQ(D_HA, req, "processing: ");
1060                 (void)obd->obd_recovery_handler(req);
1061                 obd->obd_replayed_requests++;
1062                 reset_recovery_timer(obd);
1063                 /* bug 1580: decide how to properly sync() in recovery */
1064                 //mds_fsync_super(obd->u.obt.obt_sb);
1065                 class_export_put(req->rq_export);
1066                 if (req->rq_reply_state != NULL) {
1067                         ptlrpc_rs_decref(req->rq_reply_state);
1068                         /* req->rq_reply_state = NULL; */
1069                 }
1070                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1071                 OBD_FREE(req, sizeof *req);
1072                 spin_lock_bh(&obd->obd_processing_task_lock);
1073                 obd->obd_next_recovery_transno++;
1074                 if (list_empty(&obd->obd_recovery_queue)) {
1075                         obd->obd_processing_task = 0;
1076                         spin_unlock_bh(&obd->obd_processing_task_lock);
1077                         break;
1078                 }
1079                 spin_unlock_bh(&obd->obd_processing_task_lock);
1080         }
1081         EXIT;
1082 }
1083
1084 int target_queue_recovery_request(struct ptlrpc_request *req,
1085                                   struct obd_device *obd)
1086 {
1087         struct list_head *tmp;
1088         int inserted = 0;
1089         __u64 transno = req->rq_reqmsg->transno;
1090         struct ptlrpc_request *saved_req;
1091         struct lustre_msg *reqmsg;
1092
1093         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1094          * (i.e. buflens etc are in my own byte order), but type-dependent
1095          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1096
1097         if (!transno) {
1098                 INIT_LIST_HEAD(&req->rq_list);
1099                 DEBUG_REQ(D_HA, req, "not queueing");
1100                 return 1;
1101         }
1102
1103         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1104         /* XXX just like the request-dup code in queue_final_reply */
1105         OBD_ALLOC(saved_req, sizeof *saved_req);
1106         if (!saved_req)
1107                 LBUG();
1108         OBD_ALLOC(reqmsg, req->rq_reqlen);
1109         if (!reqmsg)
1110                 LBUG();
1111
1112         spin_lock_bh(&obd->obd_processing_task_lock);
1113
1114         /* If we're processing the queue, we want don't want to queue this
1115          * message.
1116          *
1117          * Also, if this request has a transno less than the one we're waiting
1118          * for, we should process it now.  It could (and currently always will)
1119          * be an open request for a descriptor that was opened some time ago.
1120          *
1121          * Also, a resent, replayed request that has already been
1122          * handled will pass through here and be processed immediately.
1123          */
1124         if (obd->obd_processing_task == current->pid ||
1125             transno < obd->obd_next_recovery_transno) {
1126                 /* Processing the queue right now, don't re-add. */
1127                 LASSERT(list_empty(&req->rq_list));
1128                 spin_unlock_bh(&obd->obd_processing_task_lock);
1129                 OBD_FREE(reqmsg, req->rq_reqlen);
1130                 OBD_FREE(saved_req, sizeof *saved_req);
1131                 return 1;
1132         }
1133
1134         /* A resent, replayed request that is still on the queue; just drop it.
1135            The queued request will handle this. */
1136         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1137             (MSG_RESENT | MSG_REPLAY)) {
1138                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1139                 spin_unlock_bh(&obd->obd_processing_task_lock);
1140                 OBD_FREE(reqmsg, req->rq_reqlen);
1141                 OBD_FREE(saved_req, sizeof *saved_req);
1142                 return 0;
1143         }
1144
1145         memcpy(saved_req, req, sizeof *req);
1146         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1147         req = saved_req;
1148         req->rq_reqmsg = reqmsg;
1149         class_export_get(req->rq_export);
1150         INIT_LIST_HEAD(&req->rq_list);
1151
1152         /* XXX O(n^2) */
1153         list_for_each(tmp, &obd->obd_recovery_queue) {
1154                 struct ptlrpc_request *reqiter =
1155                         list_entry(tmp, struct ptlrpc_request, rq_list);
1156
1157                 if (reqiter->rq_reqmsg->transno > transno) {
1158                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1159                         inserted = 1;
1160                         break;
1161                 }
1162         }
1163
1164         if (!inserted) {
1165                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1166         }
1167
1168         obd->obd_requests_queued_for_recovery++;
1169
1170         if (obd->obd_processing_task != 0) {
1171                 /* Someone else is processing this queue, we'll leave it to
1172                  * them.
1173                  */
1174                 wake_up(&obd->obd_next_transno_waitq);
1175                 spin_unlock_bh(&obd->obd_processing_task_lock);
1176                 return 0;
1177         }
1178
1179         /* Nobody is processing, and we know there's (at least) one to process
1180          * now, so we'll do the honours.
1181          */
1182         obd->obd_processing_task = current->pid;
1183         spin_unlock_bh(&obd->obd_processing_task_lock);
1184
1185         process_recovery_queue(obd);
1186         return 0;
1187 }
1188
1189 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1190 {
1191         return req->rq_export->exp_obd;
1192 }
1193
1194 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1195 {
1196         struct obd_device *obd = target_req2obd(req);
1197         struct ptlrpc_request *saved_req;
1198         struct lustre_msg *reqmsg;
1199         int recovery_done = 0;
1200
1201         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1202
1203         if (rc) {
1204                 /* Just like ptlrpc_error, but without the sending. */
1205                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1206                 LASSERT(rc == 0); /* XXX handle this */
1207                 req->rq_type = PTL_RPC_MSG_ERR;
1208         }
1209
1210         LASSERT (!req->rq_reply_state->rs_difficult);
1211         LASSERT(list_empty(&req->rq_list));
1212         /* XXX a bit like the request-dup code in queue_recovery_request */
1213         OBD_ALLOC(saved_req, sizeof *saved_req);
1214         if (!saved_req)
1215                 LBUG();
1216         OBD_ALLOC(reqmsg, req->rq_reqlen);
1217         if (!reqmsg)
1218                 LBUG();
1219         *saved_req = *req;
1220         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1221
1222         /* Don't race cleanup */
1223         spin_lock_bh(&obd->obd_processing_task_lock);
1224         if (obd->obd_stopping) {
1225                 spin_unlock_bh(&obd->obd_processing_task_lock);
1226                 OBD_FREE(reqmsg, req->rq_reqlen);
1227                 OBD_FREE(saved_req, sizeof *req);
1228                 req->rq_status = -ENOTCONN;
1229                 /* rv is ignored anyhow */
1230                 return -ENOTCONN;
1231         }
1232         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1233         req = saved_req;
1234         req->rq_reqmsg = reqmsg;
1235         class_export_get(req->rq_export);
1236         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1237
1238         /* only count the first "replay over" request from each
1239            export */
1240         if (req->rq_export->exp_replay_needed) {
1241                 --obd->obd_recoverable_clients;
1242                 req->rq_export->exp_replay_needed = 0;
1243         }
1244         recovery_done = (obd->obd_recoverable_clients == 0);
1245         spin_unlock_bh(&obd->obd_processing_task_lock);
1246
1247         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1248         if (recovery_done) {
1249                 spin_lock_bh(&obd->obd_processing_task_lock);
1250                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1251                 target_cancel_recovery_timer(obd);
1252                 spin_unlock_bh(&obd->obd_processing_task_lock);
1253
1254                 target_finish_recovery(obd);
1255                 ptlrpc_run_recovery_over_upcall(obd);
1256         } else {
1257                 CWARN("%s: %d recoverable clients remain\n",
1258                        obd->obd_name, obd->obd_recoverable_clients);
1259                 wake_up(&obd->obd_next_transno_waitq);
1260         }
1261
1262         return 1;
1263 }
1264
1265 int
1266 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1267 {
1268         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1269                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1270                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1271                 return (-ECOMM);
1272         }
1273
1274         if (rc) {
1275                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1276                 req->rq_status = rc;
1277                 return (ptlrpc_error(req));
1278         } else {
1279                 DEBUG_REQ(D_NET, req, "sending reply");
1280         }
1281
1282         return (ptlrpc_send_reply(req, 1));
1283 }
1284
1285 void
1286 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1287 {
1288         int                        netrc;
1289         unsigned long              flags;
1290         struct ptlrpc_reply_state *rs;
1291         struct obd_device         *obd;
1292         struct obd_export         *exp;
1293         struct ptlrpc_service     *svc;
1294
1295         svc = req->rq_rqbd->rqbd_service;
1296         rs = req->rq_reply_state;
1297         if (rs == NULL || !rs->rs_difficult) {
1298                 /* no notifiers */
1299                 target_send_reply_msg (req, rc, fail_id);
1300                 return;
1301         }
1302
1303         /* must be an export if locks saved */
1304         LASSERT (req->rq_export != NULL);
1305         /* req/reply consistent */
1306         LASSERT (rs->rs_service == svc);
1307
1308         /* "fresh" reply */
1309         LASSERT (!rs->rs_scheduled);
1310         LASSERT (!rs->rs_scheduled_ever);
1311         LASSERT (!rs->rs_handled);
1312         LASSERT (!rs->rs_on_net);
1313         LASSERT (rs->rs_export == NULL);
1314         LASSERT (list_empty(&rs->rs_obd_list));
1315         LASSERT (list_empty(&rs->rs_exp_list));
1316
1317         exp = class_export_get (req->rq_export);
1318         obd = exp->exp_obd;
1319
1320         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1321         rs->rs_scheduled = 1;
1322         rs->rs_on_net    = 1;
1323         rs->rs_xid       = req->rq_xid;
1324         rs->rs_transno   = req->rq_transno;
1325         rs->rs_export    = exp;
1326
1327         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1328
1329         if (rs->rs_transno > obd->obd_last_committed) {
1330                 /* not committed already */
1331                 list_add_tail (&rs->rs_obd_list,
1332                                &obd->obd_uncommitted_replies);
1333         }
1334
1335         spin_unlock (&obd->obd_uncommitted_replies_lock);
1336         spin_lock (&exp->exp_lock);
1337
1338         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1339
1340         spin_unlock_irqrestore (&exp->exp_lock, flags);
1341
1342         netrc = target_send_reply_msg (req, rc, fail_id);
1343
1344         spin_lock_irqsave (&svc->srv_lock, flags);
1345
1346         svc->srv_n_difficult_replies++;
1347
1348         if (netrc != 0) {
1349                 /* error sending: reply is off the net.  Also we need +1
1350                  * reply ref until ptlrpc_server_handle_reply() is done
1351                  * with the reply state (if the send was successful, there
1352                  * would have been +1 ref for the net, which
1353                  * reply_out_callback leaves alone) */
1354                 rs->rs_on_net = 0;
1355                 ptlrpc_rs_addref(rs);
1356                 atomic_inc (&svc->srv_outstanding_replies);
1357         }
1358
1359         if (!rs->rs_on_net ||                   /* some notifier */
1360             list_empty(&rs->rs_exp_list) ||     /* completed already */
1361             list_empty(&rs->rs_obd_list)) {
1362                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1363                 wake_up (&svc->srv_waitq);
1364         } else {
1365                 list_add (&rs->rs_list, &svc->srv_active_replies);
1366                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1367         }
1368
1369         spin_unlock_irqrestore (&svc->srv_lock, flags);
1370 }
1371
1372 int target_handle_ping(struct ptlrpc_request *req)
1373 {
1374         return lustre_pack_reply(req, 0, NULL, NULL);
1375 }
1376
1377 void target_committed_to_req(struct ptlrpc_request *req)
1378 {
1379         struct obd_device *obd = req->rq_export->exp_obd;
1380
1381         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1382                 req->rq_repmsg->last_committed = obd->obd_last_committed;
1383         else
1384                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1385
1386         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1387                obd->obd_last_committed, req->rq_xid);
1388 }
1389
1390 EXPORT_SYMBOL(target_committed_to_req);
1391
1392 #ifdef HAVE_QUOTA_SUPPORT
1393 int target_handle_qc_callback(struct ptlrpc_request *req)
1394 {
1395         struct obd_quotactl *oqctl;
1396         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1397
1398         oqctl = lustre_swab_reqbuf(req, 0, sizeof(*oqctl),
1399                                    lustre_swab_obd_quotactl);
1400
1401         cli->cl_qchk_stat = oqctl->qc_stat;
1402
1403         return 0;
1404 }
1405
1406 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1407 {
1408 #ifdef __KERNEL__
1409         struct obd_device *obd = req->rq_export->exp_obd;
1410         struct obd_device *master_obd;
1411         struct lustre_quota_ctxt *qctxt;
1412         struct qunit_data *qdata, *rep;
1413         int rc = 0, repsize = sizeof(struct qunit_data);
1414         ENTRY;
1415         
1416         rc = lustre_pack_reply(req, 1, &repsize, NULL);
1417         if (rc) {
1418                 CERROR("packing reply failed!: rc = %d\n", rc);
1419                 RETURN(rc);
1420         }
1421         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1422         LASSERT(rep);
1423         
1424         qdata = lustre_swab_reqbuf(req, 0, sizeof(*qdata), lustre_swab_qdata);
1425         if (qdata == NULL) {
1426                 CERROR("unpacking request buffer failed!");
1427                 RETURN(-EPROTO);
1428         }
1429
1430         /* we use the observer */
1431         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1432         master_obd = obd->obd_observer->obd_observer;
1433         qctxt = &master_obd->u.obt.obt_qctxt;
1434         
1435         LASSERT(qctxt->lqc_handler);
1436         rc = qctxt->lqc_handler(master_obd, qdata, req->rq_reqmsg->opc);
1437         if (rc && rc != -EDQUOT)
1438                 CDEBUG(rc == -EBUSY  ? D_QUOTA : D_ERROR, 
1439                        "dqacq failed! (rc:%d)\n", rc);
1440         
1441         /* the qd_count might be changed in lqc_handler */
1442         memcpy(rep, qdata, sizeof(*rep));
1443         req->rq_status = rc;
1444         rc = ptlrpc_reply(req);
1445         
1446         RETURN(rc);     
1447 #else
1448         return 0;
1449 #endif /* !__KERNEL__ */
1450 }
1451 #endif /* HAVE_QUOTA_SUPPORT */
1452
1453 ldlm_mode_t lck_compat_array[] = {
1454         [LCK_EX] LCK_COMPAT_EX,
1455         [LCK_PW] LCK_COMPAT_PW,
1456         [LCK_PR] LCK_COMPAT_PR,
1457         [LCK_CW] LCK_COMPAT_CW,
1458         [LCK_CR] LCK_COMPAT_CR,
1459         [LCK_NL] LCK_COMPAT_NL,
1460         [LCK_GROUP] LCK_COMPAT_GROUP
1461 };