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