Whamcloud - gitweb
b=17305
[fs/lustre-release.git] / lustre / ptlrpc / llog_net.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/llog_net.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  *
40  * Invariants in implementation:
41  * - we do not share logs among different OST<->MDS connections, so that
42  *   if an OST or MDS fails it need only look at log(s) relevant to itself
43  *
44  * Author: Andreas Dilger <adilger@clusterfs.com>
45  */
46
47 #define DEBUG_SUBSYSTEM S_LOG
48
49 #ifndef EXPORT_SYMTAB
50 #define EXPORT_SYMTAB
51 #endif
52
53 #ifdef __KERNEL__
54 #include <libcfs/libcfs.h>
55 #else
56 #include <liblustre.h>
57 #endif
58
59 #include <obd_class.h>
60 #include <lustre_log.h>
61 #include <libcfs/list.h>
62 #include <lvfs.h>
63 #include <lustre_fsfilt.h>
64
65 #ifdef __KERNEL__
66 int llog_origin_connect(struct llog_ctxt *ctxt,
67                         struct llog_logid *logid, struct llog_gen *gen,
68                         struct obd_uuid *uuid)
69 {
70         struct llog_gen_rec    *lgr;
71         struct ptlrpc_request  *req;
72         struct llogd_conn_body *req_body;
73         struct inode* inode = ctxt->loc_handle->lgh_file->f_dentry->d_inode;
74         void *handle;
75         int rc, rc1;
76
77         ENTRY;
78
79         if (list_empty(&ctxt->loc_handle->u.chd.chd_head)) {
80                 CDEBUG(D_HA, "there is no record related to ctxt %p\n", ctxt);
81                 RETURN(0);
82         }
83
84         /* FIXME what value for gen->conn_cnt */
85         LLOG_GEN_INC(ctxt->loc_gen);
86
87         /* first add llog_gen_rec */
88         OBD_ALLOC_PTR(lgr);
89         if (!lgr)
90                 RETURN(-ENOMEM);
91         lgr->lgr_hdr.lrh_len = lgr->lgr_tail.lrt_len = sizeof(*lgr);
92         lgr->lgr_hdr.lrh_type = LLOG_GEN_REC;
93
94         handle = fsfilt_start_log(ctxt->loc_exp->exp_obd, inode, 
95                                   FSFILT_OP_CANCEL_UNLINK, NULL, 1);
96         if (IS_ERR(handle)) {
97                CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
98                OBD_FREE(lgr, sizeof(*lgr));
99                rc = PTR_ERR(handle);
100                RETURN(rc);
101         }
102         
103         lgr->lgr_gen = ctxt->loc_gen;
104         rc = llog_add(ctxt, &lgr->lgr_hdr, NULL, NULL, 1);
105         OBD_FREE_PTR(lgr);
106         rc1 = fsfilt_commit(ctxt->loc_exp->exp_obd, inode, handle, 0);
107         if (rc != 1 || rc1 != 0) {
108                 rc = (rc != 1) ? rc : rc1;
109                 RETURN(rc);
110         }
111
112         LASSERT(ctxt->loc_imp);
113         req = ptlrpc_request_alloc_pack(ctxt->loc_imp, &RQF_LLOG_ORIGIN_CONNECT,
114                                         LUSTRE_LOG_VERSION,
115                                         LLOG_ORIGIN_CONNECT);
116         if (req == NULL)
117                 RETURN(-ENOMEM);
118
119         req_body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
120         req_body->lgdc_gen = ctxt->loc_gen;
121         req_body->lgdc_logid = ctxt->loc_handle->lgh_id;
122         req_body->lgdc_ctxt_idx = ctxt->loc_idx + 1;
123         ptlrpc_request_set_replen(req);
124
125         rc = ptlrpc_queue_wait(req);
126         ptlrpc_req_finished(req);
127
128         RETURN(rc);
129 }
130 EXPORT_SYMBOL(llog_origin_connect);
131
132 int llog_handle_connect(struct ptlrpc_request *req)
133 {
134         struct obd_device *obd = req->rq_export->exp_obd;
135         struct llogd_conn_body *req_body;
136         struct llog_ctxt *ctxt;
137         int rc;
138         ENTRY;
139
140         req_body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
141
142         ctxt = llog_get_context(obd, req_body->lgdc_ctxt_idx);
143         rc = llog_connect(ctxt, &req_body->lgdc_logid,
144                           &req_body->lgdc_gen, NULL);
145
146         llog_ctxt_put(ctxt);
147         if (rc != 0)
148                 CERROR("failed at llog_relp_connect\n");
149
150         RETURN(rc);
151 }
152 EXPORT_SYMBOL(llog_handle_connect);
153
154 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp)
155 {
156         ENTRY;
157
158         LASSERT(ctxt);
159         mutex_down(&ctxt->loc_sem);
160         if (ctxt->loc_imp != imp) {
161                 if (ctxt->loc_imp) {
162                         CWARN("changing the import %p - %p\n",
163                               ctxt->loc_imp, imp);
164                         class_import_put(ctxt->loc_imp);
165                 }
166                 ctxt->loc_imp = class_import_get(imp);
167         }
168         mutex_up(&ctxt->loc_sem);
169         RETURN(0);
170 }
171 EXPORT_SYMBOL(llog_receptor_accept);
172
173 #else /* !__KERNEL__ */
174
175 int llog_origin_connect(struct llog_ctxt *ctxt,
176                         struct llog_logid *logid, struct llog_gen *gen,
177                         struct obd_uuid *uuid)
178 {
179         return 0;
180 }
181 #endif
182
183 int llog_initiator_connect(struct llog_ctxt *ctxt)
184 {
185         struct obd_import *new_imp;
186         ENTRY;
187
188         LASSERT(ctxt);
189         new_imp = ctxt->loc_obd->u.cli.cl_import;
190         LASSERTF(ctxt->loc_imp == NULL || ctxt->loc_imp == new_imp,
191                  "%p - %p\n", ctxt->loc_imp, new_imp);
192         mutex_down(&ctxt->loc_sem);
193         if (ctxt->loc_imp != new_imp) {
194                 if (ctxt->loc_imp)
195                         class_import_put(ctxt->loc_imp);
196                 ctxt->loc_imp = class_import_get(new_imp);
197         }
198         mutex_up(&ctxt->loc_sem);
199         RETURN(0);
200 }
201 EXPORT_SYMBOL(llog_initiator_connect);