Whamcloud - gitweb
land b1_5 onto HEAD
[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  * Copyright (C) 2002, 2003 Cluster File Systems, Inc.
5  * Author: Phil Schwan <phil@clusterfs.com>
6  *         Peter Braam <braam@clusterfs.com>
7  *         Mike Shaver <shaver@clusterfs.com>
8  *
9  *   This file is part of the Lustre file system, http://www.lustre.org
10  *   Lustre is a trademark of Cluster File Systems, Inc.
11  *
12  *   You may have signed or agreed to another license before downloading
13  *   this software.  If so, you are bound by the terms and conditions
14  *   of that agreement, and the following does not apply to you.  See the
15  *   LICENSE file included with this distribution for more information.
16  *
17  *   If you did not agree to a different license, then this copy of Lustre
18  *   is open source software; you can redistribute it and/or modify it
19  *   under the terms of version 2 of the GNU General Public License as
20  *   published by the Free Software Foundation.
21  *
22  *   In either case, Lustre is distributed in the hope that it will be
23  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
24  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  *   license text for more details.
26  */
27
28 #ifndef EXPORT_SYMTAB
29 # define EXPORT_SYMTAB
30 #endif
31 #define DEBUG_SUBSYSTEM S_LOV
32 #ifdef __KERNEL__
33 #include <libcfs/libcfs.h>
34 #else
35 #include <liblustre.h>
36 #endif
37
38 #include <obd_support.h>
39 #include <lustre_lib.h>
40 #include <lustre_net.h>
41 #include <lustre/lustre_idl.h>
42 #include <lustre_dlm.h>
43 #include <lustre_mds.h>
44 #include <obd_class.h>
45 #include <obd_lov.h>
46 #include <obd_ost.h>
47 #include <lprocfs_status.h>
48
49 #include "lov_internal.h"
50
51 /* Add log records for each OSC that this object is striped over, and return
52  * cookies for each one.  We _would_ have nice abstraction here, except that
53  * we need to keep cookies in stripe order, even if some are NULL, so that
54  * the right cookies are passed back to the right OSTs at the client side.
55  * Unset cookies should be all-zero (which will never occur naturally). */
56 static int lov_llog_origin_add(struct llog_ctxt *ctxt,
57                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
58                         struct llog_cookie *logcookies, int numcookies)
59 {
60         struct obd_device *obd = ctxt->loc_obd;
61         struct lov_obd *lov = &obd->u.lov;
62         struct lov_oinfo *loi;
63         int i, rc = 0;
64         ENTRY;
65
66         LASSERTF(logcookies && numcookies >= lsm->lsm_stripe_count, 
67                  "logcookies %p, numcookies %d lsm->lsm_stripe_count %d \n",
68                  logcookies, numcookies, lsm->lsm_stripe_count);
69
70         for (i = 0,loi = lsm->lsm_oinfo; i < lsm->lsm_stripe_count; i++,loi++) {
71                 struct obd_device *child = 
72                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd; 
73                 struct llog_ctxt *cctxt = llog_get_context(child, ctxt->loc_idx);
74
75                 /* fill mds unlink/setattr log record */
76                 switch (rec->lrh_type) {
77                 case MDS_UNLINK_REC: {
78                         struct llog_unlink_rec *lur = (struct llog_unlink_rec *)rec;
79                         lur->lur_oid = loi->loi_id;
80                         lur->lur_ogen = loi->loi_gr;
81                         break;
82                 }
83                 case MDS_SETATTR_REC: {
84                         struct llog_setattr_rec *lsr = (struct llog_setattr_rec *)rec;
85                         lsr->lsr_oid = loi->loi_id;
86                         lsr->lsr_ogen = loi->loi_gr;
87                         break;
88                 }
89                 default:
90                         break;
91                 }
92
93                 rc += llog_add(cctxt, rec, NULL, logcookies + rc,
94                                 numcookies - rc);
95         }
96
97         RETURN(rc);
98 }
99
100 static int lov_llog_origin_connect(struct llog_ctxt *ctxt, int count,
101                                    struct llog_logid *logid,
102                                    struct llog_gen *gen,
103                                    struct obd_uuid *uuid)
104 {
105         struct obd_device *obd = ctxt->loc_obd;
106         struct lov_obd *lov = &obd->u.lov;
107         int i, rc = 0, err = 0;
108         ENTRY;
109
110         lov_getref(obd);
111         for (i = 0; i < count; i++) {
112                 struct obd_device *child;
113                 struct llog_ctxt *cctxt;
114                 
115                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
116                         continue;
117                 if (uuid && !obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
118                         continue;
119                 CDEBUG(D_CONFIG, "connect %d/%d\n", i, count);
120                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
121                 cctxt = llog_get_context(child, ctxt->loc_idx);
122                 rc = llog_connect(cctxt, 1, logid, gen, uuid);
123                 if (rc) {
124                         CERROR("error osc_llog_connect tgt %d (%d)\n", i, rc);
125                         if (!err) 
126                                 err = rc;
127                 }
128         }
129         lov_putref(obd);
130
131         RETURN(err);
132 }
133
134 /* the replicators commit callback */
135 static int lov_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
136                           int count, struct llog_cookie *cookies, int flags)
137 {
138         struct lov_obd *lov;
139         struct obd_device *obd = ctxt->loc_obd;
140         struct lov_oinfo *loi;
141         int rc = 0, i;
142         ENTRY;
143
144         LASSERT(lsm != NULL);
145         LASSERT(count == lsm->lsm_stripe_count);
146
147         loi = lsm->lsm_oinfo;
148         lov = &obd->u.lov;
149         lov_getref(obd);
150         for (i = 0; i < count; i++, cookies++, loi++) {
151                 struct obd_device *child = 
152                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd;
153                 struct llog_ctxt *cctxt = 
154                         llog_get_context(child, ctxt->loc_idx);
155                 int err;
156
157                 err = llog_cancel(cctxt, NULL, 1, cookies, flags);
158                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
159                         CERROR("error: objid "LPX64" subobj "LPX64
160                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
161                                loi->loi_id, loi->loi_ost_idx, err);
162                         if (!rc)
163                                 rc = err;
164                 }
165         }
166         lov_putref(obd);
167         RETURN(rc);
168 }
169
170 static struct llog_operations lov_mds_ost_orig_logops = {
171         lop_add: lov_llog_origin_add,
172         lop_connect: lov_llog_origin_connect
173 };
174
175 static struct llog_operations lov_size_repl_logops = {
176         lop_cancel: lov_llog_repl_cancel
177 };
178
179 int lov_llog_init(struct obd_device *obd, struct obd_device *tgt,
180                   int count, struct llog_catid *logid, struct obd_uuid *uuid)
181 {
182         struct lov_obd *lov = &obd->u.lov;
183         struct obd_device *child;
184         int i, rc = 0, err = 0;
185         ENTRY;
186
187         rc = llog_setup(obd, LLOG_MDS_OST_ORIG_CTXT, tgt, 0, NULL,
188                         &lov_mds_ost_orig_logops);
189         if (rc)
190                 RETURN(rc);
191
192         rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, tgt, 0, NULL,
193                         &lov_size_repl_logops);
194         if (rc)
195                 RETURN(rc);
196
197         lov_getref(obd);
198         /* count may not match lov->desc.ld_tgt_count during dynamic ost add */
199         for (i = 0; i < count; i++) {
200                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
201                         continue;
202                 if (uuid && !obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
203                         continue;
204                 CDEBUG(D_CONFIG, "init %d/%d\n", i, count);
205                 LASSERT(lov->lov_tgts[i]->ltd_exp);
206                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
207                 rc = obd_llog_init(child, tgt, 1, logid + i, uuid);
208                 if (rc) {
209                         CERROR("error osc_llog_init idx %d osc '%s' tgt '%s' "
210                                "(rc=%d)\n", i, child->obd_name, tgt->obd_name,
211                                rc);
212                         if (!err) 
213                                 err = rc;
214                 }
215         }
216         lov_putref(obd);
217         RETURN(err);
218 }
219
220 int lov_llog_finish(struct obd_device *obd, int count)
221 {
222         struct llog_ctxt *ctxt;
223         int rc = 0, rc2 = 0;
224         ENTRY;
225
226         /* cleanup our llogs only if the ctxts have been setup
227          * (client lov doesn't setup, mds lov does). */
228         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
229         if (ctxt)
230                 rc = llog_cleanup(ctxt);
231
232         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
233         if (ctxt)
234                 rc2 = llog_cleanup(ctxt);
235         if (!rc)
236                 rc = rc2;
237
238         /* lov->tgt llogs are cleaned during osc_cleanup. */
239         RETURN(rc);
240 }