Whamcloud - gitweb
ea45e847ca87b072f2ff9b15db258f4ead254f2b
[fs/lustre-release.git] / lustre / lov / lov_log.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 only,
10  * as published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * General Public License version 2 for more details (a copy is included
16  * in the LICENSE file that accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License
19  * version 2 along with this program; If not, see
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
21  *
22  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23  * CA 95054 USA or visit www.sun.com if you need additional information or
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright  2008 Sun Microsystems, Inc. All rights reserved
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/lov/lov_log.c
37  *
38  * Author: Phil Schwan <phil@clusterfs.com>
39  * Author: Peter Braam <braam@clusterfs.com>
40  * Author: Mike Shaver <shaver@clusterfs.com>
41  */
42
43 #ifndef EXPORT_SYMTAB
44 # define EXPORT_SYMTAB
45 #endif
46 #define DEBUG_SUBSYSTEM S_LOV
47 #ifdef __KERNEL__
48 #include <libcfs/libcfs.h>
49 #else
50 #include <liblustre.h>
51 #endif
52
53 #include <obd_support.h>
54 #include <lustre_lib.h>
55 #include <lustre_net.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre_dlm.h>
58 #include <lustre_mds.h>
59 #include <obd_class.h>
60 #include <obd_lov.h>
61 #include <obd_ost.h>
62 #include <lprocfs_status.h>
63
64 #include "lov_internal.h"
65
66 /* Add log records for each OSC that this object is striped over, and return
67  * cookies for each one.  We _would_ have nice abstraction here, except that
68  * we need to keep cookies in stripe order, even if some are NULL, so that
69  * the right cookies are passed back to the right OSTs at the client side.
70  * Unset cookies should be all-zero (which will never occur naturally). */
71 static int lov_llog_origin_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
72                                struct lov_stripe_md *lsm, 
73                                struct llog_cookie *logcookies, int numcookies)
74 {
75         struct obd_device *obd = ctxt->loc_obd;
76         struct lov_obd *lov = &obd->u.lov;
77         int i, rc = 0, rc1;
78         ENTRY;
79
80         LASSERTF(logcookies && numcookies >= lsm->lsm_stripe_count, 
81                  "logcookies %p, numcookies %d lsm->lsm_stripe_count %d \n",
82                  logcookies, numcookies, lsm->lsm_stripe_count);
83
84         for (i = 0; i < lsm->lsm_stripe_count; i++) {
85                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
86                 struct obd_device *child = 
87                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd; 
88                 struct llog_ctxt *cctxt = llog_get_context(child, ctxt->loc_idx);
89
90                 /* fill mds unlink/setattr log record */
91                 switch (rec->lrh_type) {
92                 case MDS_UNLINK_REC: {
93                         struct llog_unlink_rec *lur = (struct llog_unlink_rec *)rec;
94                         lur->lur_oid = loi->loi_id;
95                         lur->lur_ogen = loi->loi_gr;
96                         break;
97                 }
98                 case MDS_SETATTR_REC: {
99                         struct llog_setattr_rec *lsr = (struct llog_setattr_rec *)rec;
100                         lsr->lsr_oid = loi->loi_id;
101                         lsr->lsr_ogen = loi->loi_gr;
102                         break;
103                 }
104                 default:
105                         break;
106                 }
107                 LASSERT(lsm->lsm_object_gr == loi->loi_gr);
108                 rc1 = llog_add(cctxt, rec, NULL, logcookies + rc,
109                                numcookies - rc);
110                 llog_ctxt_put(cctxt);
111                 if (rc1 < 0)
112                         RETURN(rc1);
113                 rc += rc1;
114         }
115
116         RETURN(rc);
117 }
118
119 static int lov_llog_origin_connect(struct llog_ctxt *ctxt,
120                                    struct llog_logid *logid,
121                                    struct llog_gen *gen,
122                                    struct obd_uuid *uuid)
123 {
124         struct obd_device *obd = ctxt->loc_obd;
125         struct lov_obd *lov = &obd->u.lov;
126         int i, rc = 0, err = 0;
127         ENTRY;
128
129         lov_getref(obd);
130         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
131                 struct obd_device *child;
132                 struct llog_ctxt *cctxt;
133                 
134                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
135                         continue;
136                 if (uuid && !obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
137                         continue;
138                 CDEBUG(D_CONFIG, "connect %d/%d\n", i, lov->desc.ld_tgt_count);
139                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
140                 cctxt = llog_get_context(child, ctxt->loc_idx);
141                 rc = llog_connect(cctxt, logid, gen, uuid);
142                 llog_ctxt_put(cctxt);
143  
144                 if (rc) {
145                         CERROR("error osc_llog_connect tgt %d (%d)\n", i, rc);
146                         if (!err) 
147                                 err = rc;
148                 }
149         }
150         lov_putref(obd);
151
152         RETURN(err);
153 }
154
155 /* the replicators commit callback */
156 static int lov_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
157                           int count, struct llog_cookie *cookies, int flags)
158 {
159         struct lov_obd *lov;
160         struct obd_device *obd = ctxt->loc_obd;
161         int rc = 0, i;
162         ENTRY;
163
164         LASSERT(lsm != NULL);
165         LASSERT(count == lsm->lsm_stripe_count);
166
167         lov = &obd->u.lov;
168         lov_getref(obd);
169         for (i = 0; i < count; i++, cookies++) {
170                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
171                 struct obd_device *child = 
172                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd;
173                 struct llog_ctxt *cctxt = 
174                         llog_get_context(child, ctxt->loc_idx);
175                 int err;
176
177                 err = llog_cancel(cctxt, NULL, 1, cookies, flags);
178                 llog_ctxt_put(cctxt);
179                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
180                         CERROR("error: objid "LPX64" subobj "LPX64
181                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
182                                loi->loi_id, loi->loi_ost_idx, err);
183                         if (!rc)
184                                 rc = err;
185                 }
186         }
187         lov_putref(obd);
188         RETURN(rc);
189 }
190
191 static struct llog_operations lov_mds_ost_orig_logops = {
192         lop_add: lov_llog_origin_add,
193         lop_connect: lov_llog_origin_connect
194 };
195
196 static struct llog_operations lov_size_repl_logops = {
197         lop_cancel: lov_llog_repl_cancel
198 };
199
200 int lov_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
201                   struct obd_device *tgt, int count, struct llog_catid *logid,
202                   struct obd_uuid *uuid)
203 {
204         struct lov_obd *lov = &obd->u.lov;
205         struct obd_device *child;
206         int i, rc = 0;
207         ENTRY;
208
209         /* allow init only one target at one time */
210         LASSERT(uuid);
211
212         LASSERT(olg == &obd->obd_olg);
213         rc = llog_setup(obd, olg, LLOG_MDS_OST_ORIG_CTXT, tgt, 0, NULL,
214                         &lov_mds_ost_orig_logops);
215         if (rc)
216                 RETURN(rc);
217
218         rc = llog_setup(obd, olg, LLOG_SIZE_REPL_CTXT, tgt, 0, NULL,
219                         &lov_size_repl_logops);
220         if (rc)
221                 GOTO(err_cleanup, rc);
222
223         lov_getref(obd);
224         /* count may not match lov->desc.ld_tgt_count during dynamic ost add */
225         for (i = 0; i < lov->desc.ld_tgt_count; i++) {
226                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
227                         continue;
228                 if (!obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
229                         continue;
230
231                 LASSERT(lov->lov_tgts[i]->ltd_exp);
232                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
233                 rc = obd_llog_init(child, &child->obd_olg, tgt, 1, logid, uuid);
234                 if (rc)
235                         CERROR("error osc_llog_init idx %d osc '%s' tgt '%s' "
236                                "(rc=%d)\n", i, child->obd_name, tgt->obd_name,
237                                rc);
238                 break;
239         }
240         lov_putref(obd);
241         GOTO(err_cleanup, rc);
242 err_cleanup:
243         if (rc) {
244                 struct llog_ctxt *ctxt = 
245                         llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
246                 if (ctxt)
247                         llog_cleanup(ctxt);
248                 ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
249                 if (ctxt)
250                         llog_cleanup(ctxt);
251         }
252         return rc;
253 }
254
255 int lov_llog_finish(struct obd_device *obd, int count)
256 {
257         struct llog_ctxt *ctxt;
258         int rc = 0, rc2 = 0;
259         ENTRY;
260
261         /* cleanup our llogs only if the ctxts have been setup
262          * (client lov doesn't setup, mds lov does). */
263         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
264         if (ctxt)
265                 rc = llog_cleanup(ctxt);
266
267         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
268         if (ctxt)
269                 rc2 = llog_cleanup(ctxt);
270         if (!rc)
271                 rc = rc2;
272
273         /* lov->tgt llogs are cleaned during osc_cleanup. */
274         RETURN(rc);
275 }