Whamcloud - gitweb
LU-1302 llog: pass lu_env as parametr in llog functions
[fs/lustre-release.git] / lustre / lov / lov_log.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/lov/lov_log.c
35  *
36  * Author: Phil Schwan <phil@clusterfs.com>
37  * Author: Peter Braam <braam@clusterfs.com>
38  * Author: Mike Shaver <shaver@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_LOV
42 #ifdef __KERNEL__
43 #include <libcfs/libcfs.h>
44 #else
45 #include <liblustre.h>
46 #endif
47
48 #include <obd_support.h>
49 #include <lustre_lib.h>
50 #include <lustre_net.h>
51 #include <lustre/lustre_idl.h>
52 #include <lustre_dlm.h>
53 #include <lustre_mds.h>
54 #include <obd_class.h>
55 #include <obd_lov.h>
56 #include <obd_ost.h>
57 #include <lprocfs_status.h>
58
59 #include "lov_internal.h"
60
61 /* Add log records for each OSC that this object is striped over, and return
62  * cookies for each one.  We _would_ have nice abstraction here, except that
63  * we need to keep cookies in stripe order, even if some are NULL, so that
64  * the right cookies are passed back to the right OSTs at the client side.
65  * Unset cookies should be all-zero (which will never occur naturally). */
66 static int lov_llog_origin_add(const struct lu_env *env,
67                                struct llog_ctxt *ctxt,
68                                struct llog_rec_hdr *rec,
69                                struct lov_stripe_md *lsm,
70                                struct llog_cookie *logcookies, int numcookies)
71 {
72         struct obd_device *obd = ctxt->loc_obd;
73         struct lov_obd *lov = &obd->u.lov;
74         int i, rc = 0, cookies = 0;
75         ENTRY;
76
77         LASSERTF(logcookies && numcookies >= lsm->lsm_stripe_count,
78                  "logcookies %p, numcookies %d lsm->lsm_stripe_count %d \n",
79                  logcookies, numcookies, lsm->lsm_stripe_count);
80
81         for (i = 0; i < lsm->lsm_stripe_count; i++) {
82                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
83                 struct obd_device *child =
84                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd;
85                 struct llog_ctxt *cctxt = llog_get_context(child, ctxt->loc_idx);
86
87                 /* fill mds unlink/setattr log record */
88                 switch (rec->lrh_type) {
89                 case MDS_UNLINK_REC: {
90                         struct llog_unlink_rec *lur = (struct llog_unlink_rec *)rec;
91                         lur->lur_oid = loi->loi_id;
92                         lur->lur_oseq = loi->loi_seq;
93                         break;
94                 }
95                 case MDS_SETATTR64_REC: {
96                         struct llog_setattr64_rec *lsr = (struct llog_setattr64_rec *)rec;
97                         lsr->lsr_oid = loi->loi_id;
98                         lsr->lsr_oseq = loi->loi_seq;
99                         break;
100                 }
101                 default:
102                         break;
103                 }
104                 /* inject error in llog_add() below */
105                 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_FAIL_LOV_LOG_ADD)) {
106                         llog_ctxt_put(cctxt);
107                         cctxt = NULL;
108                 }
109                 rc = llog_add(env, cctxt, rec, NULL, logcookies + cookies,
110                               numcookies - cookies);
111                 llog_ctxt_put(cctxt);
112                 if (rc < 0) {
113                         CERROR("Can't add llog (rc = %d) for stripe %d\n",
114                                rc, cookies);
115                         memset(logcookies + cookies, 0,
116                                sizeof(struct llog_cookie));
117                         rc = 1; /* skip this cookie */
118                 }
119                 /* Note that rc is always 1 if llog_add was successful */
120                 cookies += rc;
121         }
122         RETURN(cookies);
123 }
124
125 static int lov_llog_origin_connect(struct llog_ctxt *ctxt,
126                                    struct llog_logid *logid,
127                                    struct llog_gen *gen,
128                                    struct obd_uuid *uuid)
129 {
130         struct obd_device *obd = ctxt->loc_obd;
131         struct lov_obd *lov = &obd->u.lov;
132         int i, rc = 0, err = 0;
133         ENTRY;
134
135         obd_getref(obd);
136         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
137                 struct obd_device *child;
138                 struct llog_ctxt *cctxt;
139
140                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
141                         continue;
142                 if (uuid && !obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
143                         continue;
144                 CDEBUG(D_CONFIG, "connect %d/%d\n", i, lov->desc.ld_tgt_count);
145                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
146                 cctxt = llog_get_context(child, ctxt->loc_idx);
147                 rc = llog_connect(cctxt, logid, gen, uuid);
148                 llog_ctxt_put(cctxt);
149
150                 if (rc) {
151                         CERROR("error osc_llog_connect tgt %d (%d)\n", i, rc);
152                         if (!err)
153                                 err = rc;
154                 }
155         }
156         obd_putref(obd);
157
158         RETURN(err);
159 }
160
161 /* the replicators commit callback */
162 static int lov_llog_repl_cancel(const struct lu_env *env,
163                                 struct llog_ctxt *ctxt,
164                                 struct lov_stripe_md *lsm,
165                                 int count, struct llog_cookie *cookies,
166                                 int flags)
167 {
168         struct lov_obd *lov;
169         struct obd_device *obd = ctxt->loc_obd;
170         int rc = 0, i;
171         ENTRY;
172
173         LASSERT(lsm != NULL);
174         LASSERT(count == lsm->lsm_stripe_count);
175
176         lov = &obd->u.lov;
177         obd_getref(obd);
178         for (i = 0; i < count; i++, cookies++) {
179                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
180                 struct obd_device *child =
181                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd;
182                 struct llog_ctxt *cctxt =
183                         llog_get_context(child, ctxt->loc_idx);
184                 int err;
185
186                 err = llog_cancel(env, cctxt, NULL, 1, cookies, flags);
187                 llog_ctxt_put(cctxt);
188                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
189                         CERROR("error: objid "LPX64" subobj "LPX64
190                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
191                                loi->loi_id, loi->loi_ost_idx, err);
192                         if (!rc)
193                                 rc = err;
194                 }
195         }
196         obd_putref(obd);
197         RETURN(rc);
198 }
199
200 static struct llog_operations lov_mds_ost_orig_logops = {
201         lop_add: lov_llog_origin_add,
202         lop_connect: lov_llog_origin_connect
203 };
204
205 static struct llog_operations lov_size_repl_logops = {
206         lop_cancel: lov_llog_repl_cancel
207 };
208
209 int lov_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
210                   struct obd_device *disk_obd, int *index)
211 {
212         struct lov_obd *lov = &obd->u.lov;
213         struct obd_device *child;
214         int i, rc = 0;
215         ENTRY;
216
217         LASSERT(olg == &obd->obd_olg);
218         rc = llog_setup(obd, olg, LLOG_MDS_OST_ORIG_CTXT, disk_obd, 0, NULL,
219                         &lov_mds_ost_orig_logops);
220         if (rc)
221                 RETURN(rc);
222
223         rc = llog_setup(obd, olg, LLOG_SIZE_REPL_CTXT, disk_obd, 0, NULL,
224                         &lov_size_repl_logops);
225         if (rc)
226                 GOTO(err_cleanup, rc);
227
228         obd_getref(obd);
229         /* count may not match lov->desc.ld_tgt_count during dynamic ost add */
230         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
231                 if (!lov->lov_tgts[i])
232                         continue;
233
234                 if (index && i != *index)
235                         continue;
236
237                 child = lov->lov_tgts[i]->ltd_obd;
238                 rc = obd_llog_init(child, &child->obd_olg, disk_obd, &i);
239                 if (rc)
240                         CERROR("error osc_llog_init idx %d osc '%s' tgt '%s' "
241                                "(rc=%d)\n", i, child->obd_name,
242                                disk_obd->obd_name, rc);
243                 rc = 0;
244         }
245         obd_putref(obd);
246         GOTO(err_cleanup, rc);
247 err_cleanup:
248         if (rc) {
249                 struct llog_ctxt *ctxt =
250                         llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
251                 if (ctxt)
252                         llog_cleanup(ctxt);
253                 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
254                 if (ctxt)
255                         llog_cleanup(ctxt);
256         }
257         return rc;
258 }
259
260 int lov_llog_finish(struct obd_device *obd, int count)
261 {
262         struct llog_ctxt *ctxt;
263         int rc = 0, rc2 = 0;
264         ENTRY;
265
266         /* cleanup our llogs only if the ctxts have been setup
267          * (client lov doesn't setup, mds lov does). */
268         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
269         if (ctxt)
270                 rc = llog_cleanup(ctxt);
271
272         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
273         if (ctxt)
274                 rc2 = llog_cleanup(ctxt);
275         if (!rc)
276                 rc = rc2;
277
278         /* lov->tgt llogs are cleaned during osc_cleanup. */
279         RETURN(rc);
280 }