Whamcloud - gitweb
LU-1302 llog: modify llog_write/llog_add to support OSD
[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 #ifdef __KERNEL__
48 #include <libcfs/libcfs.h>
49 #else
50 #include <liblustre.h>
51 #endif
52
53 #include <obd_class.h>
54 #include <lustre_log.h>
55 #include <libcfs/list.h>
56 #include <lvfs.h>
57 #include <lustre_fsfilt.h>
58
59 #ifdef __KERNEL__
60 int llog_origin_connect(struct llog_ctxt *ctxt,
61                         struct llog_logid *logid, struct llog_gen *gen,
62                         struct obd_uuid *uuid)
63 {
64         struct llog_gen_rec    *lgr;
65         struct ptlrpc_request  *req;
66         struct llogd_conn_body *req_body;
67         struct inode* inode = ctxt->loc_handle->lgh_file->f_dentry->d_inode;
68         void *handle;
69         int rc, rc1;
70
71         ENTRY;
72
73         if (cfs_list_empty(&ctxt->loc_handle->u.chd.chd_head)) {
74                 CDEBUG(D_HA, "there is no record related to ctxt %p\n", ctxt);
75                 RETURN(0);
76         }
77
78         /* FIXME what value for gen->conn_cnt */
79         llog_gen_init(ctxt);
80
81         /* first add llog_gen_rec */
82         OBD_ALLOC_PTR(lgr);
83         if (!lgr)
84                 RETURN(-ENOMEM);
85         lgr->lgr_hdr.lrh_len = lgr->lgr_tail.lrt_len = sizeof(*lgr);
86         lgr->lgr_hdr.lrh_type = LLOG_GEN_REC;
87
88         handle = fsfilt_start_log(ctxt->loc_exp->exp_obd, inode, 
89                                   FSFILT_OP_CANCEL_UNLINK, NULL, 1);
90         if (IS_ERR(handle)) {
91                CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
92                OBD_FREE(lgr, sizeof(*lgr));
93                rc = PTR_ERR(handle);
94                RETURN(rc);
95         }
96
97         lgr->lgr_gen = ctxt->loc_gen;
98         rc = llog_obd_add(NULL, ctxt, &lgr->lgr_hdr, NULL, NULL, 1);
99         OBD_FREE_PTR(lgr);
100         rc1 = fsfilt_commit(ctxt->loc_exp->exp_obd, inode, handle, 0);
101         if (rc != 1 || rc1 != 0) {
102                 rc = (rc != 1) ? rc : rc1;
103                 RETURN(rc);
104         }
105
106         LASSERT(ctxt->loc_imp);
107         req = ptlrpc_request_alloc_pack(ctxt->loc_imp, &RQF_LLOG_ORIGIN_CONNECT,
108                                         LUSTRE_LOG_VERSION,
109                                         LLOG_ORIGIN_CONNECT);
110         if (req == NULL)
111                 RETURN(-ENOMEM);
112
113         CDEBUG(D_OTHER, "%s mount_count "LPU64", connection count "LPU64"\n",
114                ctxt->loc_exp->exp_obd->obd_type->typ_name,
115                ctxt->loc_gen.mnt_cnt, ctxt->loc_gen.conn_cnt);
116
117         req_body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
118         req_body->lgdc_gen = ctxt->loc_gen;
119         req_body->lgdc_logid = ctxt->loc_handle->lgh_id;
120         req_body->lgdc_ctxt_idx = ctxt->loc_idx + 1;
121         ptlrpc_request_set_replen(req);
122
123         req->rq_no_resend = req->rq_no_delay = 1;
124         rc = ptlrpc_queue_wait(req);
125         ptlrpc_req_finished(req);
126
127         RETURN(rc);
128 }
129 EXPORT_SYMBOL(llog_origin_connect);
130
131 int llog_handle_connect(struct ptlrpc_request *req)
132 {
133         struct obd_device *obd = req->rq_export->exp_obd;
134         struct llogd_conn_body *req_body;
135         struct llog_ctxt *ctxt;
136         int rc;
137         ENTRY;
138
139         req_body = req_capsule_client_get(&req->rq_pill, &RMF_LLOGD_CONN_BODY);
140
141         ctxt = llog_get_context(obd, req_body->lgdc_ctxt_idx);
142         rc = llog_connect(ctxt, &req_body->lgdc_logid,
143                           &req_body->lgdc_gen, NULL);
144
145         llog_ctxt_put(ctxt);
146         if (rc != 0)
147                 CERROR("failed at llog_relp_connect\n");
148
149         RETURN(rc);
150 }
151 EXPORT_SYMBOL(llog_handle_connect);
152
153 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp)
154 {
155         ENTRY;
156
157         LASSERT(ctxt);
158         cfs_mutex_lock(&ctxt->loc_mutex);
159         if (ctxt->loc_imp != imp) {
160                 if (ctxt->loc_imp) {
161                         CWARN("changing the import %p - %p\n",
162                               ctxt->loc_imp, imp);
163                         class_import_put(ctxt->loc_imp);
164                 }
165                 ctxt->loc_imp = class_import_get(imp);
166         }
167         cfs_mutex_unlock(&ctxt->loc_mutex);
168         RETURN(0);
169 }
170 EXPORT_SYMBOL(llog_receptor_accept);
171
172 #else /* !__KERNEL__ */
173
174 int llog_origin_connect(struct llog_ctxt *ctxt,
175                         struct llog_logid *logid, struct llog_gen *gen,
176                         struct obd_uuid *uuid)
177 {
178         return 0;
179 }
180 #endif
181
182 int llog_initiator_connect(struct llog_ctxt *ctxt)
183 {
184         struct obd_import *new_imp;
185         ENTRY;
186
187         LASSERT(ctxt);
188         new_imp = ctxt->loc_obd->u.cli.cl_import;
189         LASSERTF(ctxt->loc_imp == NULL || ctxt->loc_imp == new_imp,
190                  "%p - %p\n", ctxt->loc_imp, new_imp);
191         cfs_mutex_lock(&ctxt->loc_mutex);
192         if (ctxt->loc_imp != new_imp) {
193                 if (ctxt->loc_imp)
194                         class_import_put(ctxt->loc_imp);
195                 ctxt->loc_imp = class_import_get(new_imp);
196         }
197         cfs_mutex_unlock(&ctxt->loc_mutex);
198         RETURN(0);
199 }
200 EXPORT_SYMBOL(llog_initiator_connect);