Whamcloud - gitweb
Branch b1_4_mountconf
[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_ost.h> /* for LUSTRE_OSC_NAME */
37 #include <linux/lustre_mds.h> /* for LUSTRE_MDC_NAME */
38 #include <linux/lustre_dlm.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_REQUEST_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 {
211                 CERROR("unknown client OBD type \"%s\", can't setup\n",
212                        name);
213                 RETURN(-EINVAL);
214         }
215
216         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
217                 CERROR("requires a TARGET UUID\n");
218                 RETURN(-EINVAL);
219         }
220
221         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
222                 CERROR("client UUID must be less than 38 characters\n");
223                 RETURN(-EINVAL);
224         }
225
226         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
227                 CERROR("setup requires a SERVER UUID\n");
228                 RETURN(-EINVAL);
229         }
230
231         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
232                 CERROR("target UUID must be less than 38 characters\n");
233                 RETURN(-EINVAL);
234         }
235
236         sema_init(&cli->cl_sem, 1);
237         cli->cl_conn_count = 0;
238         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
239                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
240                      sizeof(server_uuid)));
241
242         cli->cl_dirty = 0;
243         cli->cl_avail_grant = 0;
244         /* FIXME: should limit this for the sum of all cl_dirty_max */
245         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
246         if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
247                 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
248         INIT_LIST_HEAD(&cli->cl_cache_waiters);
249         INIT_LIST_HEAD(&cli->cl_loi_ready_list);
250         INIT_LIST_HEAD(&cli->cl_loi_write_list);
251         INIT_LIST_HEAD(&cli->cl_loi_read_list);
252         spin_lock_init(&cli->cl_loi_list_lock);
253         cli->cl_r_in_flight = 0;
254         cli->cl_w_in_flight = 0;
255         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
256         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
257         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
258         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
259         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
260         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
261         if (num_physpages >> (20 - PAGE_SHIFT) <= 128) { /* <= 128 MB */
262                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 4;
263                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 4;
264         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512) { /* <= 512 MB */
265                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES / 2;
266                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT / 2;
267         } else {
268                 cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
269                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
270         }
271
272         rc = ldlm_get_ref();
273         if (rc) {
274                 CERROR("ldlm_get_ref failed: %d\n", rc);
275                 GOTO(err, rc);
276         }
277
278         ptlrpc_init_client(rq_portal, rp_portal, name,
279                            &obddev->obd_ldlm_client);
280
281         imp = class_new_import();
282         if (imp == NULL)
283                 GOTO(err_ldlm, rc = -ENOENT);
284         imp->imp_client = &obddev->obd_ldlm_client;
285         imp->imp_obd = obddev;
286         imp->imp_connect_op = connect_op;
287         imp->imp_generation = 0;
288         imp->imp_initial_recov = 1;
289         INIT_LIST_HEAD(&imp->imp_pinger_chain);
290         memcpy(imp->imp_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
291                LUSTRE_CFG_BUFLEN(lcfg, 1));
292         class_import_put(imp);
293
294         rc = client_import_add_conn(imp, &server_uuid, 1);
295         if (rc) {
296                 CERROR("can't add initial connection\n");
297                 GOTO(err_import, rc);
298         }
299
300         cli->cl_import = imp;
301         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
302         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
303         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
304         cli->cl_sandev = to_kdev_t(0);
305
306         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
307                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
308                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
309                                name, obddev->obd_name,
310                                imp->imp_target_uuid.uuid);
311                         imp->imp_invalid = 1;
312                 }
313         }
314
315         spin_lock_init(&cli->cl_qchk_lock);
316         cli->cl_qchk_stat = CL_NO_QUOTACHECK;
317
318         RETURN(rc);
319
320 err_import:
321         class_destroy_import(imp);
322 err_ldlm:
323         ldlm_put_ref(0);
324 err:
325         RETURN(rc);
326
327 }
328
329 int client_obd_cleanup(struct obd_device *obddev)
330 {
331         struct client_obd *cli = &obddev->u.cli;
332
333         if (!cli->cl_import)
334                 RETURN(-EINVAL);
335         class_destroy_import(cli->cl_import);
336         cli->cl_import = NULL;
337
338         ldlm_put_ref(obddev->obd_force);
339
340         RETURN(0);
341 }
342
343 /* ->o_connect() method for client side (OSC and MDC) */
344 int client_connect_import(struct lustre_handle *dlm_handle,
345                           struct obd_device *obd, struct obd_uuid *cluuid,
346                           struct obd_connect_data *data)
347 {
348         struct client_obd *cli = &obd->u.cli;
349         struct obd_import *imp = cli->cl_import;
350         struct obd_export *exp;
351         struct obd_connect_data *ocd;
352         int rc;
353         ENTRY;
354
355         down(&cli->cl_sem);
356         rc = class_connect(dlm_handle, obd, cluuid);
357         if (rc)
358                 GOTO(out_sem, rc);
359
360         cli->cl_conn_count++;
361         if (cli->cl_conn_count > 1)
362                 GOTO(out_sem, rc);
363         exp = class_conn2export(dlm_handle);
364
365         if (obd->obd_namespace != NULL)
366                 CERROR("already have namespace!\n");
367         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
368                                                 LDLM_NAMESPACE_CLIENT);
369         if (obd->obd_namespace == NULL)
370                 GOTO(out_disco, rc = -ENOMEM);
371
372         imp->imp_dlm_handle = *dlm_handle;
373         rc = ptlrpc_init_import(imp);
374         if (rc != 0)
375                 GOTO(out_ldlm, rc);
376
377         ocd = &imp->imp_connect_data;
378         if (data)
379                 *ocd = *data;
380
381         rc = ptlrpc_connect_import(imp, NULL);
382         if (rc != 0) {
383                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
384                 GOTO(out_ldlm, rc);
385         }
386         LASSERT(exp->exp_connection);
387
388         if (data) {
389                 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
390                         ocd->ocd_connect_flags);
391                 data->ocd_connect_flags = ocd->ocd_connect_flags;
392         }
393
394         ptlrpc_pinger_add_import(imp);
395         EXIT;
396
397         if (rc) {
398 out_ldlm:
399                 ldlm_namespace_free(obd->obd_namespace, 0);
400                 obd->obd_namespace = NULL;
401 out_disco:
402                 cli->cl_conn_count--;
403                 class_disconnect(exp);
404         } else {
405                 class_export_put(exp);
406         }
407 out_sem:
408         up(&cli->cl_sem);
409         return rc;
410 }
411
412 int client_disconnect_export(struct obd_export *exp)
413 {
414         struct obd_device *obd = class_exp2obd(exp);
415         struct client_obd *cli = &obd->u.cli;
416         struct obd_import *imp = cli->cl_import;
417         int rc = 0, err;
418         ENTRY;
419
420         if (!obd) {
421                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
422                        exp, exp ? exp->exp_handle.h_cookie : -1);
423                 RETURN(-EINVAL);
424         }
425
426         down(&cli->cl_sem);
427         if (!cli->cl_conn_count) {
428                 CERROR("disconnecting disconnected device (%s)\n",
429                        obd->obd_name);
430                 GOTO(out_sem, rc = -EINVAL);
431         }
432
433         cli->cl_conn_count--;
434         if (cli->cl_conn_count)
435                 GOTO(out_no_disconnect, rc = 0);
436
437         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
438          * delete it regardless.  (It's safe to delete an import that was
439          * never added.) */
440         (void)ptlrpc_pinger_del_import(imp);
441
442         if (obd->obd_namespace != NULL) {
443                 /* obd_no_recov == local only */
444                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
445                                        obd->obd_no_recov, NULL);
446                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
447                 obd->obd_namespace = NULL;
448         }
449
450         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
451         if (obd->obd_no_recov)
452                 ptlrpc_invalidate_import(imp);
453         else
454                 rc = ptlrpc_disconnect_import(imp);
455
456         EXIT;
457  out_no_disconnect:
458         err = class_disconnect(exp);
459         if (!rc && err)
460                 rc = err;
461  out_sem:
462         up(&cli->cl_sem);
463         RETURN(rc);
464 }
465
466 /* --------------------------------------------------------------------------
467  * from old lib/target.c
468  * -------------------------------------------------------------------------- */
469
470 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
471                             struct obd_uuid *cluuid)
472 {
473         if (exp->exp_connection) {
474                 struct lustre_handle *hdl;
475                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
476                 /* Might be a re-connect after a partition. */
477                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
478                         CWARN("%s reconnecting\n", cluuid->uuid);
479                         conn->cookie = exp->exp_handle.h_cookie;
480                         /* target_handle_connect() treats EALREADY and
481                          * -EALREADY differently */
482                         RETURN(EALREADY);
483                 } else {
484                         CERROR("%s reconnecting from %s, "
485                                "handle mismatch (ours "LPX64", theirs "
486                                LPX64")\n", cluuid->uuid,
487                                exp->exp_connection->c_remote_uuid.uuid,
488                                hdl->cookie, conn->cookie);
489                         memset(conn, 0, sizeof *conn);
490                         /* target_handle_connect() treats EALREADY and
491                          * -EALREADY differently */
492                         RETURN(-EALREADY);
493                 }
494         }
495
496         conn->cookie = exp->exp_handle.h_cookie;
497         CDEBUG(D_INFO, "existing export for UUID '%s' at %p\n",
498                cluuid->uuid, exp);
499         CDEBUG(D_IOCTL, "connect: cookie "LPX64"\n", conn->cookie);
500         RETURN(0);
501 }
502
503 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
504 {
505         struct obd_device *target;
506         struct obd_export *export = NULL;
507         struct obd_import *revimp;
508         struct lustre_handle conn;
509         struct obd_uuid tgtuuid;
510         struct obd_uuid cluuid;
511         struct obd_uuid remote_uuid;
512         struct list_head *p;
513         char *str, *tmp;
514         int rc = 0, abort_recovery;
515         unsigned long flags;
516         struct obd_connect_data *data;
517         int size = sizeof(*data);
518         ENTRY;
519
520         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
521
522         LASSERT_REQSWAB (req, 0);
523         str = lustre_msg_string(req->rq_reqmsg, 0, sizeof(tgtuuid) - 1);
524         if (str == NULL) {
525                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
526                 GOTO(out, rc = -EINVAL);
527         }
528
529         obd_str2uuid (&tgtuuid, str);
530         target = class_uuid2obd(&tgtuuid);
531         if (!target) {
532                 target = class_name2obd(str);
533         }
534
535         if (!target || target->obd_stopping || !target->obd_set_up) {
536                 DEBUG_REQ(D_ERROR, req, "UUID '%s' is not available "
537                           " for connect (%s)", str,
538                           !target ? "no target" :
539                           (target->obd_stopping ? "stopping" : "not set up"));
540                 GOTO(out, rc = -ENODEV);
541         }
542
543         LASSERT_REQSWAB (req, 1);
544         str = lustre_msg_string(req->rq_reqmsg, 1, sizeof(cluuid) - 1);
545         if (str == NULL) {
546                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
547                 GOTO(out, rc = -EINVAL);
548         }
549
550         obd_str2uuid (&cluuid, str);
551
552         /* XXX extract a nettype and format accordingly */
553         switch (sizeof(lnet_nid_t)) {
554                 /* NB the casts only avoid compiler warnings */
555         case 8:
556                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
557                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
558                 break;
559         case 4:
560                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
561                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
562                 break;
563         default:
564                 LBUG();
565         }
566
567         spin_lock_bh(&target->obd_processing_task_lock);
568         abort_recovery = target->obd_abort_recovery;
569         spin_unlock_bh(&target->obd_processing_task_lock);
570         if (abort_recovery)
571                 target_abort_recovery(target);
572
573         tmp = lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn);
574         if (tmp == NULL)
575                 GOTO(out, rc = -EPROTO);
576
577         memcpy(&conn, tmp, sizeof conn);
578
579         data = lustre_swab_reqbuf(req, 3, sizeof(*data), lustre_swab_connect);
580         rc = lustre_pack_reply(req, 1, &size, NULL);
581         if (rc)
582                 GOTO(out, rc);
583
584         /* lctl gets a backstage, all-access pass. */
585         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
586                 goto dont_check_exports;
587
588         spin_lock(&target->obd_dev_lock);
589         list_for_each(p, &target->obd_exports) {
590                 export = list_entry(p, struct obd_export, exp_obd_chain);
591                 if (obd_uuid_equals(&cluuid, &export->exp_client_uuid)) {
592                         spin_unlock(&target->obd_dev_lock);
593                         LASSERT(export->exp_obd == target);
594
595                         rc = target_handle_reconnect(&conn, export, &cluuid);
596                         break;
597                 }
598                 export = NULL;
599         }
600         /* If we found an export, we already unlocked. */
601         if (!export) {
602                 spin_unlock(&target->obd_dev_lock);
603         } else if (req->rq_reqmsg->conn_cnt == 1) {
604                 CERROR("%s reconnected with 1 conn_cnt; cookies not random?\n",
605                        cluuid.uuid);
606                 GOTO(out, rc = -EALREADY);
607         }
608
609         /* Tell the client if we're in recovery. */
610         /* If this is the first client, start the recovery timer */
611         if (target->obd_recovering) {
612                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
613                 target_start_recovery_timer(target, handler);
614         }
615
616         /* Tell the client if we support replayable requests */
617         if (target->obd_replayable)
618                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
619
620         if (export == NULL) {
621                 if (target->obd_recovering) {
622                         CERROR("%s: denying connection for new client %s: "
623                                "%d clients in recovery for %lds\n",
624                                target->obd_name, cluuid.uuid,
625                                target->obd_recoverable_clients,
626                                (target->obd_recovery_timer.expires-jiffies)/HZ);
627                         rc = -EBUSY;
628                 } else {
629  dont_check_exports:
630                         rc = obd_connect(&conn, target, &cluuid, data);
631                 }
632         }
633
634         /* Return only the parts of obd_connect_data that we understand, so the
635          * client knows that we don't understand the rest. */
636         if (data)
637                 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data)), data,
638                        sizeof(*data));
639
640         /* If all else goes well, this is our RPC return code. */
641         req->rq_status = 0;
642
643         /* we want to handle EALREADY but *not* -EALREADY from
644          * target_handle_reconnect() */
645         if (rc && rc != EALREADY)
646                 GOTO(out, rc);
647
648         req->rq_repmsg->handle = conn;
649
650         /* If the client and the server are the same node, we will already
651          * have an export that really points to the client's DLM export,
652          * because we have a shared handles table.
653          *
654          * XXX this will go away when shaver stops sending the "connect" handle
655          * in the real "remote handle" field of the request --phik 24 Apr 2003
656          */
657         if (req->rq_export != NULL)
658                 class_export_put(req->rq_export);
659
660         /* ownership of this export ref transfers to the request */
661         export = req->rq_export = class_conn2export(&conn);
662         LASSERT(export != NULL);
663
664         spin_lock_irqsave(&export->exp_lock, flags);
665         if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
666                 CERROR("%s: already connected at a higher conn_cnt: %d > %d\n",
667                        cluuid.uuid, export->exp_conn_cnt,
668                        req->rq_reqmsg->conn_cnt);
669                 spin_unlock_irqrestore(&export->exp_lock, flags);
670                 GOTO(out, rc = -EALREADY);
671         }
672         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
673         spin_unlock_irqrestore(&export->exp_lock, flags);
674
675         /* request from liblustre?  Don't evict it for not pinging. */
676         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
677                 export->exp_libclient = 1;
678                 spin_lock(&target->obd_dev_lock);
679                 list_del_init(&export->exp_obd_chain_timed);
680                 spin_unlock(&target->obd_dev_lock);
681         }
682
683         if (export->exp_connection != NULL)
684                 ptlrpc_put_connection(export->exp_connection);
685         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
686                                                        req->rq_self,
687                                                        &remote_uuid);
688         if (rc == EALREADY) {
689                 /* We indicate the reconnection in a flag, not an error code. */
690                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
691                 GOTO(out, rc = 0);
692         }
693
694         if (target->obd_recovering)
695                 target->obd_connected_clients++;
696
697         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
698                sizeof conn);
699
700         if (export->exp_imp_reverse != NULL)
701                 class_destroy_import(export->exp_imp_reverse);
702         revimp = export->exp_imp_reverse = class_new_import();
703         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
704         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
705         revimp->imp_remote_handle = conn;
706         revimp->imp_obd = target;
707         revimp->imp_dlm_fake = 1;
708         revimp->imp_state = LUSTRE_IMP_FULL;
709         class_import_put(revimp);
710 out:
711         if (rc)
712                 req->rq_status = rc;
713         RETURN(rc);
714 }
715
716 int target_handle_disconnect(struct ptlrpc_request *req)
717 {
718         struct obd_export *exp;
719         int rc;
720         ENTRY;
721
722         rc = lustre_pack_reply(req, 0, NULL, NULL);
723         if (rc)
724                 RETURN(rc);
725
726         /* keep the rq_export around so we can send the reply */
727         exp = class_export_get(req->rq_export);
728         req->rq_status = obd_disconnect(exp);
729         RETURN(0);
730 }
731
732 void target_destroy_export(struct obd_export *exp)
733 {
734         /* exports created from last_rcvd data, and "fake"
735            exports created by lctl don't have an import */
736         if (exp->exp_imp_reverse != NULL)
737                 class_destroy_import(exp->exp_imp_reverse);
738
739         /* We cancel locks at disconnect time, but this will catch any locks
740          * granted in a race with recovery-induced disconnect. */
741         if (exp->exp_obd->obd_namespace != NULL)
742                 ldlm_cancel_locks_for_export(exp);
743 }
744
745 /*
746  * Recovery functions
747  */
748
749
750 static void target_release_saved_req(struct ptlrpc_request *req)
751 {
752         if (req->rq_reply_state != NULL) {
753                 ptlrpc_rs_decref(req->rq_reply_state);
754                 /* req->rq_reply_state = NULL; */
755         }
756
757         class_export_put(req->rq_export);
758         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
759         OBD_FREE(req, sizeof *req);
760 }
761
762 static void target_finish_recovery(struct obd_device *obd)
763 {
764         struct list_head *tmp, *n;
765         int rc;
766
767         CWARN("%s: sending delayed replies to recovered clients\n",
768               obd->obd_name);
769
770         ldlm_reprocess_all_ns(obd->obd_namespace);
771
772         /* when recovery finished, cleanup orphans on mds and ost */
773         if (OBT(obd) && OBP(obd, postrecov)) {
774                 rc = OBP(obd, postrecov)(obd);
775                 if (rc >= 0)
776                         CWARN("%s: all clients recovered, %d MDS "
777                               "orphans deleted\n", obd->obd_name, rc);
778                 else
779                         CERROR("postrecov failed %d\n", rc);
780         }
781
782         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
783                 struct ptlrpc_request *req;
784                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
785                 list_del(&req->rq_list);
786                 DEBUG_REQ(D_WARNING, req, "delayed:");
787                 ptlrpc_reply(req);
788                 target_release_saved_req(req);
789         }
790         obd->obd_recovery_end = CURRENT_SECONDS;
791         return;
792 }
793
794 static void abort_recovery_queue(struct obd_device *obd)
795 {
796         struct ptlrpc_request *req;
797         struct list_head *tmp, *n;
798         int rc;
799
800         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
801                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
802                 list_del(&req->rq_list);
803                 DEBUG_REQ(D_ERROR, req, "aborted:");
804                 req->rq_status = -ENOTCONN;
805                 req->rq_type = PTL_RPC_MSG_ERR;
806                 rc = lustre_pack_reply(req, 0, NULL, NULL);
807                 if (rc == 0) {
808                         ptlrpc_reply(req);
809                 } else {
810                         DEBUG_REQ(D_ERROR, req,
811                                   "packing failed for abort-reply; skipping");
812                 }
813                 target_release_saved_req(req);
814         }
815 }
816
817 /* Called from a cleanup function if the device is being cleaned up
818    forcefully.  The exports should all have been disconnected already,
819    the only thing left to do is
820      - clear the recovery flags
821      - cancel the timer
822      - free queued requests and replies, but don't send replies
823    Because the obd_stopping flag is set, no new requests should be received.
824
825 */
826 void target_cleanup_recovery(struct obd_device *obd)
827 {
828         struct list_head *tmp, *n;
829         struct ptlrpc_request *req;
830         ENTRY;
831
832         LASSERT(obd->obd_stopping);
833
834         spin_lock_bh(&obd->obd_processing_task_lock);
835         if (!obd->obd_recovering) {
836                 spin_unlock_bh(&obd->obd_processing_task_lock);
837                 EXIT;
838                 return;
839         }
840         obd->obd_recovering = obd->obd_abort_recovery = 0;
841         target_cancel_recovery_timer(obd);
842         spin_unlock_bh(&obd->obd_processing_task_lock);
843
844         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
845                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
846                 list_del(&req->rq_list);
847                 target_release_saved_req(req);
848         }
849
850         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
851                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
852                 list_del(&req->rq_list);
853                 target_release_saved_req(req);
854         }
855         EXIT;
856 }
857
858 void target_abort_recovery(void *data)
859 {
860         struct obd_device *obd = data;
861
862         spin_lock_bh(&obd->obd_processing_task_lock);
863         if (!obd->obd_recovering) {
864                 spin_unlock_bh(&obd->obd_processing_task_lock);
865                 EXIT;
866                 return;
867         }
868         obd->obd_recovering = obd->obd_abort_recovery = 0;
869         obd->obd_recoverable_clients = 0;
870         target_cancel_recovery_timer(obd);
871         spin_unlock_bh(&obd->obd_processing_task_lock);
872
873         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
874                obd->obd_name);
875         class_disconnect_stale_exports(obd);
876         abort_recovery_queue(obd);
877
878         target_finish_recovery(obd);
879
880         ptlrpc_run_recovery_over_upcall(obd);
881 }
882
883 static void target_recovery_expired(unsigned long castmeharder)
884 {
885         struct obd_device *obd = (struct obd_device *)castmeharder;
886         CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
887         spin_lock_bh(&obd->obd_processing_task_lock);
888         if (obd->obd_recovering)
889                 obd->obd_abort_recovery = 1;
890         wake_up(&obd->obd_next_transno_waitq);
891         spin_unlock_bh(&obd->obd_processing_task_lock);
892 }
893
894
895 /* obd_processing_task_lock should be held */
896 void target_cancel_recovery_timer(struct obd_device *obd)
897 {
898         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
899         del_timer(&obd->obd_recovery_timer);
900 }
901
902 static void reset_recovery_timer(struct obd_device *obd)
903 {
904         spin_lock_bh(&obd->obd_processing_task_lock);
905         if (!obd->obd_recovering) {
906                 spin_unlock_bh(&obd->obd_processing_task_lock);
907                 return;
908         }
909         mod_timer(&obd->obd_recovery_timer, jiffies + OBD_RECOVERY_TIMEOUT);
910         spin_unlock_bh(&obd->obd_processing_task_lock);
911         CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
912                (int)(OBD_RECOVERY_TIMEOUT / HZ));
913         /* Only used for lprocfs_status */
914         obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT/HZ;
915 }
916
917
918 /* Only start it the first time called */
919 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
920 {
921         spin_lock_bh(&obd->obd_processing_task_lock);
922         if (obd->obd_recovery_handler) {
923                 spin_unlock_bh(&obd->obd_processing_task_lock);
924                 return;
925         }
926         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
927               (int)(OBD_RECOVERY_TIMEOUT / HZ));
928         obd->obd_recovery_handler = handler;
929         obd->obd_recovery_timer.function = target_recovery_expired;
930         obd->obd_recovery_timer.data = (unsigned long)obd;
931         spin_unlock_bh(&obd->obd_processing_task_lock);
932
933         reset_recovery_timer(obd);
934 }
935
936 static int check_for_next_transno(struct obd_device *obd)
937 {
938         struct ptlrpc_request *req;
939         int wake_up = 0, connected, completed, queue_len, max;
940         __u64 next_transno, req_transno;
941
942         spin_lock_bh(&obd->obd_processing_task_lock);
943         req = list_entry(obd->obd_recovery_queue.next,
944                          struct ptlrpc_request, rq_list);
945         max = obd->obd_max_recoverable_clients;
946         req_transno = req->rq_reqmsg->transno;
947         connected = obd->obd_connected_clients;
948         completed = max - obd->obd_recoverable_clients;
949         queue_len = obd->obd_requests_queued_for_recovery;
950         next_transno = obd->obd_next_recovery_transno;
951
952         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
953                "req_transno: "LPU64", next_transno: "LPU64"\n",
954                max, connected, completed, queue_len, req_transno, next_transno);
955         if (obd->obd_abort_recovery) {
956                 CDEBUG(D_HA, "waking for aborted recovery\n");
957                 wake_up = 1;
958         } else if (!obd->obd_recovering) {
959                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
960                 wake_up = 1;
961         } else if (req_transno == next_transno) {
962                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
963                 wake_up = 1;
964         } else if (queue_len + completed == max) {
965                 CDEBUG(D_ERROR,
966                        "waking for skipped transno (skip: "LPD64
967                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
968                        next_transno, queue_len, completed, max, req_transno);
969                 obd->obd_next_recovery_transno = req_transno;
970                 wake_up = 1;
971         }
972         spin_unlock_bh(&obd->obd_processing_task_lock);
973         LASSERT(req->rq_reqmsg->transno >= next_transno);
974         return wake_up;
975 }
976
977 static void process_recovery_queue(struct obd_device *obd)
978 {
979         struct ptlrpc_request *req;
980         int abort_recovery = 0;
981         struct l_wait_info lwi = { 0 };
982         ENTRY;
983
984         for (;;) {
985                 spin_lock_bh(&obd->obd_processing_task_lock);
986                 LASSERT(obd->obd_processing_task == current->pid);
987                 req = list_entry(obd->obd_recovery_queue.next,
988                                  struct ptlrpc_request, rq_list);
989
990                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
991                         spin_unlock_bh(&obd->obd_processing_task_lock);
992                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
993                                LPD64")\n",
994                                obd->obd_next_recovery_transno,
995                                req->rq_reqmsg->transno);
996                         l_wait_event(obd->obd_next_transno_waitq,
997                                      check_for_next_transno(obd), &lwi);
998                         spin_lock_bh(&obd->obd_processing_task_lock);
999                         abort_recovery = obd->obd_abort_recovery;
1000                         spin_unlock_bh(&obd->obd_processing_task_lock);
1001                         if (abort_recovery) {
1002                                 target_abort_recovery(obd);
1003                                 return;
1004                         }
1005                         continue;
1006                 }
1007                 list_del_init(&req->rq_list);
1008                 obd->obd_requests_queued_for_recovery--;
1009                 spin_unlock_bh(&obd->obd_processing_task_lock);
1010
1011                 DEBUG_REQ(D_HA, req, "processing: ");
1012                 (void)obd->obd_recovery_handler(req);
1013                 obd->obd_replayed_requests++;
1014                 reset_recovery_timer(obd);
1015                 /* bug 1580: decide how to properly sync() in recovery */
1016                 //mds_fsync_super(mds->mds_sb);
1017                 class_export_put(req->rq_export);
1018                 if (req->rq_reply_state != NULL) {
1019                         ptlrpc_rs_decref(req->rq_reply_state);
1020                         /* req->rq_reply_state = NULL; */
1021                 }
1022                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1023                 OBD_FREE(req, sizeof *req);
1024                 spin_lock_bh(&obd->obd_processing_task_lock);
1025                 obd->obd_next_recovery_transno++;
1026                 if (list_empty(&obd->obd_recovery_queue)) {
1027                         obd->obd_processing_task = 0;
1028                         spin_unlock_bh(&obd->obd_processing_task_lock);
1029                         break;
1030                 }
1031                 spin_unlock_bh(&obd->obd_processing_task_lock);
1032         }
1033         EXIT;
1034 }
1035
1036 int target_queue_recovery_request(struct ptlrpc_request *req,
1037                                   struct obd_device *obd)
1038 {
1039         struct list_head *tmp;
1040         int inserted = 0;
1041         __u64 transno = req->rq_reqmsg->transno;
1042         struct ptlrpc_request *saved_req;
1043         struct lustre_msg *reqmsg;
1044
1045         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1046          * (i.e. buflens etc are in my own byte order), but type-dependent
1047          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1048
1049         if (!transno) {
1050                 INIT_LIST_HEAD(&req->rq_list);
1051                 DEBUG_REQ(D_HA, req, "not queueing");
1052                 return 1;
1053         }
1054
1055         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1056         /* XXX just like the request-dup code in queue_final_reply */
1057         OBD_ALLOC(saved_req, sizeof *saved_req);
1058         if (!saved_req)
1059                 LBUG();
1060         OBD_ALLOC(reqmsg, req->rq_reqlen);
1061         if (!reqmsg)
1062                 LBUG();
1063
1064         spin_lock_bh(&obd->obd_processing_task_lock);
1065
1066         /* If we're processing the queue, we want don't want to queue this
1067          * message.
1068          *
1069          * Also, if this request has a transno less than the one we're waiting
1070          * for, we should process it now.  It could (and currently always will)
1071          * be an open request for a descriptor that was opened some time ago.
1072          *
1073          * Also, a resent, replayed request that has already been
1074          * handled will pass through here and be processed immediately.
1075          */
1076         if (obd->obd_processing_task == current->pid ||
1077             transno < obd->obd_next_recovery_transno) {
1078                 /* Processing the queue right now, don't re-add. */
1079                 LASSERT(list_empty(&req->rq_list));
1080                 spin_unlock_bh(&obd->obd_processing_task_lock);
1081                 OBD_FREE(reqmsg, req->rq_reqlen);
1082                 OBD_FREE(saved_req, sizeof *saved_req);
1083                 return 1;
1084         }
1085
1086         /* A resent, replayed request that is still on the queue; just drop it.
1087            The queued request will handle this. */
1088         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1089             (MSG_RESENT | MSG_REPLAY)) {
1090                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1091                 spin_unlock_bh(&obd->obd_processing_task_lock);
1092                 OBD_FREE(reqmsg, req->rq_reqlen);
1093                 OBD_FREE(saved_req, sizeof *saved_req);
1094                 return 0;
1095         }
1096
1097         memcpy(saved_req, req, sizeof *req);
1098         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1099         req = saved_req;
1100         req->rq_reqmsg = reqmsg;
1101         class_export_get(req->rq_export);
1102         INIT_LIST_HEAD(&req->rq_list);
1103
1104         /* XXX O(n^2) */
1105         list_for_each(tmp, &obd->obd_recovery_queue) {
1106                 struct ptlrpc_request *reqiter =
1107                         list_entry(tmp, struct ptlrpc_request, rq_list);
1108
1109                 if (reqiter->rq_reqmsg->transno > transno) {
1110                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1111                         inserted = 1;
1112                         break;
1113                 }
1114         }
1115
1116         if (!inserted) {
1117                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1118         }
1119
1120         obd->obd_requests_queued_for_recovery++;
1121
1122         if (obd->obd_processing_task != 0) {
1123                 /* Someone else is processing this queue, we'll leave it to
1124                  * them.
1125                  */
1126                 wake_up(&obd->obd_next_transno_waitq);
1127                 spin_unlock_bh(&obd->obd_processing_task_lock);
1128                 return 0;
1129         }
1130
1131         /* Nobody is processing, and we know there's (at least) one to process
1132          * now, so we'll do the honours.
1133          */
1134         obd->obd_processing_task = current->pid;
1135         spin_unlock_bh(&obd->obd_processing_task_lock);
1136
1137         process_recovery_queue(obd);
1138         return 0;
1139 }
1140
1141 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1142 {
1143         return req->rq_export->exp_obd;
1144 }
1145
1146 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1147 {
1148         struct obd_device *obd = target_req2obd(req);
1149         struct ptlrpc_request *saved_req;
1150         struct lustre_msg *reqmsg;
1151         int recovery_done = 0;
1152
1153         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1154
1155         if (rc) {
1156                 /* Just like ptlrpc_error, but without the sending. */
1157                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1158                 LASSERT(rc == 0); /* XXX handle this */
1159                 req->rq_type = PTL_RPC_MSG_ERR;
1160         }
1161
1162         LASSERT (!req->rq_reply_state->rs_difficult);
1163         LASSERT(list_empty(&req->rq_list));
1164         /* XXX a bit like the request-dup code in queue_recovery_request */
1165         OBD_ALLOC(saved_req, sizeof *saved_req);
1166         if (!saved_req)
1167                 LBUG();
1168         OBD_ALLOC(reqmsg, req->rq_reqlen);
1169         if (!reqmsg)
1170                 LBUG();
1171         memcpy(saved_req, req, sizeof *saved_req);
1172         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1173
1174         /* Don't race cleanup */
1175         spin_lock_bh(&obd->obd_processing_task_lock);
1176         if (obd->obd_stopping) {
1177                 spin_unlock_bh(&obd->obd_processing_task_lock);
1178                 OBD_FREE(reqmsg, req->rq_reqlen);
1179                 OBD_FREE(saved_req, sizeof *req);
1180                 req->rq_status = -ENOTCONN;
1181                 /* rv is ignored anyhow */
1182                 return -ENOTCONN;
1183         }
1184         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1185         req = saved_req;
1186         req->rq_reqmsg = reqmsg;
1187         class_export_get(req->rq_export);
1188         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1189
1190         /* only count the first "replay over" request from each
1191            export */
1192         if (req->rq_export->exp_replay_needed) {
1193                 --obd->obd_recoverable_clients;
1194                 req->rq_export->exp_replay_needed = 0;
1195         }
1196         recovery_done = (obd->obd_recoverable_clients == 0);
1197         spin_unlock_bh(&obd->obd_processing_task_lock);
1198
1199         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1200         if (recovery_done) {
1201                 spin_lock_bh(&obd->obd_processing_task_lock);
1202                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1203                 target_cancel_recovery_timer(obd);
1204                 spin_unlock_bh(&obd->obd_processing_task_lock);
1205
1206                 target_finish_recovery(obd);
1207                 ptlrpc_run_recovery_over_upcall(obd);
1208         } else {
1209                 CWARN("%s: %d recoverable clients remain\n",
1210                        obd->obd_name, obd->obd_recoverable_clients);
1211                 wake_up(&obd->obd_next_transno_waitq);
1212         }
1213
1214         return 1;
1215 }
1216
1217 int
1218 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1219 {
1220         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1221                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1222                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1223                 return (-ECOMM);
1224         }
1225
1226         if (rc) {
1227                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1228                 req->rq_status = rc;
1229                 return (ptlrpc_error(req));
1230         } else {
1231                 DEBUG_REQ(D_NET, req, "sending reply");
1232         }
1233
1234         return (ptlrpc_send_reply(req, 1));
1235 }
1236
1237 void
1238 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1239 {
1240         int                        netrc;
1241         unsigned long              flags;
1242         struct ptlrpc_reply_state *rs;
1243         struct obd_device         *obd;
1244         struct obd_export         *exp;
1245         struct ptlrpc_service     *svc;
1246
1247         svc = req->rq_rqbd->rqbd_service;
1248         rs = req->rq_reply_state;
1249         if (rs == NULL || !rs->rs_difficult) {
1250                 /* no notifiers */
1251                 target_send_reply_msg (req, rc, fail_id);
1252                 return;
1253         }
1254
1255         /* must be an export if locks saved */
1256         LASSERT (req->rq_export != NULL);
1257         /* req/reply consistent */
1258         LASSERT (rs->rs_service == svc);
1259
1260         /* "fresh" reply */
1261         LASSERT (!rs->rs_scheduled);
1262         LASSERT (!rs->rs_scheduled_ever);
1263         LASSERT (!rs->rs_handled);
1264         LASSERT (!rs->rs_on_net);
1265         LASSERT (rs->rs_export == NULL);
1266         LASSERT (list_empty(&rs->rs_obd_list));
1267         LASSERT (list_empty(&rs->rs_exp_list));
1268
1269         exp = class_export_get (req->rq_export);
1270         obd = exp->exp_obd;
1271
1272         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1273         rs->rs_scheduled = 1;
1274         rs->rs_on_net    = 1;
1275         rs->rs_xid       = req->rq_xid;
1276         rs->rs_transno   = req->rq_transno;
1277         rs->rs_export    = exp;
1278
1279         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1280
1281         if (rs->rs_transno > obd->obd_last_committed) {
1282                 /* not committed already */
1283                 list_add_tail (&rs->rs_obd_list,
1284                                &obd->obd_uncommitted_replies);
1285         }
1286
1287         spin_unlock (&obd->obd_uncommitted_replies_lock);
1288         spin_lock (&exp->exp_lock);
1289
1290         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1291
1292         spin_unlock_irqrestore (&exp->exp_lock, flags);
1293
1294         netrc = target_send_reply_msg (req, rc, fail_id);
1295
1296         spin_lock_irqsave (&svc->srv_lock, flags);
1297
1298         svc->srv_n_difficult_replies++;
1299
1300         if (netrc != 0) {
1301                 /* error sending: reply is off the net.  Also we need +1
1302                  * reply ref until ptlrpc_server_handle_reply() is done
1303                  * with the reply state (if the send was successful, there
1304                  * would have been +1 ref for the net, which
1305                  * reply_out_callback leaves alone) */
1306                 rs->rs_on_net = 0;
1307                 ptlrpc_rs_addref(rs);
1308                 atomic_inc (&svc->srv_outstanding_replies);
1309         }
1310
1311         if (!rs->rs_on_net ||                   /* some notifier */
1312             list_empty(&rs->rs_exp_list) ||     /* completed already */
1313             list_empty(&rs->rs_obd_list)) {
1314                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1315                 wake_up (&svc->srv_waitq);
1316         } else {
1317                 list_add (&rs->rs_list, &svc->srv_active_replies);
1318                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1319         }
1320
1321         spin_unlock_irqrestore (&svc->srv_lock, flags);
1322 }
1323
1324 int target_handle_ping(struct ptlrpc_request *req)
1325 {
1326         return lustre_pack_reply(req, 0, NULL, NULL);
1327 }
1328
1329 void target_committed_to_req(struct ptlrpc_request *req)
1330 {
1331         struct obd_device *obd = req->rq_export->exp_obd;
1332
1333         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1334                 req->rq_repmsg->last_committed = obd->obd_last_committed;
1335         else
1336                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1337
1338         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1339                obd->obd_last_committed, req->rq_xid);
1340 }
1341
1342 EXPORT_SYMBOL(target_committed_to_req);