Whamcloud - gitweb
pass lu_context to ->o_connect() method
[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 <libcfs/libcfs.h>
32 #else
33 # include <liblustre.h>
34 #endif
35 #include <obd.h>
36 #include <lustre_mds.h>
37 #include <lustre_dlm.h>
38 #include <lustre_net.h>
39 #include <lustre_ver.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, struct lustre_cfg *lcfg)
190 {
191         struct client_obd *cli = &obddev->u.cli;
192         struct obd_import *imp;
193         struct obd_uuid server_uuid;
194         int rq_portal, rp_portal, connect_op;
195         char *name = obddev->obd_type->typ_name;
196         int rc;
197         ENTRY;
198
199         /* In a more perfect world, we would hang a ptlrpc_client off of
200          * obd_type and just use the values from there. */
201         if (!strcmp(name, LUSTRE_OSC_NAME)) {
202                 rq_portal = OST_IO_PORTAL;
203                 rp_portal = OSC_REPLY_PORTAL;
204                 connect_op = OST_CONNECT;
205         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
206                 rq_portal = MDS_REQUEST_PORTAL;
207                 rp_portal = MDC_REPLY_PORTAL;
208                 connect_op = MDS_CONNECT;
209         } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
210                 rq_portal = MGS_REQUEST_PORTAL;
211                 rp_portal = MGC_REPLY_PORTAL;
212                 connect_op = MGS_CONNECT;
213         } else {
214                 CERROR("unknown client OBD type \"%s\", can't setup\n",
215                        name);
216                 RETURN(-EINVAL);
217         }
218
219         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
220                 CERROR("requires a TARGET UUID\n");
221                 RETURN(-EINVAL);
222         }
223
224         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
225                 CERROR("client UUID must be less than 38 characters\n");
226                 RETURN(-EINVAL);
227         }
228
229         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
230                 CERROR("setup requires a SERVER UUID\n");
231                 RETURN(-EINVAL);
232         }
233
234         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
235                 CERROR("target UUID must be less than 38 characters\n");
236                 RETURN(-EINVAL);
237         }
238
239         sema_init(&cli->cl_sem, 1);
240         sema_init(&cli->cl_mgc_sem, 1);
241         cli->cl_conn_count = 0;
242         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
243                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
244                      sizeof(server_uuid)));
245
246         cli->cl_dirty = 0;
247         cli->cl_avail_grant = 0;
248         /* FIXME: should limit this for the sum of all cl_dirty_max */
249         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
250         if (cli->cl_dirty_max >> PAGE_SHIFT > num_physpages / 8)
251                 cli->cl_dirty_max = num_physpages << (PAGE_SHIFT - 3);
252         CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
253         CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
254         CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
255         CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
256         client_obd_list_lock_init(&cli->cl_loi_list_lock);
257         cli->cl_r_in_flight = 0;
258         cli->cl_w_in_flight = 0;
259
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         cli->cl_max_pages_per_rpc = PTLRPC_MAX_BRW_PAGES;
267         if (num_physpages >> (20 - PAGE_SHIFT) <= 128 /* MB */) {
268                 cli->cl_max_rpcs_in_flight = 2;
269         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 256 /* MB */) {
270                 cli->cl_max_rpcs_in_flight = 3;
271         } else if (num_physpages >> (20 - PAGE_SHIFT) <= 512 /* MB */) {
272                 cli->cl_max_rpcs_in_flight = 4;
273         } else {
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(obddev);
287         if (imp == NULL)
288                 GOTO(err_ldlm, rc = -ENOENT);
289         imp->imp_client = &obddev->obd_ldlm_client;
290         imp->imp_connect_op = connect_op;
291         imp->imp_initial_recov = 1;
292         imp->imp_initial_recov_bk = 0;
293         CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
294         memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
295                LUSTRE_CFG_BUFLEN(lcfg, 1));
296         class_import_put(imp);
297
298         rc = client_import_add_conn(imp, &server_uuid, 1);
299         if (rc) {
300                 CERROR("can't add initial connection\n");
301                 GOTO(err_import, rc);
302         }
303
304         cli->cl_import = imp;
305         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
306         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
307         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
308         cli->cl_sandev = to_kdev_t(0);
309
310         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
311                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
312                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
313                                name, obddev->obd_name,
314                                cli->cl_target_uuid.uuid);
315                         imp->imp_invalid = 1;
316                 }
317         }
318
319         cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
320
321         RETURN(rc);
322
323 err_import:
324         class_destroy_import(imp);
325 err_ldlm:
326         ldlm_put_ref(0);
327 err:
328         RETURN(rc);
329
330 }
331
332 int client_obd_cleanup(struct obd_device *obddev)
333 {
334         ENTRY;
335         ldlm_put_ref(obddev->obd_force);
336
337         RETURN(0);
338 }
339
340 /* ->o_connect() method for client side (OSC and MDC and MGC) */
341 int client_connect_import(const struct lu_context *ctx,
342                           struct lustre_handle *dlm_handle,
343                           struct obd_device *obd, struct obd_uuid *cluuid,
344                           struct obd_connect_data *data)
345 {
346         struct client_obd *cli = &obd->u.cli;
347         struct obd_import *imp = cli->cl_import;
348         struct obd_export *exp;
349         struct obd_connect_data *ocd;
350         int rc;
351         ENTRY;
352
353         mutex_down(&cli->cl_sem);
354         rc = class_connect(dlm_handle, obd, cluuid);
355         if (rc)
356                 GOTO(out_sem, rc);
357
358         cli->cl_conn_count++;
359         if (cli->cl_conn_count > 1)
360                 GOTO(out_sem, rc);
361         exp = class_conn2export(dlm_handle);
362
363         if (obd->obd_namespace != NULL)
364                 CERROR("already have namespace!\n");
365         obd->obd_namespace = ldlm_namespace_new(obd->obd_name,
366                                                 LDLM_NAMESPACE_CLIENT);
367         if (obd->obd_namespace == NULL)
368                 GOTO(out_disco, rc = -ENOMEM);
369
370         imp->imp_dlm_handle = *dlm_handle;
371         rc = ptlrpc_init_import(imp);
372         if (rc != 0)
373                 GOTO(out_ldlm, rc);
374
375         ocd = &imp->imp_connect_data;
376         if (data) {
377                 *ocd = *data;
378                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
379         }
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
396         EXIT;
397
398         if (rc) {
399 out_ldlm:
400                 ldlm_namespace_free(obd->obd_namespace, 0);
401                 obd->obd_namespace = NULL;
402 out_disco:
403                 cli->cl_conn_count--;
404                 class_disconnect(exp);
405         } else {
406                 class_export_put(exp);
407         }
408 out_sem:
409         mutex_up(&cli->cl_sem);
410         return rc;
411 }
412
413 int client_disconnect_export(struct obd_export *exp)
414 {
415         struct obd_device *obd = class_exp2obd(exp);
416         struct client_obd *cli;
417         struct obd_import *imp;
418         int rc = 0, err;
419         ENTRY;
420
421         if (!obd) {
422                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
423                        exp, exp ? exp->exp_handle.h_cookie : -1);
424                 RETURN(-EINVAL);
425         }
426
427         cli = &obd->u.cli;
428         imp = cli->cl_import;
429
430         mutex_down(&cli->cl_sem);
431         if (!cli->cl_conn_count) {
432                 CERROR("disconnecting disconnected device (%s)\n",
433                        obd->obd_name);
434                 GOTO(out_sem, rc = -EINVAL);
435         }
436
437         cli->cl_conn_count--;
438         if (cli->cl_conn_count)
439                 GOTO(out_no_disconnect, rc = 0);
440
441         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
442          * delete it regardless.  (It's safe to delete an import that was
443          * never added.) */
444         (void)ptlrpc_pinger_del_import(imp);
445
446         if (obd->obd_namespace != NULL) {
447                 /* obd_no_recov == local only */
448                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
449                                        obd->obd_no_recov, NULL);
450                 ldlm_namespace_free(obd->obd_namespace, obd->obd_no_recov);
451                 obd->obd_namespace = NULL;
452         }
453
454         /* Yeah, obd_no_recov also (mainly) means "forced shutdown". */
455         if (!obd->obd_no_recov)
456                 rc = ptlrpc_disconnect_import(imp);
457
458         ptlrpc_invalidate_import(imp);
459         imp->imp_deactive = 1;
460         ptlrpc_free_rq_pool(imp->imp_rq_pool);
461         class_destroy_import(imp);
462         cli->cl_import = NULL;
463
464         EXIT;
465  out_no_disconnect:
466         err = class_disconnect(exp);
467         if (!rc && err)
468                 rc = err;
469  out_sem:
470         mutex_up(&cli->cl_sem);
471         RETURN(rc);
472 }
473
474 /* --------------------------------------------------------------------------
475  * from old lib/target.c
476  * -------------------------------------------------------------------------- */
477
478 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
479                             struct obd_uuid *cluuid)
480 {
481         ENTRY;
482         if (exp->exp_connection && exp->exp_imp_reverse) {
483                 struct lustre_handle *hdl;
484                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
485                 /* Might be a re-connect after a partition. */
486                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
487                         CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
488                               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                         if (export->exp_connecting) { /* bug 9635, et. al. */
636                                 CWARN("%s: exp %p already connecting\n",
637                                       export->exp_obd->obd_name, export);
638                                 export = NULL;
639                                 rc = -EALREADY;
640                                 break;
641                         }
642                         export->exp_connecting = 1;
643                         spin_unlock(&target->obd_dev_lock);
644                         LASSERT(export->exp_obd == target);
645
646                         rc = target_handle_reconnect(&conn, export, &cluuid);
647                         break;
648                 }
649                 export = NULL;
650         }
651         /* If we found an export, we already unlocked. */
652         if (!export) {
653                 spin_unlock(&target->obd_dev_lock);
654                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
655         } else if (req->rq_reqmsg->conn_cnt == 1) {
656                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
657                        "cookies not random?\n", target->obd_name,
658                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
659                 GOTO(out, rc = -EALREADY);
660         } else {
661                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
662         }
663
664         /* We want to handle EALREADY but *not* -EALREADY from
665          * target_handle_reconnect(), return reconnection state in a flag */
666         if (rc == EALREADY) {
667                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
668                 rc = 0;
669         } else if (rc) {
670                 GOTO(out, rc);
671         }
672
673         /* Tell the client if we're in recovery. */
674         /* If this is the first client, start the recovery timer */
675         if (target->obd_recovering) {
676                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
677                 target_start_recovery_timer(target, handler);
678         }
679
680         /* Tell the client if we support replayable requests */
681         if (target->obd_replayable)
682                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
683
684         if (export == NULL) {
685                 if (target->obd_recovering) {
686                         CERROR("%s: denying connection for new client %s (%s): "
687                                "%d clients in recovery for %lds\n",
688                                target->obd_name,
689                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
690                                target->obd_recoverable_clients,
691                                cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
692                                                              cfs_time_current())));
693                         rc = -EBUSY;
694                 } else {
695  dont_check_exports:
696                         rc = obd_connect(req->rq_svc_thread->t_ctx,
697                                          &conn, target, &cluuid, data);
698                 }
699         } else {
700                 rc = obd_reconnect(export, target, &cluuid, data);
701         }
702
703         if (rc)
704                 GOTO(out, rc);
705
706         /* Return only the parts of obd_connect_data that we understand, so the
707          * client knows that we don't understand the rest. */
708         if (data)
709                 memcpy(lustre_msg_buf(req->rq_repmsg, 0, sizeof(*data)), data,
710                        sizeof(*data));
711
712         /* If all else goes well, this is our RPC return code. */
713         req->rq_status = 0;
714
715         req->rq_repmsg->handle = conn;
716
717         /* ownership of this export ref transfers to the request AFTER we
718          * drop any previous reference the request had, but we don't want
719          * that to go to zero before we get our new export reference. */
720         export = class_conn2export(&conn);
721         LASSERT(export != NULL);
722
723         /* If the client and the server are the same node, we will already
724          * have an export that really points to the client's DLM export,
725          * because we have a shared handles table.
726          *
727          * XXX this will go away when shaver stops sending the "connect" handle
728          * in the real "remote handle" field of the request --phik 24 Apr 2003
729          */
730         if (req->rq_export != NULL)
731                 class_export_put(req->rq_export);
732
733         req->rq_export = export;
734
735         spin_lock_irqsave(&export->exp_lock, flags);
736         if (export->exp_conn_cnt >= req->rq_reqmsg->conn_cnt) {
737                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
738                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
739                        export->exp_conn_cnt, req->rq_reqmsg->conn_cnt);
740                 spin_unlock_irqrestore(&export->exp_lock, flags);
741                 GOTO(out, rc = -EALREADY);
742         }
743         export->exp_conn_cnt = req->rq_reqmsg->conn_cnt;
744         spin_unlock_irqrestore(&export->exp_lock, flags);
745
746         /* request from liblustre?  Don't evict it for not pinging. */
747         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
748                 export->exp_libclient = 1;
749                 spin_lock(&target->obd_dev_lock);
750                 list_del_init(&export->exp_obd_chain_timed);
751                 spin_unlock(&target->obd_dev_lock);
752         }
753
754         if (export->exp_connection != NULL)
755                 ptlrpc_put_connection(export->exp_connection);
756         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
757                                                        req->rq_self,
758                                                        &remote_uuid);
759
760         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT)
761                 GOTO(out, rc = 0);
762
763         if (target->obd_recovering)
764                 target->obd_connected_clients++;
765
766         memcpy(&conn, lustre_msg_buf(req->rq_reqmsg, 2, sizeof conn),
767                sizeof conn);
768
769         if (export->exp_imp_reverse != NULL)
770                 class_destroy_import(export->exp_imp_reverse);
771         revimp = export->exp_imp_reverse = class_new_import(target);
772         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
773         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
774         revimp->imp_remote_handle = conn;
775         revimp->imp_dlm_fake = 1;
776         revimp->imp_state = LUSTRE_IMP_FULL;
777         class_import_put(revimp);
778 out:
779         if (export)
780                 export->exp_connecting = 0;
781         if (rc)
782                 req->rq_status = rc;
783         RETURN(rc);
784 }
785
786 int target_handle_disconnect(struct ptlrpc_request *req)
787 {
788         int rc;
789         ENTRY;
790
791         rc = lustre_pack_reply(req, 0, NULL, NULL);
792         if (rc)
793                 RETURN(rc);
794
795         /* keep the rq_export around so we can send the reply */
796         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
797         RETURN(0);
798 }
799
800 void target_destroy_export(struct obd_export *exp)
801 {
802         /* exports created from last_rcvd data, and "fake"
803            exports created by lctl don't have an import */
804         if (exp->exp_imp_reverse != NULL)
805                 class_destroy_import(exp->exp_imp_reverse);
806
807         /* We cancel locks at disconnect time, but this will catch any locks
808          * granted in a race with recovery-induced disconnect. */
809         if (exp->exp_obd->obd_namespace != NULL)
810                 ldlm_cancel_locks_for_export(exp);
811 }
812
813 /*
814  * Recovery functions
815  */
816
817
818 static void target_release_saved_req(struct ptlrpc_request *req)
819 {
820         if (req->rq_reply_state != NULL) {
821                 ptlrpc_rs_decref(req->rq_reply_state);
822                 /* req->rq_reply_state = NULL; */
823         }
824
825         class_export_put(req->rq_export);
826         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
827         OBD_FREE(req, sizeof *req);
828 }
829
830 static void target_finish_recovery(struct obd_device *obd)
831 {
832         struct list_head *tmp, *n;
833
834         CWARN("%s: sending delayed replies to recovered clients\n",
835               obd->obd_name);
836
837         ldlm_reprocess_all_ns(obd->obd_namespace);
838
839         /* when recovery finished, cleanup orphans on mds and ost */
840         if (OBT(obd) && OBP(obd, postrecov)) {
841                 int rc = OBP(obd, postrecov)(obd);
842                 CWARN("%s: recovery %s: rc %d\n", obd->obd_name,
843                       rc < 0 ? "failed" : "complete", rc);
844         }
845
846         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
847                 struct ptlrpc_request *req;
848                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
849                 list_del(&req->rq_list);
850                 DEBUG_REQ(D_WARNING, req, "delayed:");
851                 ptlrpc_reply(req);
852                 target_release_saved_req(req);
853         }
854         obd->obd_recovery_end = CURRENT_SECONDS;
855 }
856
857 static void abort_recovery_queue(struct obd_device *obd)
858 {
859         struct ptlrpc_request *req;
860         struct list_head *tmp, *n;
861         int rc;
862
863         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
864                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
865                 list_del(&req->rq_list);
866                 DEBUG_REQ(D_ERROR, req, "aborted:");
867                 req->rq_status = -ENOTCONN;
868                 req->rq_type = PTL_RPC_MSG_ERR;
869                 rc = lustre_pack_reply(req, 0, NULL, NULL);
870                 if (rc == 0) {
871                         ptlrpc_reply(req);
872                 } else {
873                         DEBUG_REQ(D_ERROR, req,
874                                   "packing failed for abort-reply; skipping");
875                 }
876                 target_release_saved_req(req);
877         }
878 }
879
880 /* Called from a cleanup function if the device is being cleaned up
881    forcefully.  The exports should all have been disconnected already,
882    the only thing left to do is
883      - clear the recovery flags
884      - cancel the timer
885      - free queued requests and replies, but don't send replies
886    Because the obd_stopping flag is set, no new requests should be received.
887
888 */
889 void target_cleanup_recovery(struct obd_device *obd)
890 {
891         struct list_head *tmp, *n;
892         struct ptlrpc_request *req;
893         ENTRY;
894
895         LASSERT(obd->obd_stopping);
896
897         spin_lock_bh(&obd->obd_processing_task_lock);
898         if (!obd->obd_recovering) {
899                 spin_unlock_bh(&obd->obd_processing_task_lock);
900                 EXIT;
901                 return;
902         }
903         obd->obd_recovering = obd->obd_abort_recovery = 0;
904         target_cancel_recovery_timer(obd);
905         spin_unlock_bh(&obd->obd_processing_task_lock);
906
907         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
908                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
909                 list_del(&req->rq_list);
910                 target_release_saved_req(req);
911         }
912
913         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
914                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
915                 list_del(&req->rq_list);
916                 target_release_saved_req(req);
917         }
918         EXIT;
919 }
920
921 void target_abort_recovery(void *data)
922 {
923         struct obd_device *obd = data;
924
925         ENTRY;
926         spin_lock_bh(&obd->obd_processing_task_lock);
927         if (!obd->obd_recovering) {
928                 spin_unlock_bh(&obd->obd_processing_task_lock);
929                 EXIT;
930                 return;
931         }
932         obd->obd_recovering = obd->obd_abort_recovery = 0;
933         obd->obd_recoverable_clients = 0;
934         target_cancel_recovery_timer(obd);
935         spin_unlock_bh(&obd->obd_processing_task_lock);
936
937         CERROR("%s: recovery period over; disconnecting unfinished clients.\n",
938                obd->obd_name);
939         class_disconnect_stale_exports(obd);
940         abort_recovery_queue(obd);
941
942         target_finish_recovery(obd);
943
944         ptlrpc_run_recovery_over_upcall(obd);
945         EXIT;
946 }
947
948 static void target_recovery_expired(unsigned long castmeharder)
949 {
950         struct obd_device *obd = (struct obd_device *)castmeharder;
951         CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
952         spin_lock_bh(&obd->obd_processing_task_lock);
953         if (obd->obd_recovering)
954                 obd->obd_abort_recovery = 1;
955         cfs_waitq_signal(&obd->obd_next_transno_waitq);
956         spin_unlock_bh(&obd->obd_processing_task_lock);
957 }
958
959
960 /* obd_processing_task_lock should be held */
961 void target_cancel_recovery_timer(struct obd_device *obd)
962 {
963         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
964         cfs_timer_disarm(&obd->obd_recovery_timer);
965 }
966
967 static void reset_recovery_timer(struct obd_device *obd)
968 {
969         spin_lock_bh(&obd->obd_processing_task_lock);
970         if (!obd->obd_recovering) {
971                 spin_unlock_bh(&obd->obd_processing_task_lock);
972                 return;
973         }
974         cfs_timer_arm(&obd->obd_recovery_timer,
975                       cfs_time_shift(OBD_RECOVERY_TIMEOUT));
976         spin_unlock_bh(&obd->obd_processing_task_lock);
977         CDEBUG(D_HA, "%s: timer will expire in %u seconds\n", obd->obd_name,
978                OBD_RECOVERY_TIMEOUT);
979         /* Only used for lprocfs_status */
980         obd->obd_recovery_end = CURRENT_SECONDS + OBD_RECOVERY_TIMEOUT;
981 }
982
983
984 /* Only start it the first time called */
985 void target_start_recovery_timer(struct obd_device *obd, svc_handler_t handler)
986 {
987         spin_lock_bh(&obd->obd_processing_task_lock);
988         if (obd->obd_recovery_handler) {
989                 spin_unlock_bh(&obd->obd_processing_task_lock);
990                 return;
991         }
992         CWARN("%s: starting recovery timer (%us)\n", obd->obd_name,
993               OBD_RECOVERY_TIMEOUT);
994         obd->obd_recovery_handler = handler;
995         cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
996         spin_unlock_bh(&obd->obd_processing_task_lock);
997
998         reset_recovery_timer(obd);
999 }
1000
1001 static int check_for_next_transno(struct obd_device *obd)
1002 {
1003         struct ptlrpc_request *req;
1004         int wake_up = 0, connected, completed, queue_len, max;
1005         __u64 next_transno, req_transno;
1006
1007         spin_lock_bh(&obd->obd_processing_task_lock);
1008         req = list_entry(obd->obd_recovery_queue.next,
1009                          struct ptlrpc_request, rq_list);
1010         max = obd->obd_max_recoverable_clients;
1011         req_transno = req->rq_reqmsg->transno;
1012         connected = obd->obd_connected_clients;
1013         completed = max - obd->obd_recoverable_clients;
1014         queue_len = obd->obd_requests_queued_for_recovery;
1015         next_transno = obd->obd_next_recovery_transno;
1016
1017         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1018                "req_transno: "LPU64", next_transno: "LPU64"\n",
1019                max, connected, completed, queue_len, req_transno, next_transno);
1020         if (obd->obd_abort_recovery) {
1021                 CDEBUG(D_HA, "waking for aborted recovery\n");
1022                 wake_up = 1;
1023         } else if (!obd->obd_recovering) {
1024                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1025                 wake_up = 1;
1026         } else if (req_transno == next_transno) {
1027                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1028                 wake_up = 1;
1029         } else if (queue_len + completed == max) {
1030                 CDEBUG(D_ERROR,
1031                        "waking for skipped transno (skip: "LPD64
1032                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1033                        next_transno, queue_len, completed, max, req_transno);
1034                 obd->obd_next_recovery_transno = req_transno;
1035                 wake_up = 1;
1036         }
1037         spin_unlock_bh(&obd->obd_processing_task_lock);
1038         LASSERT(req->rq_reqmsg->transno >= next_transno);
1039         return wake_up;
1040 }
1041
1042 static void process_recovery_queue(struct obd_device *obd)
1043 {
1044         struct ptlrpc_request *req;
1045         int abort_recovery = 0;
1046         struct l_wait_info lwi = { 0 };
1047         ENTRY;
1048
1049         for (;;) {
1050                 spin_lock_bh(&obd->obd_processing_task_lock);
1051                 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1052                 req = list_entry(obd->obd_recovery_queue.next,
1053                                  struct ptlrpc_request, rq_list);
1054
1055                 if (req->rq_reqmsg->transno != obd->obd_next_recovery_transno) {
1056                         spin_unlock_bh(&obd->obd_processing_task_lock);
1057                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1058                                LPD64")\n",
1059                                obd->obd_next_recovery_transno,
1060                                req->rq_reqmsg->transno);
1061                         l_wait_event(obd->obd_next_transno_waitq,
1062                                      check_for_next_transno(obd), &lwi);
1063                         spin_lock_bh(&obd->obd_processing_task_lock);
1064                         abort_recovery = obd->obd_abort_recovery;
1065                         spin_unlock_bh(&obd->obd_processing_task_lock);
1066                         if (abort_recovery) {
1067                                 target_abort_recovery(obd);
1068                                 return;
1069                         }
1070                         continue;
1071                 }
1072                 list_del_init(&req->rq_list);
1073                 obd->obd_requests_queued_for_recovery--;
1074                 spin_unlock_bh(&obd->obd_processing_task_lock);
1075
1076                 DEBUG_REQ(D_HA, req, "processing: ");
1077                 (void)obd->obd_recovery_handler(req);
1078                 obd->obd_replayed_requests++;
1079                 reset_recovery_timer(obd);
1080                 /* bug 1580: decide how to properly sync() in recovery */
1081                 //mds_fsync_super(obd->u.obt.obt_sb);
1082                 class_export_put(req->rq_export);
1083                 if (req->rq_reply_state != NULL) {
1084                         ptlrpc_rs_decref(req->rq_reply_state);
1085                         /* req->rq_reply_state = NULL; */
1086                 }
1087                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1088                 OBD_FREE(req, sizeof *req);
1089                 spin_lock_bh(&obd->obd_processing_task_lock);
1090                 obd->obd_next_recovery_transno++;
1091                 if (list_empty(&obd->obd_recovery_queue)) {
1092                         obd->obd_processing_task = 0;
1093                         spin_unlock_bh(&obd->obd_processing_task_lock);
1094                         break;
1095                 }
1096                 spin_unlock_bh(&obd->obd_processing_task_lock);
1097         }
1098         EXIT;
1099 }
1100
1101 int target_queue_recovery_request(struct ptlrpc_request *req,
1102                                   struct obd_device *obd)
1103 {
1104         struct list_head *tmp;
1105         int inserted = 0;
1106         __u64 transno = req->rq_reqmsg->transno;
1107         struct ptlrpc_request *saved_req;
1108         struct lustre_msg *reqmsg;
1109
1110         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1111          * (i.e. buflens etc are in my own byte order), but type-dependent
1112          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1113
1114         if (!transno) {
1115                 CFS_INIT_LIST_HEAD(&req->rq_list);
1116                 DEBUG_REQ(D_HA, req, "not queueing");
1117                 return 1;
1118         }
1119
1120         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1121         /* XXX just like the request-dup code in queue_final_reply */
1122         OBD_ALLOC(saved_req, sizeof *saved_req);
1123         if (!saved_req)
1124                 LBUG();
1125         OBD_ALLOC(reqmsg, req->rq_reqlen);
1126         if (!reqmsg)
1127                 LBUG();
1128
1129         spin_lock_bh(&obd->obd_processing_task_lock);
1130
1131         /* If we're processing the queue, we want don't want to queue this
1132          * message.
1133          *
1134          * Also, if this request has a transno less than the one we're waiting
1135          * for, we should process it now.  It could (and currently always will)
1136          * be an open request for a descriptor that was opened some time ago.
1137          *
1138          * Also, a resent, replayed request that has already been
1139          * handled will pass through here and be processed immediately.
1140          */
1141         if (obd->obd_processing_task == cfs_curproc_pid() ||
1142             transno < obd->obd_next_recovery_transno) {
1143                 /* Processing the queue right now, don't re-add. */
1144                 LASSERT(list_empty(&req->rq_list));
1145                 spin_unlock_bh(&obd->obd_processing_task_lock);
1146                 OBD_FREE(reqmsg, req->rq_reqlen);
1147                 OBD_FREE(saved_req, sizeof *saved_req);
1148                 return 1;
1149         }
1150
1151         /* A resent, replayed request that is still on the queue; just drop it.
1152            The queued request will handle this. */
1153         if ((lustre_msg_get_flags(req->rq_reqmsg) & (MSG_RESENT|MSG_REPLAY)) ==
1154             (MSG_RESENT | MSG_REPLAY)) {
1155                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1156                 spin_unlock_bh(&obd->obd_processing_task_lock);
1157                 OBD_FREE(reqmsg, req->rq_reqlen);
1158                 OBD_FREE(saved_req, sizeof *saved_req);
1159                 return 0;
1160         }
1161
1162         memcpy(saved_req, req, sizeof *req);
1163         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1164         req = saved_req;
1165         req->rq_reqmsg = reqmsg;
1166         class_export_get(req->rq_export);
1167         CFS_INIT_LIST_HEAD(&req->rq_list);
1168
1169         /* XXX O(n^2) */
1170         list_for_each(tmp, &obd->obd_recovery_queue) {
1171                 struct ptlrpc_request *reqiter =
1172                         list_entry(tmp, struct ptlrpc_request, rq_list);
1173
1174                 if (reqiter->rq_reqmsg->transno > transno) {
1175                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1176                         inserted = 1;
1177                         break;
1178                 }
1179         }
1180
1181         if (!inserted) {
1182                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1183         }
1184
1185         obd->obd_requests_queued_for_recovery++;
1186
1187         if (obd->obd_processing_task != 0) {
1188                 /* Someone else is processing this queue, we'll leave it to
1189                  * them.
1190                  */
1191                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1192                 spin_unlock_bh(&obd->obd_processing_task_lock);
1193                 return 0;
1194         }
1195
1196         /* Nobody is processing, and we know there's (at least) one to process
1197          * now, so we'll do the honours.
1198          */
1199         obd->obd_processing_task = cfs_curproc_pid();
1200         spin_unlock_bh(&obd->obd_processing_task_lock);
1201
1202         process_recovery_queue(obd);
1203         return 0;
1204 }
1205
1206 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1207 {
1208         return req->rq_export->exp_obd;
1209 }
1210
1211 int target_queue_final_reply(struct ptlrpc_request *req, int rc)
1212 {
1213         struct obd_device *obd = target_req2obd(req);
1214         struct ptlrpc_request *saved_req;
1215         struct lustre_msg *reqmsg;
1216         int recovery_done = 0;
1217
1218         LASSERT ((rc == 0) == (req->rq_reply_state != NULL));
1219
1220         if (rc) {
1221                 /* Just like ptlrpc_error, but without the sending. */
1222                 rc = lustre_pack_reply(req, 0, NULL, NULL);
1223                 LASSERT(rc == 0); /* XXX handle this */
1224                 req->rq_type = PTL_RPC_MSG_ERR;
1225         }
1226
1227         LASSERT (!req->rq_reply_state->rs_difficult);
1228         LASSERT(list_empty(&req->rq_list));
1229         /* XXX a bit like the request-dup code in queue_recovery_request */
1230         OBD_ALLOC(saved_req, sizeof *saved_req);
1231         if (!saved_req)
1232                 LBUG();
1233         OBD_ALLOC(reqmsg, req->rq_reqlen);
1234         if (!reqmsg)
1235                 LBUG();
1236         *saved_req = *req;
1237         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1238
1239         /* Don't race cleanup */
1240         spin_lock_bh(&obd->obd_processing_task_lock);
1241         if (obd->obd_stopping) {
1242                 spin_unlock_bh(&obd->obd_processing_task_lock);
1243                 OBD_FREE(reqmsg, req->rq_reqlen);
1244                 OBD_FREE(saved_req, sizeof *req);
1245                 req->rq_status = -ENOTCONN;
1246                 /* rv is ignored anyhow */
1247                 return -ENOTCONN;
1248         }
1249         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1250         req = saved_req;
1251         req->rq_reqmsg = reqmsg;
1252         class_export_get(req->rq_export);
1253         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1254
1255         /* only count the first "replay over" request from each
1256            export */
1257         if (req->rq_export->exp_replay_needed) {
1258                 --obd->obd_recoverable_clients;
1259                 req->rq_export->exp_replay_needed = 0;
1260         }
1261         recovery_done = (obd->obd_recoverable_clients == 0);
1262         spin_unlock_bh(&obd->obd_processing_task_lock);
1263
1264         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1265         if (recovery_done) {
1266                 spin_lock_bh(&obd->obd_processing_task_lock);
1267                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1268                 target_cancel_recovery_timer(obd);
1269                 spin_unlock_bh(&obd->obd_processing_task_lock);
1270
1271                 target_finish_recovery(obd);
1272                 ptlrpc_run_recovery_over_upcall(obd);
1273         } else {
1274                 CWARN("%s: %d recoverable clients remain\n",
1275                        obd->obd_name, obd->obd_recoverable_clients);
1276                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1277         }
1278
1279         return 1;
1280 }
1281
1282 int
1283 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1284 {
1285         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1286                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1287                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1288                 return (-ECOMM);
1289         }
1290
1291         if (rc) {
1292                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1293                 req->rq_status = rc;
1294                 return (ptlrpc_error(req));
1295         } else {
1296                 DEBUG_REQ(D_NET, req, "sending reply");
1297         }
1298
1299         return (ptlrpc_send_reply(req, 1));
1300 }
1301
1302 void
1303 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1304 {
1305         int                        netrc;
1306         unsigned long              flags;
1307         struct ptlrpc_reply_state *rs;
1308         struct obd_device         *obd;
1309         struct obd_export         *exp;
1310         struct ptlrpc_service     *svc;
1311
1312         svc = req->rq_rqbd->rqbd_service;
1313         rs = req->rq_reply_state;
1314         if (rs == NULL || !rs->rs_difficult) {
1315                 /* no notifiers */
1316                 target_send_reply_msg (req, rc, fail_id);
1317                 return;
1318         }
1319
1320         /* must be an export if locks saved */
1321         LASSERT (req->rq_export != NULL);
1322         /* req/reply consistent */
1323         LASSERT (rs->rs_service == svc);
1324
1325         /* "fresh" reply */
1326         LASSERT (!rs->rs_scheduled);
1327         LASSERT (!rs->rs_scheduled_ever);
1328         LASSERT (!rs->rs_handled);
1329         LASSERT (!rs->rs_on_net);
1330         LASSERT (rs->rs_export == NULL);
1331         LASSERT (list_empty(&rs->rs_obd_list));
1332         LASSERT (list_empty(&rs->rs_exp_list));
1333
1334         exp = class_export_get (req->rq_export);
1335         obd = exp->exp_obd;
1336
1337         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1338         rs->rs_scheduled = 1;
1339         rs->rs_on_net    = 1;
1340         rs->rs_xid       = req->rq_xid;
1341         rs->rs_transno   = req->rq_transno;
1342         rs->rs_export    = exp;
1343
1344         spin_lock_irqsave (&obd->obd_uncommitted_replies_lock, flags);
1345
1346         if (rs->rs_transno > obd->obd_last_committed) {
1347                 /* not committed already */
1348                 list_add_tail (&rs->rs_obd_list,
1349                                &obd->obd_uncommitted_replies);
1350         }
1351
1352         spin_unlock (&obd->obd_uncommitted_replies_lock);
1353         spin_lock (&exp->exp_lock);
1354
1355         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1356
1357         spin_unlock_irqrestore (&exp->exp_lock, flags);
1358
1359         netrc = target_send_reply_msg (req, rc, fail_id);
1360
1361         spin_lock_irqsave (&svc->srv_lock, flags);
1362
1363         svc->srv_n_difficult_replies++;
1364
1365         if (netrc != 0) {
1366                 /* error sending: reply is off the net.  Also we need +1
1367                  * reply ref until ptlrpc_server_handle_reply() is done
1368                  * with the reply state (if the send was successful, there
1369                  * would have been +1 ref for the net, which
1370                  * reply_out_callback leaves alone) */
1371                 rs->rs_on_net = 0;
1372                 ptlrpc_rs_addref(rs);
1373                 atomic_inc (&svc->srv_outstanding_replies);
1374         }
1375
1376         if (!rs->rs_on_net ||                   /* some notifier */
1377             list_empty(&rs->rs_exp_list) ||     /* completed already */
1378             list_empty(&rs->rs_obd_list)) {
1379                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1380                 cfs_waitq_signal (&svc->srv_waitq);
1381         } else {
1382                 list_add (&rs->rs_list, &svc->srv_active_replies);
1383                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1384         }
1385
1386         spin_unlock_irqrestore (&svc->srv_lock, flags);
1387 }
1388
1389 int target_handle_ping(struct ptlrpc_request *req)
1390 {
1391         return lustre_pack_reply(req, 0, NULL, NULL);
1392 }
1393
1394 void target_committed_to_req(struct ptlrpc_request *req)
1395 {
1396         struct obd_device *obd = req->rq_export->exp_obd;
1397
1398         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1399                 req->rq_repmsg->last_committed = obd->obd_last_committed;
1400         else
1401                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update");
1402
1403         CDEBUG(D_INFO, "last_committed "LPU64", xid "LPU64"\n",
1404                obd->obd_last_committed, req->rq_xid);
1405 }
1406
1407 EXPORT_SYMBOL(target_committed_to_req);
1408
1409 #ifdef HAVE_QUOTA_SUPPORT
1410 int target_handle_qc_callback(struct ptlrpc_request *req)
1411 {
1412         struct obd_quotactl *oqctl;
1413         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1414
1415         oqctl = lustre_swab_reqbuf(req, 0, sizeof(*oqctl),
1416                                    lustre_swab_obd_quotactl);
1417
1418         cli->cl_qchk_stat = oqctl->qc_stat;
1419
1420         return 0;
1421 }
1422
1423 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1424 {
1425 #ifdef __KERNEL__
1426         struct obd_device *obd = req->rq_export->exp_obd;
1427         struct obd_device *master_obd;
1428         struct lustre_quota_ctxt *qctxt;
1429         struct qunit_data *qdata, *rep;
1430         int rc = 0, repsize = sizeof(struct qunit_data);
1431         ENTRY;
1432
1433         rc = lustre_pack_reply(req, 1, &repsize, NULL);
1434         if (rc) {
1435                 CERROR("packing reply failed!: rc = %d\n", rc);
1436                 RETURN(rc);
1437         }
1438         rep = lustre_msg_buf(req->rq_repmsg, 0, sizeof(*rep));
1439         LASSERT(rep);
1440
1441         qdata = lustre_swab_reqbuf(req, 0, sizeof(*qdata), lustre_swab_qdata);
1442         if (qdata == NULL) {
1443                 CERROR("unpacking request buffer failed!");
1444                 RETURN(-EPROTO);
1445         }
1446
1447         /* we use the observer */
1448         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1449         master_obd = obd->obd_observer->obd_observer;
1450         qctxt = &master_obd->u.obt.obt_qctxt;
1451
1452         LASSERT(qctxt->lqc_handler);
1453         rc = qctxt->lqc_handler(master_obd, qdata, req->rq_reqmsg->opc);
1454         if (rc && rc != -EDQUOT)
1455                 CDEBUG_EX(rc == -EBUSY  ? D_QUOTA : D_ERROR,
1456                        "dqacq failed! (rc:%d)\n", rc);
1457
1458         /* the qd_count might be changed in lqc_handler */
1459         memcpy(rep, qdata, sizeof(*rep));
1460         req->rq_status = rc;
1461         rc = ptlrpc_reply(req);
1462
1463         RETURN(rc);
1464 #else
1465         return 0;
1466 #endif /* !__KERNEL__ */
1467 }
1468 #endif /* HAVE_QUOTA_SUPPORT */
1469
1470 ldlm_mode_t lck_compat_array[] = {
1471         [LCK_EX] LCK_COMPAT_EX,
1472         [LCK_PW] LCK_COMPAT_PW,
1473         [LCK_PR] LCK_COMPAT_PR,
1474         [LCK_CW] LCK_COMPAT_CW,
1475         [LCK_CR] LCK_COMPAT_CR,
1476         [LCK_NL] LCK_COMPAT_NL,
1477         [LCK_GROUP] LCK_COMPAT_GROUP
1478 };