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