Whamcloud - gitweb
Branch b1_8
[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  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Andreas Dilger <adilger@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  * OST<->MDS recovery logging infrastructure.
26  *
27  * Invariants in implementation:
28  * - we do not share logs among different OST<->MDS connections, so that
29  *   if an OST or MDS fails it need only look at log(s) relevant to itself
30  */
31
32 #define DEBUG_SUBSYSTEM S_LOG
33
34 #ifndef EXPORT_SYMTAB
35 #define EXPORT_SYMTAB
36 #endif
37
38 #ifdef __KERNEL__
39 #include <libcfs/libcfs.h>
40 #else
41 #include <liblustre.h>
42 #endif
43
44 #include <obd_class.h>
45 #include <lustre_log.h>
46 #include <libcfs/list.h>
47 #include <lvfs.h>
48 #include <lustre_fsfilt.h>
49
50 #ifdef __KERNEL__
51 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
52                         struct llog_logid *logid, struct llog_gen *gen,
53                         struct obd_uuid *uuid)
54 {
55         struct llog_gen_rec *lgr;
56         struct obd_import *imp;
57         struct ptlrpc_request *request;
58         struct llogd_conn_body *req_body;
59         int size[2] = { sizeof(struct ptlrpc_body),
60                         sizeof(struct llogd_conn_body) };
61         struct inode* inode = ctxt->loc_handle->lgh_file->f_dentry->d_inode;
62         void *handle;
63         int rc, rc1;
64         ENTRY;
65
66         if (list_empty(&ctxt->loc_handle->u.chd.chd_head)) {
67                 CDEBUG(D_HA, "there is no record related to ctxt %p\n", ctxt);
68                 RETURN(0);
69         }
70
71         /* FIXME what value for gen->conn_cnt */
72         LLOG_GEN_INC(ctxt->loc_gen);
73
74         /* first add llog_gen_rec */
75         OBD_ALLOC(lgr, sizeof(*lgr));
76         if (!lgr)
77                 RETURN(-ENOMEM);
78         lgr->lgr_hdr.lrh_len = lgr->lgr_tail.lrt_len = sizeof(*lgr);
79         lgr->lgr_hdr.lrh_type = LLOG_GEN_REC;
80
81         handle = fsfilt_start_log(ctxt->loc_exp->exp_obd, inode, 
82                                   FSFILT_OP_CANCEL_UNLINK, NULL, 1);
83        
84         if (IS_ERR(handle)) {
85                 CERROR("fsfilt_start failed: %ld\n", PTR_ERR(handle));
86                 OBD_FREE(lgr, sizeof(*lgr));
87                 rc = PTR_ERR(handle);
88                 RETURN(rc);
89         }
90         lgr->lgr_gen = ctxt->loc_gen;
91         rc = llog_add(ctxt, &lgr->lgr_hdr, NULL, NULL, 1);
92         OBD_FREE(lgr, sizeof(*lgr));
93         
94         rc1 = fsfilt_commit(ctxt->loc_exp->exp_obd, inode, handle, 0);
95         if (rc != 1 || rc1 != 0) {
96                 rc = (rc != 1) ? rc : rc1;
97                 RETURN(rc);
98         }
99
100         LASSERT(ctxt->loc_imp);
101         imp = ctxt->loc_imp;
102
103         request = ptlrpc_prep_req(imp, LUSTRE_LOG_VERSION,
104                                   LLOG_ORIGIN_CONNECT, 2, size, NULL);
105         if (!request)
106                 RETURN(-ENOMEM);
107
108         req_body = lustre_msg_buf(request->rq_reqmsg, REQ_REC_OFF,
109                                   sizeof(*req_body));
110
111         req_body->lgdc_gen = ctxt->loc_gen;
112         req_body->lgdc_logid = ctxt->loc_handle->lgh_id;
113         req_body->lgdc_ctxt_idx = ctxt->loc_idx + 1;
114         ptlrpc_req_set_repsize(request, 1, NULL);
115
116         rc = ptlrpc_queue_wait(request);
117         ptlrpc_req_finished(request);
118
119         RETURN(rc);
120 }
121 EXPORT_SYMBOL(llog_origin_connect);
122
123 int llog_handle_connect(struct ptlrpc_request *req)
124 {
125         struct obd_device *obd = req->rq_export->exp_obd;
126         struct llogd_conn_body *req_body;
127         struct llog_ctxt *ctxt;
128         int rc;
129         ENTRY;
130
131         req_body = lustre_msg_buf(req->rq_reqmsg, REQ_REC_OFF,
132                                   sizeof(*req_body));
133
134         ctxt = llog_get_context(obd, req_body->lgdc_ctxt_idx);
135         rc = llog_connect(ctxt, 1, &req_body->lgdc_logid,
136                           &req_body->lgdc_gen, NULL);
137
138         llog_ctxt_put(ctxt);
139         if (rc != 0)
140                 CERROR("failed at llog_relp_connect\n");
141
142         RETURN(rc);
143 }
144 EXPORT_SYMBOL(llog_handle_connect);
145
146 int llog_receptor_accept(struct llog_ctxt *ctxt, struct obd_import *imp)
147 {
148         ENTRY;
149         LASSERT(ctxt);
150         mutex_down(&ctxt->loc_sem);
151         if (ctxt->loc_imp != imp) {
152                 CWARN("changing the import %p - %p\n", ctxt->loc_imp, imp);
153                 if (ctxt->loc_imp)
154                         class_import_put(ctxt->loc_imp);
155                 ctxt->loc_imp = class_import_get(imp);
156         }
157         mutex_up(&ctxt->loc_sem);
158         RETURN(0);
159 }
160 EXPORT_SYMBOL(llog_receptor_accept);
161
162 #else /* !__KERNEL__ */
163
164 int llog_origin_connect(struct llog_ctxt *ctxt, int count,
165                         struct llog_logid *logid, struct llog_gen *gen,
166                         struct obd_uuid *uuid)
167 {
168         return 0;
169 }
170 #endif
171
172 int llog_initiator_connect(struct llog_ctxt *ctxt)
173 {
174         struct obd_import *new_imp;
175         ENTRY;
176         LASSERT(ctxt);
177         new_imp = ctxt->loc_obd->u.cli.cl_import;
178         mutex_down(&ctxt->loc_sem);
179         if (ctxt->loc_imp != new_imp) {
180                 if (ctxt->loc_imp)
181                         class_import_put(ctxt->loc_imp);
182                 ctxt->loc_imp = class_import_get(new_imp);
183         }
184         mutex_up(&ctxt->loc_sem);
185         RETURN(0);
186 }
187 EXPORT_SYMBOL(llog_initiator_connect);