Whamcloud - gitweb
c5293b3ba0c55fe8ff5312f1595af855f93f4125
[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 "ldlm_internal.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                                 item->oic_last_attempt = 0;
77                         }
78                         CDEBUG(D_HA, "imp %p@%s: found existing conn %s%s\n",
79                                imp, imp->imp_obd->obd_name, uuid->uuid,
80                                (priority ? ", moved to head" : ""));
81                         spin_unlock(&imp->imp_lock);
82                         GOTO(out_free, rc = 0);
83                 }
84         }
85         /* not found */
86         if (create) {
87                 imp_conn->oic_conn = ptlrpc_conn;
88                 imp_conn->oic_uuid = *uuid;
89                 item->oic_last_attempt = 0;
90                 if (priority)
91                         list_add(&imp_conn->oic_item, &imp->imp_conn_list);
92                 else
93                         list_add_tail(&imp_conn->oic_item, &imp->imp_conn_list);
94                 CDEBUG(D_HA, "imp %p@%s: add connection %s at %s\n",
95                        imp, imp->imp_obd->obd_name, uuid->uuid,
96                        (priority ? "head" : "tail"));
97         } else {
98                 spin_unlock(&imp->imp_lock);
99                 GOTO(out_free, rc = -ENOENT);
100
101         }
102
103         spin_unlock(&imp->imp_lock);
104         RETURN(0);
105 out_free:
106         if (imp_conn)
107                 OBD_FREE(imp_conn, sizeof(*imp_conn));
108 out_put:
109         ptlrpc_put_connection(ptlrpc_conn);
110         RETURN(rc);
111 }
112
113 int import_set_conn_priority(struct obd_import *imp, struct obd_uuid *uuid)
114 {
115         return import_set_conn(imp, uuid, 1, 0);
116 }
117
118 int client_import_add_conn(struct obd_import *imp, struct obd_uuid *uuid,
119                            int priority)
120 {
121         return import_set_conn(imp, uuid, priority, 1);
122 }
123
124 int client_import_del_conn(struct obd_import *imp, struct obd_uuid *uuid)
125 {
126         struct obd_import_conn *imp_conn;
127         struct obd_export *dlmexp;
128         int rc = -ENOENT;
129         ENTRY;
130
131         spin_lock(&imp->imp_lock);
132         if (list_empty(&imp->imp_conn_list)) {
133                 LASSERT(!imp->imp_connection);
134                 GOTO(out, rc);
135         }
136
137         list_for_each_entry(imp_conn, &imp->imp_conn_list, oic_item) {
138                 if (!obd_uuid_equals(uuid, &imp_conn->oic_uuid))
139                         continue;
140                 LASSERT(imp_conn->oic_conn);
141
142                 /* is current conn? */
143                 if (imp_conn == imp->imp_conn_current) {
144                         LASSERT(imp_conn->oic_conn == imp->imp_connection);
145
146                         if (imp->imp_state != LUSTRE_IMP_CLOSED &&
147                             imp->imp_state != LUSTRE_IMP_DISCON) {
148                                 CERROR("can't remove current connection\n");
149                                 GOTO(out, rc = -EBUSY);
150                         }
151
152                         ptlrpc_put_connection(imp->imp_connection);
153                         imp->imp_connection = NULL;
154
155                         dlmexp = class_conn2export(&imp->imp_dlm_handle);
156                         if (dlmexp && dlmexp->exp_connection) {
157                                 LASSERT(dlmexp->exp_connection ==
158                                         imp_conn->oic_conn);
159                                 ptlrpc_put_connection(dlmexp->exp_connection);
160                                 dlmexp->exp_connection = NULL;
161                         }
162                 }
163
164                 list_del(&imp_conn->oic_item);
165                 ptlrpc_put_connection(imp_conn->oic_conn);
166                 OBD_FREE(imp_conn, sizeof(*imp_conn));
167                 CDEBUG(D_HA, "imp %p@%s: remove connection %s\n",
168                        imp, imp->imp_obd->obd_name, uuid->uuid);
169                 rc = 0;
170                 break;
171         }
172 out:
173         spin_unlock(&imp->imp_lock);
174         if (rc == -ENOENT)
175                 CERROR("connection %s not found\n", uuid->uuid);
176         RETURN(rc);
177 }
178
179 /* configure an RPC client OBD device
180  *
181  * lcfg parameters:
182  * 1 - client UUID
183  * 2 - server UUID
184  * 3 - inactive-on-startup
185  */
186 int client_obd_setup(struct obd_device *obddev, obd_count len, void *buf)
187 {
188         struct lustre_cfg* lcfg = buf;
189         struct client_obd *cli = &obddev->u.cli;
190         struct obd_import *imp;
191         struct obd_uuid server_uuid;
192         int rq_portal, rp_portal, connect_op;
193         char *name = obddev->obd_type->typ_name;
194         int rc;
195         ENTRY;
196
197         /* In a more perfect world, we would hang a ptlrpc_client off of
198          * obd_type and just use the values from there. */
199         if (!strcmp(name, LUSTRE_OSC_NAME)) {
200 #ifdef __KERNEL__
201                 /* Can be removed in Lustre 1.8, for compatibility only */
202                 rq_portal = OST_IO_PORTAL;
203 #else
204                 rq_portal = OST_REQUEST_PORTAL;
205 #endif
206                 rp_portal = OSC_REPLY_PORTAL;
207                 connect_op = OST_CONNECT;
208         } else if (!strcmp(name, LUSTRE_MDC_NAME)) {
209                 rq_portal = MDS_REQUEST_PORTAL;
210                 rp_portal = MDC_REPLY_PORTAL;
211                 connect_op = MDS_CONNECT;
212         } else if (!strcmp(name, LUSTRE_MGC_NAME)) {
213                 rq_portal = MGS_REQUEST_PORTAL;
214                 rp_portal = MGC_REPLY_PORTAL;
215                 connect_op = MGS_CONNECT;
216         } else {
217                 CERROR("unknown client OBD type \"%s\", can't setup\n",
218                        name);
219                 RETURN(-EINVAL);
220         }
221
222         if (LUSTRE_CFG_BUFLEN(lcfg, 1) < 1) {
223                 CERROR("requires a TARGET UUID\n");
224                 RETURN(-EINVAL);
225         }
226
227         if (LUSTRE_CFG_BUFLEN(lcfg, 1) > 37) {
228                 CERROR("client UUID must be less than 38 characters\n");
229                 RETURN(-EINVAL);
230         }
231
232         if (LUSTRE_CFG_BUFLEN(lcfg, 2) < 1) {
233                 CERROR("setup requires a SERVER UUID\n");
234                 RETURN(-EINVAL);
235         }
236
237         if (LUSTRE_CFG_BUFLEN(lcfg, 2) > 37) {
238                 CERROR("target UUID must be less than 38 characters\n");
239                 RETURN(-EINVAL);
240         }
241
242         init_rwsem(&cli->cl_sem);
243         sema_init(&cli->cl_mgc_sem, 1);
244         cli->cl_conn_count = 0;
245         memcpy(server_uuid.uuid, lustre_cfg_buf(lcfg, 2),
246                min_t(unsigned int, LUSTRE_CFG_BUFLEN(lcfg, 2),
247                      sizeof(server_uuid)));
248
249         cli->cl_dirty = 0;
250         cli->cl_avail_grant = 0;
251         /* FIXME: should limit this for the sum of all cl_dirty_max */
252         cli->cl_dirty_max = OSC_MAX_DIRTY_DEFAULT * 1024 * 1024;
253         if (cli->cl_dirty_max >> CFS_PAGE_SHIFT > num_physpages / 8)
254                 cli->cl_dirty_max = num_physpages << (CFS_PAGE_SHIFT - 3);
255         CFS_INIT_LIST_HEAD(&cli->cl_cache_waiters);
256         CFS_INIT_LIST_HEAD(&cli->cl_loi_ready_list);
257         CFS_INIT_LIST_HEAD(&cli->cl_loi_write_list);
258         CFS_INIT_LIST_HEAD(&cli->cl_loi_read_list);
259         client_obd_list_lock_init(&cli->cl_loi_list_lock);
260         cli->cl_r_in_flight = 0;
261         cli->cl_w_in_flight = 0;
262         spin_lock_init(&cli->cl_read_rpc_hist.oh_lock);
263         spin_lock_init(&cli->cl_write_rpc_hist.oh_lock);
264         spin_lock_init(&cli->cl_read_page_hist.oh_lock);
265         spin_lock_init(&cli->cl_write_page_hist.oh_lock);
266         spin_lock_init(&cli->cl_read_offset_hist.oh_lock);
267         spin_lock_init(&cli->cl_write_offset_hist.oh_lock);
268         cfs_waitq_init(&cli->cl_destroy_waitq);
269         atomic_set(&cli->cl_destroy_in_flight, 0);
270 #ifdef ENABLE_CHECKSUM
271         /* Turn on checksumming by default. */
272         cli->cl_checksum = 1;
273         /*
274          * The supported checksum types will be worked out at connect time
275          * Set cl_chksum* to CRC32 for now to avoid returning screwed info
276          * through procfs.
277          */
278         cli->cl_cksum_type = cli->cl_supp_cksum_types = OBD_CKSUM_CRC32;
279 #endif
280         atomic_set(&cli->cl_resends, OSC_DEFAULT_RESENDS);
281
282         /* This value may be changed at connect time in
283            ptlrpc_connect_interpret. */
284         cli->cl_max_pages_per_rpc = min((int)PTLRPC_MAX_BRW_PAGES,
285                                         (int)(1024 * 1024 >> CFS_PAGE_SHIFT));
286
287         if (!strcmp(name, LUSTRE_MDC_NAME)) {
288                 cli->cl_max_rpcs_in_flight = MDC_MAX_RIF_DEFAULT;
289         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 128 /* MB */) {
290                 cli->cl_max_rpcs_in_flight = 2;
291         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 256 /* MB */) {
292                 cli->cl_max_rpcs_in_flight = 3;
293         } else if (num_physpages >> (20 - CFS_PAGE_SHIFT) <= 512 /* MB */) {
294                 cli->cl_max_rpcs_in_flight = 4;
295         } else {
296                 cli->cl_max_rpcs_in_flight = OSC_MAX_RIF_DEFAULT;
297         }
298         rc = ldlm_get_ref();
299         if (rc) {
300                 CERROR("ldlm_get_ref failed: %d\n", rc);
301                 GOTO(err, rc);
302         }
303
304         ptlrpc_init_client(rq_portal, rp_portal, name,
305                            &obddev->obd_ldlm_client);
306
307         imp = class_new_import(obddev);
308         if (imp == NULL)
309                 GOTO(err_ldlm, rc = -ENOENT);
310         imp->imp_client = &obddev->obd_ldlm_client;
311         imp->imp_connect_op = connect_op;
312         imp->imp_initial_recov = 1;
313         imp->imp_initial_recov_bk = 0;
314         CFS_INIT_LIST_HEAD(&imp->imp_pinger_chain);
315         memcpy(cli->cl_target_uuid.uuid, lustre_cfg_buf(lcfg, 1),
316                LUSTRE_CFG_BUFLEN(lcfg, 1));
317         class_import_put(imp);
318
319         rc = client_import_add_conn(imp, &server_uuid, 1);
320         if (rc) {
321                 CERROR("can't add initial connection\n");
322                 GOTO(err_import, rc);
323         }
324
325         cli->cl_import = imp;
326         /* cli->cl_max_mds_{easize,cookiesize} updated by mdc_init_ea_size() */
327         cli->cl_max_mds_easize = sizeof(struct lov_mds_md);
328         cli->cl_max_mds_cookiesize = sizeof(struct llog_cookie);
329
330         if (LUSTRE_CFG_BUFLEN(lcfg, 3) > 0) {
331                 if (!strcmp(lustre_cfg_string(lcfg, 3), "inactive")) {
332                         CDEBUG(D_HA, "marking %s %s->%s as inactive\n",
333                                name, obddev->obd_name,
334                                cli->cl_target_uuid.uuid);
335                         spin_lock(&imp->imp_lock);
336                         imp->imp_invalid = 1;
337                         spin_unlock(&imp->imp_lock);
338                 }
339         }
340
341         cli->cl_qchk_stat = CL_NOT_QUOTACHECKED;
342
343         RETURN(rc);
344
345 err_import:
346         class_destroy_import(imp);
347 err_ldlm:
348         ldlm_put_ref();
349 err:
350         RETURN(rc);
351
352 }
353
354 int client_obd_cleanup(struct obd_device *obddev)
355 {
356         ENTRY;
357         ldlm_put_ref();
358         RETURN(0);
359 }
360
361 /* ->o_connect() method for client side (OSC and MDC and MGC) */
362 int client_connect_import(struct lustre_handle *dlm_handle,
363                           struct obd_device *obd, struct obd_uuid *cluuid,
364                           struct obd_connect_data *data, void *localdata)
365 {
366         struct client_obd *cli = &obd->u.cli;
367         struct obd_import *imp = cli->cl_import;
368         struct obd_export *exp;
369         struct obd_connect_data *ocd;
370         struct ldlm_namespace *to_be_freed = NULL;
371         int rc;
372         ENTRY;
373
374         down_write(&cli->cl_sem);
375         rc = class_connect(dlm_handle, obd, cluuid);
376         if (rc)
377                 GOTO(out_sem, rc);
378
379         cli->cl_conn_count++;
380         if (cli->cl_conn_count > 1)
381                 GOTO(out_sem, rc);
382         exp = class_conn2export(dlm_handle);
383
384         if (obd->obd_namespace != NULL)
385                 CERROR("already have namespace!\n");
386         obd->obd_namespace = ldlm_namespace_new(obd, obd->obd_name,
387                                                 LDLM_NAMESPACE_CLIENT,
388                                                 LDLM_NAMESPACE_GREEDY);
389         if (obd->obd_namespace == NULL)
390                 GOTO(out_disco, rc = -ENOMEM);
391
392         imp->imp_dlm_handle = *dlm_handle;
393         rc = ptlrpc_init_import(imp);
394         if (rc != 0)
395                 GOTO(out_ldlm, rc);
396
397         ocd = &imp->imp_connect_data;
398         if (data) {
399                 *ocd = *data;
400                 imp->imp_connect_flags_orig = data->ocd_connect_flags;
401         }
402
403         rc = ptlrpc_connect_import(imp, NULL);
404         if (rc != 0) {
405                 LASSERT (imp->imp_state == LUSTRE_IMP_DISCON);
406                 GOTO(out_ldlm, rc);
407         }
408         LASSERT(exp->exp_connection);
409
410         if (data) {
411                 LASSERT((ocd->ocd_connect_flags & data->ocd_connect_flags) ==
412                         ocd->ocd_connect_flags);
413                 data->ocd_connect_flags = ocd->ocd_connect_flags;
414         }
415
416         ptlrpc_pinger_add_import(imp);
417         EXIT;
418
419         if (rc) {
420 out_ldlm:
421                 ldlm_namespace_free_prior(obd->obd_namespace, imp, 0);
422                 to_be_freed = obd->obd_namespace;
423                 obd->obd_namespace = NULL;
424 out_disco:
425                 cli->cl_conn_count--;
426                 class_disconnect(exp);
427         } else {
428                 class_export_put(exp);
429         }
430 out_sem:
431         up_write(&cli->cl_sem);
432         if (to_be_freed)
433                 ldlm_namespace_free_post(to_be_freed);
434         return rc;
435 }
436
437 int client_disconnect_export(struct obd_export *exp)
438 {
439         struct obd_device *obd = class_exp2obd(exp);
440         struct client_obd *cli;
441         struct obd_import *imp;
442         struct ldlm_namespace *to_be_freed = NULL;
443         int rc = 0, err;
444         ENTRY;
445
446         if (!obd) {
447                 CERROR("invalid export for disconnect: exp %p cookie "LPX64"\n",
448                        exp, exp ? exp->exp_handle.h_cookie : -1);
449                 RETURN(-EINVAL);
450         }
451
452         cli = &obd->u.cli;
453         imp = cli->cl_import;
454
455         down_write(&cli->cl_sem);
456         if (!cli->cl_conn_count) {
457                 CERROR("disconnecting disconnected device (%s)\n",
458                        obd->obd_name);
459                 GOTO(out_sem, rc = -EINVAL);
460         }
461
462         cli->cl_conn_count--;
463         if (cli->cl_conn_count)
464                 GOTO(out_no_disconnect, rc = 0);
465
466         /* Mark import deactivated now, so we don't try to reconnect if any
467          * of the cleanup RPCs fails (e.g. ldlm cancel, etc).  We don't
468          * fully deactivate the import, or that would drop all requests. */
469         spin_lock(&imp->imp_lock);
470         imp->imp_deactive = 1;
471         spin_unlock(&imp->imp_lock);
472
473         /* Some non-replayable imports (MDS's OSCs) are pinged, so just
474          * delete it regardless.  (It's safe to delete an import that was
475          * never added.) */
476         (void)ptlrpc_pinger_del_import(imp);
477
478         if (obd->obd_namespace != NULL) {
479                 /* obd_force == local only */
480                 ldlm_cli_cancel_unused(obd->obd_namespace, NULL,
481                                        obd->obd_force ? LDLM_FL_LOCAL_ONLY:0,
482                                        NULL);
483                 ldlm_namespace_free_prior(obd->obd_namespace, imp, 
484                                           obd->obd_force);
485                 to_be_freed = obd->obd_namespace;
486         }
487
488         rc = ptlrpc_disconnect_import(imp, 0);
489
490         ptlrpc_invalidate_import(imp);
491         /* set obd_namespace to NULL only after invalidate, because we can have
492          * some connect requests in flight, and his need store a connect flags
493          * in obd_namespace. bug 14260 */
494         obd->obd_namespace = NULL;
495         
496         ptlrpc_free_rq_pool(imp->imp_rq_pool);
497         class_destroy_import(imp);
498         cli->cl_import = NULL;
499
500         EXIT;
501  out_no_disconnect:
502         err = class_disconnect(exp);
503         if (!rc && err)
504                 rc = err;
505  out_sem:
506         up_write(&cli->cl_sem);
507         if (to_be_freed)
508                 ldlm_namespace_free_post(to_be_freed);
509         RETURN(rc);
510 }
511
512 /* --------------------------------------------------------------------------
513  * from old lib/target.c
514  * -------------------------------------------------------------------------- */
515
516 int target_handle_reconnect(struct lustre_handle *conn, struct obd_export *exp,
517                             struct obd_uuid *cluuid)
518 {
519         ENTRY;
520         if (exp->exp_connection && exp->exp_imp_reverse) {
521                 struct lustre_handle *hdl;
522                 hdl = &exp->exp_imp_reverse->imp_remote_handle;
523                 /* Might be a re-connect after a partition. */
524                 if (!memcmp(&conn->cookie, &hdl->cookie, sizeof conn->cookie)) {
525                         CWARN("%s: %s reconnecting\n", exp->exp_obd->obd_name,
526                               cluuid->uuid);
527                         conn->cookie = exp->exp_handle.h_cookie;
528                         /* target_handle_connect() treats EALREADY and
529                          * -EALREADY differently.  EALREADY means we are
530                          * doing a valid reconnect from the same client. */
531                         RETURN(EALREADY);
532                 } else {
533                         CERROR("%s reconnecting from %s, "
534                                "handle mismatch (ours "LPX64", theirs "
535                                LPX64")\n", cluuid->uuid,
536                                exp->exp_connection->c_remote_uuid.uuid,
537                                hdl->cookie, conn->cookie);
538                         memset(conn, 0, sizeof *conn);
539                         /* target_handle_connect() treats EALREADY and
540                          * -EALREADY differently.  -EALREADY is an error
541                          * (same UUID, different handle). */
542                         RETURN(-EALREADY);
543                 }
544         }
545
546         conn->cookie = exp->exp_handle.h_cookie;
547         CDEBUG(D_HA, "connect export for UUID '%s' at %p, cookie "LPX64"\n",
548                cluuid->uuid, exp, conn->cookie);
549         RETURN(0);
550 }
551
552 void target_client_add_cb(struct obd_device *obd, __u64 transno, void *cb_data,
553                           int error)
554 {
555         struct obd_export *exp = cb_data;
556
557         CDEBUG(D_RPCTRACE, "%s: committing for initial connect of %s\n",
558                obd->obd_name, exp->exp_client_uuid.uuid);
559
560         spin_lock(&exp->exp_lock);
561         exp->exp_need_sync = 0;
562         spin_unlock(&exp->exp_lock);
563 }
564 EXPORT_SYMBOL(target_client_add_cb);
565
566 static void 
567 target_start_and_reset_recovery_timer(struct obd_device *obd,
568                                       svc_handler_t handler,
569                                       struct ptlrpc_request *req,
570                                       int new_client);
571
572 int target_handle_connect(struct ptlrpc_request *req, svc_handler_t handler)
573 {
574         struct obd_device *target, *targref = NULL;
575         struct obd_export *export = NULL;
576         struct obd_import *revimp;
577         struct lustre_handle conn;
578         struct obd_uuid tgtuuid;
579         struct obd_uuid cluuid;
580         struct obd_uuid remote_uuid;
581         char *str, *tmp;
582         int rc = 0, abort_recovery;
583         struct obd_connect_data *data;
584         int size[2] = { sizeof(struct ptlrpc_body), sizeof(*data) };
585         lnet_nid_t *client_nid = NULL;
586         ENTRY;
587
588         OBD_RACE(OBD_FAIL_TGT_CONN_RACE);
589
590         lustre_set_req_swabbed(req, REQ_REC_OFF);
591         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF, sizeof(tgtuuid)-1);
592         if (str == NULL) {
593                 DEBUG_REQ(D_ERROR, req, "bad target UUID for connect");
594                 GOTO(out, rc = -EINVAL);
595         }
596
597         obd_str2uuid (&tgtuuid, str);
598         target = class_uuid2obd(&tgtuuid);
599         /* COMPAT_146 */
600         /* old (pre 1.6) lustre_process_log tries to connect to mdsname
601            (eg. mdsA) instead of uuid. */
602         if (!target) {
603                 snprintf((char *)tgtuuid.uuid, sizeof(tgtuuid), "%s_UUID", str);
604                 target = class_uuid2obd(&tgtuuid);
605         }
606         if (!target)
607                 target = class_name2obd(str);
608         /* end COMPAT_146 */
609
610         if (!target || target->obd_stopping || !target->obd_set_up) {
611                 LCONSOLE_ERROR_MSG(0x137, "UUID '%s' is not available "
612                                    " for connect (%s)\n", str,
613                                    !target ? "no target" :
614                                    (target->obd_stopping ? "stopping" :
615                                    "not set up"));
616                 GOTO(out, rc = -ENODEV);
617         }
618
619         if (target->obd_no_conn) {
620                 LCONSOLE_WARN("%s: temporarily refusing client connection "
621                               "from %s\n", target->obd_name, 
622                               libcfs_nid2str(req->rq_peer.nid));
623                 GOTO(out, rc = -EAGAIN);
624         }
625
626         /* Make sure the target isn't cleaned up while we're here. Yes, 
627            there's still a race between the above check and our incref here. 
628            Really, class_uuid2obd should take the ref. */
629         targref = class_incref(target);
630
631         lustre_set_req_swabbed(req, REQ_REC_OFF + 1);
632         str = lustre_msg_string(req->rq_reqmsg, REQ_REC_OFF + 1,
633                                 sizeof(cluuid) - 1);
634         if (str == NULL) {
635                 DEBUG_REQ(D_ERROR, req, "bad client UUID for connect");
636                 GOTO(out, rc = -EINVAL);
637         }
638
639         obd_str2uuid (&cluuid, str);
640
641         /* XXX extract a nettype and format accordingly */
642         switch (sizeof(lnet_nid_t)) {
643                 /* NB the casts only avoid compiler warnings */
644         case 8:
645                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
646                          "NET_"LPX64"_UUID", (__u64)req->rq_peer.nid);
647                 break;
648         case 4:
649                 snprintf(remote_uuid.uuid, sizeof remote_uuid,
650                          "NET_%x_UUID", (__u32)req->rq_peer.nid);
651                 break;
652         default:
653                 LBUG();
654         }
655
656         spin_lock_bh(&target->obd_processing_task_lock);
657         abort_recovery = target->obd_abort_recovery;
658         spin_unlock_bh(&target->obd_processing_task_lock);
659         if (abort_recovery)
660                 target_abort_recovery(target);
661
662         tmp = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn);
663         if (tmp == NULL)
664                 GOTO(out, rc = -EPROTO);
665
666         memcpy(&conn, tmp, sizeof conn);
667
668         data = lustre_swab_reqbuf(req, REQ_REC_OFF + 3, sizeof(*data),
669                                   lustre_swab_connect);
670         rc = lustre_pack_reply(req, 2, size, NULL);
671         if (rc)
672                 GOTO(out, rc);
673
674         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
675                 if (!data) {
676                         DEBUG_REQ(D_WARNING, req, "Refusing old (unversioned) "
677                                   "libclient connection attempt");
678                         GOTO(out, rc = -EPROTO);
679                 } else if (data->ocd_version < LUSTRE_VERSION_CODE -
680                                                LUSTRE_VERSION_ALLOWED_OFFSET ||
681                            data->ocd_version > LUSTRE_VERSION_CODE +
682                                                LUSTRE_VERSION_ALLOWED_OFFSET) {
683                         DEBUG_REQ(D_WARNING, req, "Refusing %s (%d.%d.%d.%d) "
684                                   "libclient connection attempt",
685                                   data->ocd_version < LUSTRE_VERSION_CODE ?
686                                   "old" : "new",
687                                   OBD_OCD_VERSION_MAJOR(data->ocd_version),
688                                   OBD_OCD_VERSION_MINOR(data->ocd_version),
689                                   OBD_OCD_VERSION_PATCH(data->ocd_version),
690                                   OBD_OCD_VERSION_FIX(data->ocd_version));
691                         data = lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
692                                               offsetof(typeof(*data),
693                                                        ocd_version) +
694                                               sizeof(data->ocd_version));
695                         if (data) {
696                                 data->ocd_connect_flags = OBD_CONNECT_VERSION;
697                                 data->ocd_version = LUSTRE_VERSION_CODE;
698                         }
699                         GOTO(out, rc = -EPROTO);
700                 }
701         }
702
703         /* lctl gets a backstage, all-access pass. */
704         if (obd_uuid_equals(&cluuid, &target->obd_uuid))
705                 goto dont_check_exports;
706
707         spin_lock(&target->obd_dev_lock);
708         export = lustre_hash_get_object_by_key(target->obd_uuid_hash_body, &cluuid);
709
710         if (export != NULL && export->exp_connecting) { /* bug 9635, et. al. */
711                 CWARN("%s: exp %p already connecting\n",
712                       export->exp_obd->obd_name, export);
713                 class_export_put(export);
714                 export = NULL;
715                 rc = -EALREADY;
716         } else if (export != NULL && export->exp_connection != NULL &&
717                    req->rq_peer.nid != export->exp_connection->c_peer.nid) {
718                 /* make darn sure this is coming from the same peer
719                  * if the UUIDs matched */
720                   CWARN("%s: cookie %s seen on new NID %s when "
721                           "existing NID %s is already connected\n",
722                         target->obd_name, cluuid.uuid,
723                   libcfs_nid2str(req->rq_peer.nid),
724                   libcfs_nid2str(export->exp_connection->c_peer.nid));
725                   class_export_put(export);
726                   export = NULL;
727                   rc = -EALREADY;
728         } else if (export != NULL && export->exp_failed) { /* bug 11327 */
729                 CDEBUG(D_HA, "%s: exp %p evict in progress - new cookie needed "
730                       "for connect\n", export->exp_obd->obd_name, export);
731                 class_export_put(export);
732                 export = NULL;
733                 rc = -ENODEV;
734         } else if (export != NULL) {
735                 spin_lock(&export->exp_lock);
736                 export->exp_connecting = 1;
737                 spin_unlock(&export->exp_lock);
738                 class_export_put(export);
739                 spin_unlock(&target->obd_dev_lock);
740                 LASSERT(export->exp_obd == target);
741
742                 rc = target_handle_reconnect(&conn, export, &cluuid);
743         }
744
745         /* If we found an export, we already unlocked. */
746         if (!export) {
747                 spin_unlock(&target->obd_dev_lock);
748                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_CONNECT, 2 * obd_timeout);
749         } else if (req->rq_export == NULL &&
750                    atomic_read(&export->exp_rpc_count) > 0) {
751                 CWARN("%s: refuse connection from %s/%s to 0x%p; still busy "
752                       "with %d references\n", target->obd_name, cluuid.uuid,
753                       libcfs_nid2str(req->rq_peer.nid),
754                       export, atomic_read(&export->exp_refcount));
755                 GOTO(out, rc = -EBUSY);
756         } else if (req->rq_export != NULL &&
757                    atomic_read(&export->exp_rpc_count) > 1) {
758                 CWARN("%s: refuse reconnection from %s@%s to 0x%p; still busy "
759                       "with %d active RPCs\n", target->obd_name, cluuid.uuid,
760                       libcfs_nid2str(req->rq_peer.nid),
761                       export, atomic_read(&export->exp_rpc_count));
762                 GOTO(out, rc = -EBUSY);
763         } else if (lustre_msg_get_conn_cnt(req->rq_reqmsg) == 1) {
764                 CERROR("%s: NID %s (%s) reconnected with 1 conn_cnt; "
765                        "cookies not random?\n", target->obd_name,
766                        libcfs_nid2str(req->rq_peer.nid), cluuid.uuid);
767                 GOTO(out, rc = -EALREADY);
768         } else {
769                 OBD_FAIL_TIMEOUT(OBD_FAIL_TGT_DELAY_RECONNECT, 2 * obd_timeout);
770         }
771
772         if (rc < 0)
773                 GOTO(out, rc);
774
775         /* Tell the client if we're in recovery. */
776         if (target->obd_recovering) {
777                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECOVERING);
778                 /* If this is the first time a client connects,
779                    reset the recovery timer */
780                 if (rc == 0)
781                         target_start_and_reset_recovery_timer(target, handler,
782                                                               req, !export);
783         }
784
785         /* We want to handle EALREADY but *not* -EALREADY from
786          * target_handle_reconnect(), return reconnection state in a flag */
787         if (rc == EALREADY) {
788                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_RECONNECT);
789                 rc = 0;
790         } else {
791                 LASSERT(rc == 0);
792         }
793
794         /* Tell the client if we support replayable requests */
795         if (target->obd_replayable)
796                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_REPLAYABLE);
797         client_nid = &req->rq_peer.nid;
798
799         if (export == NULL) {
800                 if (target->obd_recovering) {
801                         CERROR("%s: denying connection for new client %s (%s): "
802                                "%d clients in recovery for %lds\n",
803                                target->obd_name,
804                                libcfs_nid2str(req->rq_peer.nid), cluuid.uuid,
805                                target->obd_recoverable_clients,
806                                cfs_duration_sec(cfs_time_sub(cfs_timer_deadline(&target->obd_recovery_timer),
807                                                              cfs_time_current())));
808                         rc = -EBUSY;
809                 } else {
810  dont_check_exports:
811                         rc = obd_connect(&conn, target, &cluuid, data,
812                                          client_nid);
813                 }
814         } else {
815                 rc = obd_reconnect(export, target, &cluuid, data);
816         }
817
818         if (rc)
819                 GOTO(out, rc);
820
821         /* Return only the parts of obd_connect_data that we understand, so the
822          * client knows that we don't understand the rest. */
823         if (data)
824                 memcpy(lustre_msg_buf(req->rq_repmsg, REPLY_REC_OFF,
825                                       sizeof(*data)),
826                        data, sizeof(*data));
827
828         /* If all else goes well, this is our RPC return code. */
829         req->rq_status = 0;
830
831         lustre_msg_set_handle(req->rq_repmsg, &conn);
832
833         /* ownership of this export ref transfers to the request AFTER we
834          * drop any previous reference the request had, but we don't want
835          * that to go to zero before we get our new export reference. */
836         export = class_conn2export(&conn);
837         if (!export) {
838                 DEBUG_REQ(D_ERROR, req, "Missing export!");
839                 GOTO(out, rc = -ENODEV);
840         }
841
842         /* If the client and the server are the same node, we will already
843          * have an export that really points to the client's DLM export,
844          * because we have a shared handles table.
845          *
846          * XXX this will go away when shaver stops sending the "connect" handle
847          * in the real "remote handle" field of the request --phik 24 Apr 2003
848          */
849         if (req->rq_export != NULL)
850                 class_export_put(req->rq_export);
851
852         req->rq_export = export;
853
854         spin_lock(&export->exp_lock);
855         if (export->exp_conn_cnt >= lustre_msg_get_conn_cnt(req->rq_reqmsg)) {
856                 CERROR("%s: %s already connected at higher conn_cnt: %d > %d\n",
857                        cluuid.uuid, libcfs_nid2str(req->rq_peer.nid),
858                        export->exp_conn_cnt,
859                        lustre_msg_get_conn_cnt(req->rq_reqmsg));
860
861                 spin_unlock(&export->exp_lock);
862                 GOTO(out, rc = -EALREADY);
863         }
864         export->exp_conn_cnt = lustre_msg_get_conn_cnt(req->rq_reqmsg);
865
866         /* request from liblustre?  Don't evict it for not pinging. */
867         if (lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_LIBCLIENT) {
868                 export->exp_libclient = 1;
869                 spin_unlock(&export->exp_lock);
870
871                 spin_lock(&target->obd_dev_lock);
872                 list_del_init(&export->exp_obd_chain_timed);
873                 spin_unlock(&target->obd_dev_lock);
874         } else {
875                 spin_unlock(&export->exp_lock);
876         }
877
878         if (export->exp_connection != NULL)
879                 ptlrpc_put_connection(export->exp_connection);
880         export->exp_connection = ptlrpc_get_connection(req->rq_peer,
881                                                        req->rq_self,
882                                                        &remote_uuid);
883
884         spin_lock(&target->obd_dev_lock);
885         /* Export might be hashed already, e.g. if this is reconnect */
886         if (hlist_unhashed(&export->exp_nid_hash))
887                 lustre_hash_additem(export->exp_obd->obd_nid_hash_body,
888                                     &export->exp_connection->c_peer.nid,
889                                     &export->exp_nid_hash);
890         spin_unlock(&target->obd_dev_lock);
891
892         if (lustre_msg_get_op_flags(req->rq_repmsg) & MSG_CONNECT_RECONNECT) {
893                 revimp = class_import_get(export->exp_imp_reverse);
894                 GOTO(set_flags, rc = 0);
895         }
896
897         if (target->obd_recovering)
898                 target->obd_connected_clients++;
899
900         memcpy(&conn,
901                lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF + 2, sizeof conn),
902                sizeof conn);
903
904         if (export->exp_imp_reverse != NULL)
905                 class_destroy_import(export->exp_imp_reverse);
906         revimp = export->exp_imp_reverse = class_new_import(target);
907         revimp->imp_connection = ptlrpc_connection_addref(export->exp_connection);
908         revimp->imp_client = &export->exp_obd->obd_ldlm_client;
909         revimp->imp_remote_handle = conn;
910         revimp->imp_dlm_fake = 1;
911         revimp->imp_state = LUSTRE_IMP_FULL;
912
913 set_flags:
914         if (req->rq_reqmsg->lm_magic == LUSTRE_MSG_MAGIC_V1 &&
915             lustre_msg_get_op_flags(req->rq_reqmsg) & MSG_CONNECT_NEXT_VER) {
916                 revimp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
917                 lustre_msg_add_op_flags(req->rq_repmsg, MSG_CONNECT_NEXT_VER);
918         } else {
919                 /* unknown versions will be caught in
920                  * ptlrpc_handle_server_req_in->lustre_unpack_msg() */
921                 revimp->imp_msg_magic = req->rq_reqmsg->lm_magic;
922         }
923
924         if (revimp->imp_msg_magic != LUSTRE_MSG_MAGIC_V1) {
925                 if (export->exp_connect_flags & OBD_CONNECT_AT)
926                         revimp->imp_msghdr_flags |= MSGHDR_AT_SUPPORT;
927                 else
928                         revimp->imp_msghdr_flags &= ~MSGHDR_AT_SUPPORT;
929         }
930
931         class_import_put(revimp);
932 out:
933         if (export) {
934                 spin_lock(&export->exp_lock);
935                 export->exp_connecting = 0;
936                 spin_unlock(&export->exp_lock);
937         }
938         if (targref)
939                 class_decref(targref);
940         if (rc)
941                 req->rq_status = rc;
942         RETURN(rc);
943 }
944
945 int target_handle_disconnect(struct ptlrpc_request *req)
946 {
947         int rc;
948         ENTRY;
949
950         rc = lustre_pack_reply(req, 1, NULL, NULL);
951         if (rc)
952                 RETURN(rc);
953
954         /* keep the rq_export around so we can send the reply */
955         req->rq_status = obd_disconnect(class_export_get(req->rq_export));
956         RETURN(0);
957 }
958
959 void target_destroy_export(struct obd_export *exp)
960 {
961         /* exports created from last_rcvd data, and "fake"
962            exports created by lctl don't have an import */
963         if (exp->exp_imp_reverse != NULL)
964                 class_destroy_import(exp->exp_imp_reverse);
965
966         /* We cancel locks at disconnect time, but this will catch any locks
967          * granted in a race with recovery-induced disconnect. */
968         if (exp->exp_obd->obd_namespace != NULL)
969                 ldlm_cancel_locks_for_export(exp);
970 }
971
972 /*
973  * Recovery functions
974  */
975
976 static int target_exp_enqueue_req_replay(struct ptlrpc_request *req)
977 {
978         __u64                  transno = lustre_msg_get_transno(req->rq_reqmsg);
979         struct obd_export     *exp = req->rq_export;
980         struct ptlrpc_request *reqiter;
981         int                    dup = 0;
982
983         LASSERT(exp);
984
985         spin_lock(&exp->exp_lock);
986         list_for_each_entry(reqiter, &exp->exp_req_replay_queue,
987                             rq_replay_list) {
988                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) == transno) {
989                         dup = 1;
990                         break;
991                 }
992         }
993
994         if (dup) {
995                 /* we expect it with RESENT and REPLAY flags */
996                 if ((lustre_msg_get_flags(req->rq_reqmsg) &
997                      (MSG_RESENT | MSG_REPLAY)) != (MSG_RESENT | MSG_REPLAY))
998                         CERROR("invalid flags %x of resent replay\n",
999                                lustre_msg_get_flags(req->rq_reqmsg));
1000         } else {
1001                 list_add_tail(&req->rq_replay_list, &exp->exp_req_replay_queue);
1002         }
1003
1004         spin_unlock(&exp->exp_lock);
1005         return dup;
1006 }
1007
1008 static void target_exp_dequeue_req_replay(struct ptlrpc_request *req)
1009 {
1010         LASSERT(!list_empty(&req->rq_replay_list));
1011         LASSERT(req->rq_export);
1012
1013         spin_lock(&req->rq_export->exp_lock);
1014         list_del_init(&req->rq_replay_list);
1015         spin_unlock(&req->rq_export->exp_lock);
1016 }
1017
1018 static void target_release_saved_req(struct ptlrpc_request *req)
1019 {
1020         ptlrpc_req_drop_rs(req);
1021         class_export_put(req->rq_export);
1022         OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1023         OBD_FREE(req, sizeof *req);
1024 }
1025
1026 static void target_finish_recovery(struct obd_device *obd)
1027 {
1028         struct list_head *tmp, *n;
1029
1030         LCONSOLE_INFO("%s: sending delayed replies to recovered clients\n",
1031                       obd->obd_name);
1032
1033         ldlm_reprocess_all_ns(obd->obd_namespace);
1034
1035         /* when recovery finished, cleanup orphans on mds and ost */
1036         if (OBT(obd) && OBP(obd, postrecov)) {
1037                 int rc = OBP(obd, postrecov)(obd);
1038                 LCONSOLE_WARN("%s: recovery %s: rc %d\n", obd->obd_name,
1039                               rc < 0 ? "failed" : "complete", rc);
1040         }
1041
1042         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1043                 struct ptlrpc_request *req;
1044                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1045                 list_del(&req->rq_list);
1046                 DEBUG_REQ(D_HA, req, "delayed:");
1047                 ptlrpc_reply(req);
1048                 target_release_saved_req(req);
1049         }
1050         obd->obd_recovery_end = cfs_time_current_sec();
1051 }
1052
1053 static void abort_recovery_queue(struct obd_device *obd)
1054 {
1055         struct ptlrpc_request *req;
1056         struct list_head *tmp, *n;
1057         int rc;
1058
1059         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1060                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1061                 target_exp_dequeue_req_replay(req);
1062                 list_del(&req->rq_list);
1063                 DEBUG_REQ(D_ERROR, req, "aborted:");
1064                 req->rq_status = -ENOTCONN;
1065                 req->rq_type = PTL_RPC_MSG_ERR;
1066                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1067                 if (rc == 0)
1068                         ptlrpc_reply(req);
1069                 else
1070                         DEBUG_REQ(D_ERROR, req,
1071                                   "packing failed for abort-reply; skipping");
1072                 target_release_saved_req(req);
1073         }
1074 }
1075
1076 /* Called from a cleanup function if the device is being cleaned up
1077    forcefully.  The exports should all have been disconnected already,
1078    the only thing left to do is
1079      - clear the recovery flags
1080      - cancel the timer
1081      - free queued requests and replies, but don't send replies
1082    Because the obd_stopping flag is set, no new requests should be received.
1083
1084 */
1085 void target_cleanup_recovery(struct obd_device *obd)
1086 {
1087         struct list_head *tmp, *n;
1088         struct ptlrpc_request *req;
1089         ENTRY;
1090
1091         LASSERT(obd->obd_stopping);
1092
1093         spin_lock_bh(&obd->obd_processing_task_lock);
1094         if (!obd->obd_recovering) {
1095                 spin_unlock_bh(&obd->obd_processing_task_lock);
1096                 EXIT;
1097                 return;
1098         }
1099         obd->obd_recovering = obd->obd_abort_recovery = 0;
1100         target_cancel_recovery_timer(obd);
1101         spin_unlock_bh(&obd->obd_processing_task_lock);
1102
1103         list_for_each_safe(tmp, n, &obd->obd_delayed_reply_queue) {
1104                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1105                 list_del(&req->rq_list);
1106                 target_release_saved_req(req);
1107         }
1108
1109         list_for_each_safe(tmp, n, &obd->obd_recovery_queue) {
1110                 req = list_entry(tmp, struct ptlrpc_request, rq_list);
1111                 target_exp_dequeue_req_replay(req);
1112                 list_del(&req->rq_list);
1113                 target_release_saved_req(req);
1114         }
1115         EXIT;
1116 }
1117
1118 void target_abort_recovery(void *data)
1119 {
1120         struct obd_device *obd = data;
1121         ENTRY;
1122
1123         spin_lock_bh(&obd->obd_processing_task_lock);
1124         if (!obd->obd_recovering) {
1125                 spin_unlock_bh(&obd->obd_processing_task_lock);
1126                 EXIT;
1127                 return;
1128         }
1129         obd->obd_recovering = obd->obd_abort_recovery = 0;
1130         target_cancel_recovery_timer(obd);
1131         spin_unlock_bh(&obd->obd_processing_task_lock);
1132
1133         LCONSOLE_WARN("%s: recovery period over; %d clients never reconnected "
1134                       "after %lds (%d clients did)\n",
1135                       obd->obd_name, obd->obd_recoverable_clients,
1136                       cfs_time_current_sec()- obd->obd_recovery_start,
1137                       obd->obd_connected_clients);
1138         class_disconnect_stale_exports(obd);
1139         abort_recovery_queue(obd);
1140
1141         target_finish_recovery(obd);
1142         CDEBUG(D_HA, "%s: recovery complete\n", obd_uuid2str(&obd->obd_uuid));
1143         EXIT;
1144 }
1145
1146 static void target_recovery_expired(unsigned long castmeharder)
1147 {
1148         struct obd_device *obd = (struct obd_device *)castmeharder;
1149         CERROR("%s: recovery timed out, aborting\n", obd->obd_name);
1150         spin_lock_bh(&obd->obd_processing_task_lock);
1151         if (obd->obd_recovering)
1152                 obd->obd_abort_recovery = 1;
1153         cfs_waitq_signal(&obd->obd_next_transno_waitq);
1154         spin_unlock_bh(&obd->obd_processing_task_lock);
1155 }
1156
1157
1158 /* obd_processing_task_lock should be held */
1159 void target_cancel_recovery_timer(struct obd_device *obd)
1160 {
1161         CDEBUG(D_HA, "%s: cancel recovery timer\n", obd->obd_name);
1162         cfs_timer_disarm(&obd->obd_recovery_timer);
1163 }
1164
1165 /* extend = 1 means require at least "duration" seconds left in the timer,
1166    extend = 0 means set the total duration (start_recovery_timer) */
1167 static void reset_recovery_timer(struct obd_device *obd, int duration,
1168                                  int extend)
1169 {
1170         cfs_time_t now = cfs_time_current_sec();
1171         cfs_duration_t left;
1172
1173         spin_lock_bh(&obd->obd_processing_task_lock);
1174         if (!obd->obd_recovering) {
1175                 spin_unlock_bh(&obd->obd_processing_task_lock);
1176                 return;
1177         }
1178
1179         left = cfs_time_sub(obd->obd_recovery_end, now);
1180
1181         if (extend && (duration > left))
1182                 obd->obd_recovery_timeout += duration - left;
1183         else if (!extend && (duration > obd->obd_recovery_timeout))
1184                 /* Track the client's largest expected replay time */
1185                 obd->obd_recovery_timeout = duration;
1186 #ifdef CRAY_XT3
1187         /* 
1188          * If total recovery time already exceed the 
1189          * obd_recovery_max_time, then CRAY XT3 will 
1190          * abort the recovery
1191          */
1192         if(obd->obd_recovery_timeout > obd->obd_recovery_max_time)
1193                 obd->obd_recovery_timeout = obd->obd_recovery_max_time;
1194 #endif
1195         obd->obd_recovery_end = obd->obd_recovery_start + 
1196                                 obd->obd_recovery_timeout;
1197         if (cfs_time_before(now, obd->obd_recovery_end)) {
1198                 left = cfs_time_sub(obd->obd_recovery_end, now);
1199                 cfs_timer_arm(&obd->obd_recovery_timer, cfs_time_shift(left));
1200         }
1201         spin_unlock_bh(&obd->obd_processing_task_lock);
1202         CDEBUG(D_HA, "%s: recovery timer will expire in %u seconds\n",
1203                obd->obd_name, (unsigned)left);
1204 }
1205
1206 static void check_and_start_recovery_timer(struct obd_device *obd,
1207                                            svc_handler_t handler)
1208 {
1209         spin_lock_bh(&obd->obd_processing_task_lock);
1210         if (obd->obd_recovery_handler) { 
1211                 spin_unlock_bh(&obd->obd_processing_task_lock);
1212                 return;
1213         }
1214         CWARN("%s: starting recovery timer\n", obd->obd_name);
1215         obd->obd_recovery_start = cfs_time_current_sec();
1216         /* minimum */
1217         obd->obd_recovery_timeout = OBD_RECOVERY_FACTOR * obd_timeout;
1218         obd->obd_recovery_handler = handler;
1219         cfs_timer_init(&obd->obd_recovery_timer, target_recovery_expired, obd);
1220         spin_unlock_bh(&obd->obd_processing_task_lock);
1221
1222         reset_recovery_timer(obd, obd->obd_recovery_timeout, 0);
1223 }
1224
1225 /* Reset the timer with each new client connection */
1226 /*
1227  * This timer is actually reconnect_timer, which is for making sure 
1228  * the total recovery window is at least as big as my reconnect 
1229  * attempt timing. So the initial recovery time_out will be set to
1230  * OBD_RECOVERY_FACTOR * obd_timeout. If the timeout coming
1231  * from client is bigger than this, then the recovery time_out will
1232  * be extend to make sure the client could be reconnected, in the 
1233  * process, the timeout from the new client should be ignored.
1234  */
1235
1236 static void
1237 target_start_and_reset_recovery_timer(struct obd_device *obd,
1238                                       svc_handler_t handler,
1239                                       struct ptlrpc_request *req,
1240                                       int new_client)
1241 {
1242         int req_timeout = OBD_RECOVERY_FACTOR * 
1243                           lustre_msg_get_timeout(req->rq_reqmsg);
1244
1245         check_and_start_recovery_timer(obd, handler);
1246
1247         if (req_timeout > obd->obd_recovery_timeout && !new_client)
1248                 reset_recovery_timer(obd, req_timeout, 0);
1249 }
1250
1251 static int check_for_next_transno(struct obd_device *obd)
1252 {
1253         struct ptlrpc_request *req;
1254         int wake_up = 0, connected, completed, queue_len, max;
1255         __u64 next_transno, req_transno;
1256
1257         spin_lock_bh(&obd->obd_processing_task_lock);
1258         req = list_entry(obd->obd_recovery_queue.next,
1259                          struct ptlrpc_request, rq_list);
1260         max = obd->obd_max_recoverable_clients;
1261         req_transno = lustre_msg_get_transno(req->rq_reqmsg);
1262         connected = obd->obd_connected_clients;
1263         completed = max - obd->obd_recoverable_clients;
1264         queue_len = obd->obd_requests_queued_for_recovery;
1265         next_transno = obd->obd_next_recovery_transno;
1266
1267         CDEBUG(D_HA,"max: %d, connected: %d, completed: %d, queue_len: %d, "
1268                "req_transno: "LPU64", next_transno: "LPU64"\n",
1269                max, connected, completed, queue_len, req_transno, next_transno);
1270         if (obd->obd_abort_recovery) {
1271                 CDEBUG(D_HA, "waking for aborted recovery\n");
1272                 wake_up = 1;
1273         } else if (!obd->obd_recovering) {
1274                 CDEBUG(D_HA, "waking for completed recovery (?)\n");
1275                 wake_up = 1;
1276         } else if (req_transno == next_transno) {
1277                 CDEBUG(D_HA, "waking for next ("LPD64")\n", next_transno);
1278                 wake_up = 1;
1279         } else if (queue_len + completed == max) {
1280                 CDEBUG(D_ERROR,
1281                        "waking for skipped transno (skip: "LPD64
1282                        ", ql: %d, comp: %d, conn: %d, next: "LPD64")\n",
1283                        next_transno, queue_len, completed, max, req_transno);
1284                 obd->obd_next_recovery_transno = req_transno;
1285                 wake_up = 1;
1286         }
1287         spin_unlock_bh(&obd->obd_processing_task_lock);
1288         LASSERT(lustre_msg_get_transno(req->rq_reqmsg) >= next_transno);
1289         return wake_up;
1290 }
1291
1292 static void process_recovery_queue(struct obd_device *obd)
1293 {
1294         struct ptlrpc_request *req;
1295         int abort_recovery = 0;
1296         struct l_wait_info lwi = { 0 };
1297         ENTRY;
1298
1299         for (;;) {
1300                 spin_lock_bh(&obd->obd_processing_task_lock);
1301                 LASSERT(obd->obd_processing_task == cfs_curproc_pid());
1302                 req = list_entry(obd->obd_recovery_queue.next,
1303                                  struct ptlrpc_request, rq_list);
1304
1305                 if (lustre_msg_get_transno(req->rq_reqmsg) !=
1306                     obd->obd_next_recovery_transno) {
1307                         spin_unlock_bh(&obd->obd_processing_task_lock);
1308                         CDEBUG(D_HA, "Waiting for transno "LPD64" (1st is "
1309                                LPD64", x"LPU64")\n",
1310                                obd->obd_next_recovery_transno,
1311                                lustre_msg_get_transno(req->rq_reqmsg),
1312                                req->rq_xid);
1313                         l_wait_event(obd->obd_next_transno_waitq,
1314                                      check_for_next_transno(obd), &lwi);
1315                         spin_lock_bh(&obd->obd_processing_task_lock);
1316                         abort_recovery = obd->obd_abort_recovery;
1317                         spin_unlock_bh(&obd->obd_processing_task_lock);
1318                         if (abort_recovery) {
1319                                 target_abort_recovery(obd);
1320                                 return;
1321                         }
1322                         continue;
1323                 }
1324                 target_exp_dequeue_req_replay(req);
1325                 list_del_init(&req->rq_list);
1326                 obd->obd_requests_queued_for_recovery--;
1327                 spin_unlock_bh(&obd->obd_processing_task_lock);
1328
1329                 DEBUG_REQ(D_HA, req, "processing: ");
1330                 (void)obd->obd_recovery_handler(req);
1331                 obd->obd_replayed_requests++;
1332                 reset_recovery_timer(obd, OBD_RECOVERY_FACTOR *
1333                        AT_OFF ? obd_timeout :
1334                        at_get(&req->rq_rqbd->rqbd_service->srv_at_estimate), 1);
1335                 /* bug 1580: decide how to properly sync() in recovery */
1336                 //mds_fsync_super(obd->u.obt.obt_sb);
1337                 class_export_put(req->rq_export);
1338                 ptlrpc_req_drop_rs(req);
1339                 OBD_FREE(req->rq_reqmsg, req->rq_reqlen);
1340                 OBD_FREE(req, sizeof *req);
1341                 spin_lock_bh(&obd->obd_processing_task_lock);
1342                 obd->obd_next_recovery_transno++;
1343                 if (list_empty(&obd->obd_recovery_queue)) {
1344                         obd->obd_processing_task = 0;
1345                         spin_unlock_bh(&obd->obd_processing_task_lock);
1346                         break;
1347                 }
1348                 spin_unlock_bh(&obd->obd_processing_task_lock);
1349         }
1350         EXIT;
1351 }
1352
1353 int target_queue_recovery_request(struct ptlrpc_request *req,
1354                                   struct obd_device *obd)
1355 {
1356         struct list_head *tmp;
1357         int inserted = 0;
1358         __u64 transno = lustre_msg_get_transno(req->rq_reqmsg);
1359         struct ptlrpc_request *saved_req;
1360         struct lustre_msg *reqmsg;
1361         int rc = 0;
1362
1363         /* CAVEAT EMPTOR: The incoming request message has been swabbed
1364          * (i.e. buflens etc are in my own byte order), but type-dependent
1365          * buffers (eg mds_body, ost_body etc) have NOT been swabbed. */
1366
1367         if (!transno) {
1368                 CFS_INIT_LIST_HEAD(&req->rq_list);
1369                 DEBUG_REQ(D_HA, req, "not queueing");
1370                 return 1;
1371         }
1372
1373         /* XXX If I were a real man, these LBUGs would be sane cleanups. */
1374         /* XXX just like the request-dup code in queue_final_reply */
1375         OBD_ALLOC(saved_req, sizeof *saved_req);
1376         if (!saved_req)
1377                 LBUG();
1378         OBD_ALLOC(reqmsg, req->rq_reqlen);
1379         if (!reqmsg)
1380                 LBUG();
1381
1382         spin_lock_bh(&obd->obd_processing_task_lock);
1383
1384         /* If we're processing the queue, we want don't want to queue this
1385          * message.
1386          *
1387          * Also, if this request has a transno less than the one we're waiting
1388          * for, we should process it now.  It could (and currently always will)
1389          * be an open request for a descriptor that was opened some time ago.
1390          *
1391          * Also, a resent, replayed request that has already been
1392          * handled will pass through here and be processed immediately.
1393          */
1394         if (obd->obd_processing_task == cfs_curproc_pid() ||
1395             transno < obd->obd_next_recovery_transno) {
1396                 /* Processing the queue right now, don't re-add. */
1397                 LASSERT(list_empty(&req->rq_list));
1398                 spin_unlock_bh(&obd->obd_processing_task_lock);
1399                 GOTO(err_free, rc = 1);
1400         }
1401
1402         if (unlikely(OBD_FAIL_CHECK(OBD_FAIL_TGT_REPLAY_DROP))) {
1403                 spin_unlock_bh(&obd->obd_processing_task_lock);
1404                 GOTO(err_free, rc = 0);
1405         }
1406
1407         memcpy(saved_req, req, sizeof *req);
1408         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1409         req = saved_req;
1410         req->rq_reqmsg = reqmsg;
1411         class_export_get(req->rq_export);
1412         CFS_INIT_LIST_HEAD(&req->rq_list);
1413         CFS_INIT_LIST_HEAD(&req->rq_replay_list);
1414
1415         if (target_exp_enqueue_req_replay(req)) {
1416                 spin_unlock_bh(&obd->obd_processing_task_lock);
1417                 DEBUG_REQ(D_ERROR, req, "dropping resent queued req");
1418                 GOTO(err_exp, rc = 0);
1419         }
1420
1421         /* XXX O(n^2) */
1422         list_for_each(tmp, &obd->obd_recovery_queue) {
1423                 struct ptlrpc_request *reqiter =
1424                         list_entry(tmp, struct ptlrpc_request, rq_list);
1425
1426                 if (lustre_msg_get_transno(reqiter->rq_reqmsg) > transno) {
1427                         list_add_tail(&req->rq_list, &reqiter->rq_list);
1428                         inserted = 1;
1429                         break;
1430                 }
1431
1432                 if (unlikely(lustre_msg_get_transno(reqiter->rq_reqmsg) ==
1433                              transno)) {
1434                         spin_unlock_bh(&obd->obd_processing_task_lock);
1435                         DEBUG_REQ(D_ERROR, req, "dropping replay: transno "
1436                                   "has been claimed by another client");
1437                         target_exp_dequeue_req_replay(req);
1438                         GOTO(err_exp, rc = 0);
1439                 }
1440         }
1441
1442         if (!inserted) {
1443                 list_add_tail(&req->rq_list, &obd->obd_recovery_queue);
1444         }
1445
1446         obd->obd_requests_queued_for_recovery++;
1447
1448         if (obd->obd_processing_task != 0) {
1449                 /* Someone else is processing this queue, we'll leave it to
1450                  * them.
1451                  */
1452                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1453                 spin_unlock_bh(&obd->obd_processing_task_lock);
1454                 return 0;
1455         }
1456
1457         /* Nobody is processing, and we know there's (at least) one to process
1458          * now, so we'll do the honours.
1459          */
1460         obd->obd_processing_task = cfs_curproc_pid();
1461         spin_unlock_bh(&obd->obd_processing_task_lock);
1462
1463         process_recovery_queue(obd);
1464         return 0;
1465
1466 err_exp:
1467         class_export_put(req->rq_export);
1468 err_free:
1469         OBD_FREE(reqmsg, req->rq_reqlen);
1470         OBD_FREE(saved_req, sizeof(*saved_req));
1471         return rc;
1472 }
1473
1474 struct obd_device * target_req2obd(struct ptlrpc_request *req)
1475 {
1476         return req->rq_export->exp_obd;
1477 }
1478
1479 int target_queue_last_replay_reply(struct ptlrpc_request *req, int rc)
1480 {
1481         struct obd_device *obd = target_req2obd(req);
1482         struct ptlrpc_request *saved_req;
1483         struct lustre_msg *reqmsg;
1484         int recovery_done = 0;
1485
1486         LASSERT ((rc == 0) == req->rq_packed_final);
1487
1488         if (!req->rq_packed_final) {
1489                 /* Just like ptlrpc_error, but without the sending. */
1490                 rc = lustre_pack_reply(req, 1, NULL, NULL);
1491                 if (rc)
1492                         return rc;
1493                 req->rq_type = PTL_RPC_MSG_ERR;
1494         }
1495
1496         LASSERT(!req->rq_reply_state->rs_difficult);
1497         LASSERT(list_empty(&req->rq_list));
1498         /* XXX a bit like the request-dup code in queue_recovery_request */
1499         OBD_ALLOC(saved_req, sizeof *saved_req);
1500         if (!saved_req)
1501                 return -ENOMEM;
1502         OBD_ALLOC(reqmsg, req->rq_reqlen);
1503         if (!reqmsg) {
1504                 OBD_FREE(saved_req, sizeof *req);
1505                 return -ENOMEM;
1506         }
1507         *saved_req = *req;
1508         memcpy(reqmsg, req->rq_reqmsg, req->rq_reqlen);
1509
1510         /* Don't race cleanup */
1511         spin_lock_bh(&obd->obd_processing_task_lock);
1512         if (obd->obd_stopping) {
1513                 spin_unlock_bh(&obd->obd_processing_task_lock);
1514                 OBD_FREE(reqmsg, req->rq_reqlen);
1515                 OBD_FREE(saved_req, sizeof *req);
1516                 req->rq_status = -ENOTCONN;
1517                 /* rv is ignored anyhow */
1518                 return -ENOTCONN;
1519         }
1520         ptlrpc_rs_addref(req->rq_reply_state);  /* +1 ref for saved reply */
1521         req = saved_req;
1522         req->rq_reqmsg = reqmsg;
1523         class_export_get(req->rq_export);
1524         list_add(&req->rq_list, &obd->obd_delayed_reply_queue);
1525
1526         /* only count the first "replay over" request from each
1527            export */
1528         if (req->rq_export->exp_replay_needed) {
1529                 --obd->obd_recoverable_clients;
1530                 
1531                 spin_lock(&req->rq_export->exp_lock);
1532                 req->rq_export->exp_replay_needed = 0;
1533                 spin_unlock(&req->rq_export->exp_lock);
1534         }
1535         recovery_done = (obd->obd_recoverable_clients == 0);
1536         spin_unlock_bh(&obd->obd_processing_task_lock);
1537
1538         OBD_RACE(OBD_FAIL_LDLM_RECOV_CLIENTS);
1539         if (recovery_done) {
1540                 spin_lock_bh(&obd->obd_processing_task_lock);
1541                 obd->obd_recovering = obd->obd_abort_recovery = 0;
1542                 target_cancel_recovery_timer(obd);
1543                 spin_unlock_bh(&obd->obd_processing_task_lock);
1544
1545                 target_finish_recovery(obd);
1546                 CDEBUG(D_HA, "%s: recovery complete\n",
1547                        obd_uuid2str(&obd->obd_uuid));
1548         } else {
1549                 CWARN("%s: %d recoverable clients remain\n",
1550                        obd->obd_name, obd->obd_recoverable_clients);
1551                 cfs_waitq_signal(&obd->obd_next_transno_waitq);
1552         }
1553
1554         return 1;
1555 }
1556
1557 static inline struct ldlm_pool *ldlm_exp2pl(struct obd_export *exp)
1558 {
1559         LASSERT(exp != NULL);
1560         return &exp->exp_obd->obd_namespace->ns_pool;
1561 }
1562
1563 int target_pack_pool_reply(struct ptlrpc_request *req)
1564 {
1565         struct obd_device *obd;
1566         ENTRY;
1567    
1568         /* 
1569          * Check that we still have all structures alive as this may 
1570          * be some late rpc in shutdown time.
1571          */
1572         if (unlikely(!req->rq_export || !req->rq_export->exp_obd ||
1573                      !exp_connect_lru_resize(req->rq_export))) {
1574                 lustre_msg_set_slv(req->rq_repmsg, 0);
1575                 lustre_msg_set_limit(req->rq_repmsg, 0);
1576                 RETURN(0);
1577         }
1578
1579         /* 
1580          * OBD is alive here as export is alive, which we checked above. 
1581          */
1582         obd = req->rq_export->exp_obd;
1583
1584         read_lock(&obd->obd_pool_lock);
1585         lustre_msg_set_slv(req->rq_repmsg, obd->obd_pool_slv);
1586         lustre_msg_set_limit(req->rq_repmsg, obd->obd_pool_limit);
1587         read_unlock(&obd->obd_pool_lock);
1588
1589         RETURN(0);
1590 }
1591
1592 int
1593 target_send_reply_msg (struct ptlrpc_request *req, int rc, int fail_id)
1594 {
1595         if (OBD_FAIL_CHECK(fail_id | OBD_FAIL_ONCE)) {
1596                 obd_fail_loc |= OBD_FAIL_ONCE | OBD_FAILED;
1597                 DEBUG_REQ(D_ERROR, req, "dropping reply");
1598                 return (-ECOMM);
1599         }
1600
1601         if (rc) {
1602                 DEBUG_REQ(D_ERROR, req, "processing error (%d)", rc);
1603                 req->rq_status = rc;
1604                 return (ptlrpc_send_error(req, 1));
1605         } else {
1606                 DEBUG_REQ(D_NET, req, "sending reply");
1607         }
1608
1609         return (ptlrpc_send_reply(req, PTLRPC_REPLY_MAYBE_DIFFICULT));
1610 }
1611
1612 void
1613 target_send_reply(struct ptlrpc_request *req, int rc, int fail_id)
1614 {
1615         int                        netrc;
1616         struct ptlrpc_reply_state *rs;
1617         struct obd_device         *obd;
1618         struct obd_export         *exp;
1619         struct ptlrpc_service     *svc;
1620
1621         svc = req->rq_rqbd->rqbd_service;
1622         rs = req->rq_reply_state;
1623         if (rs == NULL || !rs->rs_difficult) {
1624                 /* no notifiers */
1625                 target_send_reply_msg (req, rc, fail_id);
1626                 return;
1627         }
1628
1629         /* must be an export if locks saved */
1630         LASSERT (req->rq_export != NULL);
1631         /* req/reply consistent */
1632         LASSERT (rs->rs_service == svc);
1633
1634         /* "fresh" reply */
1635         LASSERT (!rs->rs_scheduled);
1636         LASSERT (!rs->rs_scheduled_ever);
1637         LASSERT (!rs->rs_handled);
1638         LASSERT (!rs->rs_on_net);
1639         LASSERT (rs->rs_export == NULL);
1640         LASSERT (list_empty(&rs->rs_obd_list));
1641         LASSERT (list_empty(&rs->rs_exp_list));
1642
1643         exp = class_export_get (req->rq_export);
1644         obd = exp->exp_obd;
1645
1646         /* disable reply scheduling onto srv_reply_queue while I'm setting up */
1647         rs->rs_scheduled = 1;
1648         rs->rs_on_net    = 1;
1649         rs->rs_xid       = req->rq_xid;
1650         rs->rs_transno   = req->rq_transno;
1651         rs->rs_export    = exp;
1652
1653         spin_lock(&obd->obd_uncommitted_replies_lock);
1654
1655         if (rs->rs_transno > obd->obd_last_committed) {
1656                 /* not committed already */
1657                 list_add_tail (&rs->rs_obd_list,
1658                                &obd->obd_uncommitted_replies);
1659         }
1660
1661         spin_unlock (&obd->obd_uncommitted_replies_lock);
1662         spin_lock (&exp->exp_lock);
1663
1664         list_add_tail (&rs->rs_exp_list, &exp->exp_outstanding_replies);
1665
1666         spin_unlock(&exp->exp_lock);
1667
1668         netrc = target_send_reply_msg (req, rc, fail_id);
1669
1670         spin_lock(&svc->srv_lock);
1671
1672         svc->srv_n_difficult_replies++;
1673
1674         if (netrc != 0) {
1675                 /* error sending: reply is off the net.  Also we need +1
1676                  * reply ref until ptlrpc_server_handle_reply() is done
1677                  * with the reply state (if the send was successful, there
1678                  * would have been +1 ref for the net, which
1679                  * reply_out_callback leaves alone) */
1680                 rs->rs_on_net = 0;
1681                 ptlrpc_rs_addref(rs);
1682                 atomic_inc (&svc->srv_outstanding_replies);
1683         }
1684
1685         if (!rs->rs_on_net ||                   /* some notifier */
1686             list_empty(&rs->rs_exp_list) ||     /* completed already */
1687             list_empty(&rs->rs_obd_list)) {
1688                 list_add_tail (&rs->rs_list, &svc->srv_reply_queue);
1689                 cfs_waitq_signal (&svc->srv_waitq);
1690         } else {
1691                 list_add (&rs->rs_list, &svc->srv_active_replies);
1692                 rs->rs_scheduled = 0;           /* allow notifier to schedule */
1693         }
1694
1695         spin_unlock(&svc->srv_lock);
1696 }
1697
1698 int target_handle_ping(struct ptlrpc_request *req)
1699 {
1700         obd_ping(req->rq_export);
1701         return lustre_pack_reply(req, 1, NULL, NULL);
1702 }
1703
1704 void target_committed_to_req(struct ptlrpc_request *req)
1705 {
1706         struct obd_device *obd = req->rq_export->exp_obd;
1707
1708         if (!obd->obd_no_transno && req->rq_repmsg != NULL)
1709                 lustre_msg_set_last_committed(req->rq_repmsg,
1710                                               obd->obd_last_committed);
1711         else
1712                 DEBUG_REQ(D_IOCTL, req, "not sending last_committed update (%d/"
1713                           "%d)", obd->obd_no_transno, req->rq_repmsg == NULL);
1714
1715         CDEBUG(D_INFO, "last_committed x"LPU64", this req x"LPU64"\n",
1716                obd->obd_last_committed, req->rq_xid);
1717 }
1718
1719 EXPORT_SYMBOL(target_committed_to_req);
1720
1721 #ifdef HAVE_QUOTA_SUPPORT
1722 int target_handle_qc_callback(struct ptlrpc_request *req)
1723 {
1724         struct obd_quotactl *oqctl;
1725         struct client_obd *cli = &req->rq_export->exp_obd->u.cli;
1726
1727         oqctl = lustre_swab_reqbuf(req, REQ_REC_OFF, sizeof(*oqctl),
1728                                    lustre_swab_obd_quotactl);
1729         if (oqctl == NULL) {
1730                 CERROR("Can't unpack obd_quotactl\n");
1731                 RETURN(-EPROTO);
1732         }
1733
1734         cli->cl_qchk_stat = oqctl->qc_stat;
1735
1736         return 0;
1737 }
1738
1739 int target_handle_dqacq_callback(struct ptlrpc_request *req)
1740 {
1741 #ifdef __KERNEL__
1742         struct obd_device *obd = req->rq_export->exp_obd;
1743         struct obd_device *master_obd;
1744         struct lustre_quota_ctxt *qctxt;
1745         struct qunit_data *qdata = NULL;
1746         int rc = 0;
1747         int repsize[2] = { sizeof(struct ptlrpc_body), 0 };
1748         ENTRY;
1749
1750         if (OBD_FAIL_CHECK_ONCE(OBD_FAIL_MDS_DROP_QUOTA_REQ))
1751                 RETURN(rc);
1752
1753         repsize[1] = quota_get_qunit_data_size(req->rq_export->
1754                                                exp_connect_flags);
1755
1756         rc = lustre_pack_reply(req, 2, repsize, NULL);
1757         if (rc)
1758                 RETURN(rc);
1759
1760         LASSERT(req->rq_export);
1761
1762         /* there are three forms of qunit(historic causes), so we need to
1763          * adjust qunits from slaves to the same form here */
1764         OBD_ALLOC(qdata, sizeof(struct qunit_data));
1765         if (!qdata)
1766                 RETURN(-ENOMEM);
1767         rc = quota_get_qdata(req, qdata, QUOTA_REQUEST, QUOTA_EXPORT);
1768         if (rc < 0) {
1769                 CDEBUG(D_ERROR, "Can't unpack qunit_data\n");
1770                 GOTO(out, rc = -EPROTO);
1771         }
1772
1773         /* we use the observer */
1774         LASSERT(obd->obd_observer && obd->obd_observer->obd_observer);
1775         master_obd = obd->obd_observer->obd_observer;
1776         qctxt = &master_obd->u.obt.obt_qctxt;
1777
1778         LASSERT(qctxt->lqc_handler);
1779         rc = qctxt->lqc_handler(master_obd, qdata,
1780                                 lustre_msg_get_opc(req->rq_reqmsg));
1781         if (rc && rc != -EDQUOT)
1782                 CDEBUG(rc == -EBUSY  ? D_QUOTA : D_ERROR,
1783                        "dqacq failed! (rc:%d)\n", rc);
1784         req->rq_status = rc;
1785
1786         /* there are three forms of qunit(historic causes), so we need to
1787          * adjust the same form to different forms slaves needed */
1788         rc = quota_copy_qdata(req, qdata, QUOTA_REPLY, QUOTA_EXPORT);
1789         if (rc < 0) {
1790                 CDEBUG(D_ERROR, "Can't pack qunit_data\n");
1791                 GOTO(out, rc = -EPROTO);
1792         }
1793
1794         /* Block the quota req. b=14840 */
1795         OBD_FAIL_TIMEOUT(OBD_FAIL_MDS_BLOCK_QUOTA_REQ, obd_timeout);
1796
1797         rc = ptlrpc_reply(req);
1798 out:
1799         OBD_FREE(qdata, sizeof(struct qunit_data));
1800         RETURN(rc);
1801 #else
1802         return 0;
1803 #endif /* !__KERNEL__ */
1804 }
1805 #endif /* HAVE_QUOTA_SUPPORT */
1806
1807 ldlm_mode_t lck_compat_array[] = {
1808         [LCK_EX] LCK_COMPAT_EX,
1809         [LCK_PW] LCK_COMPAT_PW,
1810         [LCK_PR] LCK_COMPAT_PR,
1811         [LCK_CW] LCK_COMPAT_CW,
1812         [LCK_CR] LCK_COMPAT_CR,
1813         [LCK_NL] LCK_COMPAT_NL,
1814         [LCK_GROUP] LCK_COMPAT_GROUP
1815 };