Whamcloud - gitweb
Branch: 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         int i, rc = 0, rc1;
63         ENTRY;
64
65         LASSERTF(logcookies && numcookies >= lsm->lsm_stripe_count, 
66                  "logcookies %p, numcookies %d lsm->lsm_stripe_count %d \n",
67                  logcookies, numcookies, lsm->lsm_stripe_count);
68
69         for (i = 0; i < lsm->lsm_stripe_count; i++) {
70                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
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                 LASSERT(lsm->lsm_object_gr == loi->loi_gr);
93                 rc1 = llog_add(cctxt, rec, NULL, logcookies + rc,
94                                numcookies - rc);
95                 llog_ctxt_put(cctxt);
96                 if (rc1 < 0)
97                         RETURN(rc1);
98                 rc += rc1;
99         }
100
101         RETURN(rc);
102 }
103
104 static int lov_llog_origin_connect(struct llog_ctxt *ctxt, int count,
105                                    struct llog_logid *logid,
106                                    struct llog_gen *gen,
107                                    struct obd_uuid *uuid)
108 {
109         struct obd_device *obd = ctxt->loc_obd;
110         struct lov_obd *lov = &obd->u.lov;
111         int i, rc = 0, err = 0;
112         ENTRY;
113
114         lov_getref(obd);
115         for (i = 0; i < count; i++) {
116                 struct obd_device *child;
117                 struct llog_ctxt *cctxt;
118                 
119                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
120                         continue;
121                 if (uuid && !obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
122                         continue;
123                 CDEBUG(D_CONFIG, "connect %d/%d\n", i, count);
124                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
125                 cctxt = llog_get_context(child, ctxt->loc_idx);
126                 rc = llog_connect(cctxt, 1, logid, gen, uuid);
127                 llog_ctxt_put(cctxt);
128  
129                 if (rc) {
130                         CERROR("error osc_llog_connect tgt %d (%d)\n", i, rc);
131                         if (!err) 
132                                 err = rc;
133                 }
134         }
135         lov_putref(obd);
136
137         RETURN(err);
138 }
139
140 /* the replicators commit callback */
141 static int lov_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
142                           int count, struct llog_cookie *cookies, int flags)
143 {
144         struct lov_obd *lov;
145         struct obd_device *obd = ctxt->loc_obd;
146         int rc = 0, i;
147         ENTRY;
148
149         LASSERT(lsm != NULL);
150         LASSERT(count == lsm->lsm_stripe_count);
151
152         lov = &obd->u.lov;
153         lov_getref(obd);
154         for (i = 0; i < count; i++, cookies++) {
155                 struct lov_oinfo *loi = lsm->lsm_oinfo[i];
156                 struct obd_device *child = 
157                         lov->lov_tgts[loi->loi_ost_idx]->ltd_exp->exp_obd;
158                 struct llog_ctxt *cctxt = 
159                         llog_get_context(child, ctxt->loc_idx);
160                 int err;
161
162                 err = llog_cancel(cctxt, NULL, 1, cookies, flags);
163                 llog_ctxt_put(cctxt);
164                 if (err && lov->lov_tgts[loi->loi_ost_idx]->ltd_active) {
165                         CERROR("error: objid "LPX64" subobj "LPX64
166                                " on OST idx %d: rc = %d\n", lsm->lsm_object_id,
167                                loi->loi_id, loi->loi_ost_idx, err);
168                         if (!rc)
169                                 rc = err;
170                 }
171         }
172         lov_putref(obd);
173         RETURN(rc);
174 }
175
176 static struct llog_operations lov_mds_ost_orig_logops = {
177         lop_add: lov_llog_origin_add,
178         lop_connect: lov_llog_origin_connect
179 };
180
181 static struct llog_operations lov_size_repl_logops = {
182         lop_cancel: lov_llog_repl_cancel
183 };
184
185 int lov_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
186                   struct obd_device *tgt, int count, struct llog_catid *logid, 
187                   struct obd_uuid *uuid)
188 {
189         struct lov_obd *lov = &obd->u.lov;
190         struct obd_device *child;
191         int i, rc = 0, err = 0;
192         ENTRY;
193
194         LASSERT(olg == &obd->obd_olg);
195         rc = llog_setup(obd, olg, LLOG_MDS_OST_ORIG_CTXT, tgt, 0, NULL,
196                         &lov_mds_ost_orig_logops);
197         if (rc)
198                 RETURN(rc);
199
200         rc = llog_setup(obd, olg, LLOG_SIZE_REPL_CTXT, tgt, 0, NULL,
201                         &lov_size_repl_logops);
202         if (rc)
203                 RETURN(rc);
204
205         lov_getref(obd);
206         /* count may not match lov->desc.ld_tgt_count during dynamic ost add */
207         for (i = 0; i < count; i++) {
208                 if (!lov->lov_tgts[i] || !lov->lov_tgts[i]->ltd_active)
209                         continue;
210                 if (uuid && !obd_uuid_equals(uuid, &lov->lov_tgts[i]->ltd_uuid))
211                         continue;
212                 CDEBUG(D_CONFIG, "init %d/%d\n", i, count);
213                 LASSERT(lov->lov_tgts[i]->ltd_exp);
214                 child = lov->lov_tgts[i]->ltd_exp->exp_obd;
215                 rc = obd_llog_init(child, &child->obd_olg, tgt, 1, logid + i, uuid);
216                 if (rc) {
217                         CERROR("error osc_llog_init idx %d osc '%s' tgt '%s' "
218                                "(rc=%d)\n", i, child->obd_name, tgt->obd_name,
219                                rc);
220                         if (!err) 
221                                 err = rc;
222                 }
223         }
224         lov_putref(obd);
225         RETURN(err);
226 }
227
228 int lov_llog_finish(struct obd_device *obd, int count)
229 {
230         struct llog_ctxt *ctxt;
231         int rc = 0, rc2 = 0;
232         ENTRY;
233
234         /* cleanup our llogs only if the ctxts have been setup
235          * (client lov doesn't setup, mds lov does). */
236         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
237         if (ctxt)
238                 rc = llog_cleanup(ctxt);
239
240         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
241         if (ctxt)
242                 rc2 = llog_cleanup(ctxt);
243         if (!rc)
244                 rc = rc2;
245
246         /* lov->tgt llogs are cleaned during osc_cleanup. */
247         RETURN(rc);
248 }