Whamcloud - gitweb
Revert "b=21379 Fix orphans proceeding in osc_create"
[fs/lustre-release.git] / lustre / mds / mds_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/mds/mds_log.c
37  *
38  * Author: Peter Braam <braam@clusterfs.com>
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Phil Schwan <phil@clusterfs.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/version.h>
50
51 #include <libcfs/list.h>
52 #include <obd_class.h>
53 #include <lustre_fsfilt.h>
54 #include <lustre_mds.h>
55 #include <lustre_log.h>
56 #include "mds_internal.h"
57
58 static int mds_llog_origin_add(struct llog_ctxt *ctxt, struct llog_rec_hdr *rec,
59                                struct lov_stripe_md *lsm,
60                                struct llog_cookie *logcookies, int numcookies)
61 {
62         struct obd_device *obd = ctxt->loc_obd;
63         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
64         struct llog_ctxt *lctxt;
65         int rc;
66         ENTRY;
67
68         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
69         rc = llog_add(lctxt, rec, lsm, logcookies, numcookies);
70         llog_ctxt_put(lctxt);
71
72         RETURN(rc);
73 }
74
75 static int mds_llog_origin_connect(struct llog_ctxt *ctxt,
76                                    struct llog_logid *logid,
77                                    struct llog_gen *gen,
78                                    struct obd_uuid *uuid)
79 {
80         struct obd_device *obd = ctxt->loc_obd;
81         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
82         struct llog_ctxt *lctxt;
83         int rc;
84         ENTRY;
85
86         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
87         rc = llog_connect(lctxt, logid, gen, uuid);
88         llog_ctxt_put(lctxt);
89         RETURN(rc);
90 }
91
92 static struct llog_operations mds_ost_orig_logops = {
93         lop_add:        mds_llog_origin_add,
94         lop_connect:    mds_llog_origin_connect,
95 };
96
97 static int mds_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
98                           int count, struct llog_cookie *cookies, int flags)
99 {
100         struct obd_device *obd = ctxt->loc_obd;
101         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
102         struct llog_ctxt *lctxt;
103         int rc;
104         ENTRY;
105
106         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
107         rc = llog_cancel(lctxt, lsm, count, cookies, flags);
108         llog_ctxt_put(lctxt);
109         RETURN(rc);
110 }
111
112 static struct llog_operations mds_size_repl_logops = {
113         lop_cancel:     mds_llog_repl_cancel,
114 };
115
116 static struct llog_operations changelog_orig_logops;
117
118 static int llog_changelog_cancel_cb(struct llog_handle *llh,
119                                     struct llog_rec_hdr *hdr, void *data)
120 {
121         struct llog_changelog_rec *rec = (struct llog_changelog_rec *)hdr;
122         struct llog_cookie cookie;
123         long long endrec = *(long long *)data;
124         int rc;
125         ENTRY;
126
127         /* This is always a (sub)log, not the catalog */
128         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
129
130         if (rec->cr.cr_index > endrec)
131                 /* records are in order, so we're done */
132                 RETURN(LLOG_PROC_BREAK);
133
134         cookie.lgc_lgl = llh->lgh_id;
135         cookie.lgc_index = hdr->lrh_index;
136
137         /* cancel them one at a time.  I suppose we could store up the cookies
138            and cancel them all at once; probably more efficient, but this is
139            done as a user call, so who cares... */
140         rc = llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
141         RETURN(rc < 0 ? rc : 0);
142 }
143
144 static int llog_changelog_cancel(struct llog_ctxt *ctxt,
145                                  struct lov_stripe_md *lsm, int count,
146                                  struct llog_cookie *cookies, int flags)
147 {
148         struct llog_handle *cathandle = ctxt->loc_handle;
149         int rc;
150         ENTRY;
151
152         /* This should only be called with the catalog handle */
153         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
154
155         rc = llog_cat_process(cathandle, llog_changelog_cancel_cb,
156                               (void *)cookies, 0, 0);
157         if (rc >= 0)
158                 /* 0 or 1 means we're done */
159                 rc = 0;
160         else
161                 CERROR("cancel idx %u of catalog "LPX64" rc=%d\n",
162                        cathandle->lgh_last_idx, cathandle->lgh_id.lgl_oid, rc);
163
164         RETURN(rc);
165 }
166
167 int mds_changelog_llog_init(struct obd_device *obd, struct obd_device *tgt)
168 {
169         int rc;
170
171         /* see osc_llog_init */
172         changelog_orig_logops = llog_lvfs_ops;
173         changelog_orig_logops.lop_setup = llog_obd_origin_setup;
174         changelog_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
175         changelog_orig_logops.lop_add = llog_obd_origin_add;
176         changelog_orig_logops.lop_cancel = llog_changelog_cancel;
177
178         rc = llog_setup_named(obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
179                               tgt, 1, NULL, CHANGELOG_CATALOG,
180                               &changelog_orig_logops);
181         if (rc) {
182                 CERROR("changelog llog setup failed %d\n", rc);
183                 RETURN(rc);
184         }
185
186         rc = llog_setup_named(obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
187                               tgt, 1, NULL, CHANGELOG_USERS,
188                               &changelog_orig_logops);
189         if (rc) {
190                 CERROR("changelog users llog setup failed %d\n", rc);
191                 RETURN(rc);
192         }
193
194         RETURN(rc);
195 }
196 EXPORT_SYMBOL(mds_changelog_llog_init);
197
198 int mds_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
199                   struct obd_device *disk_obd, int *index)
200 {
201         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
202         struct llog_ctxt *ctxt;
203         int rc;
204         ENTRY;
205
206         LASSERT(olg == &obd->obd_olg);
207         rc = llog_setup(obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT, disk_obd,
208                         0, NULL, &mds_ost_orig_logops);
209         if (rc)
210                 RETURN(rc);
211
212         rc = llog_setup(obd, &obd->obd_olg, LLOG_SIZE_REPL_CTXT, disk_obd,
213                         0, NULL, &mds_size_repl_logops);
214         if (rc)
215                 GOTO(err_llog, rc);
216
217         rc = obd_llog_init(lov_obd, &lov_obd->obd_olg, disk_obd, index);
218         if (rc) {
219                 CERROR("lov_llog_init err %d\n", rc);
220                 GOTO(err_cleanup, rc);
221         }
222
223         RETURN(rc);
224 err_cleanup:
225         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
226         if (ctxt)
227                 llog_cleanup(ctxt);
228 err_llog:
229         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
230         if (ctxt)
231                 llog_cleanup(ctxt);
232         return rc;
233 }
234
235 int mds_llog_finish(struct obd_device *obd, int count)
236 {
237         struct llog_ctxt *ctxt;
238         int rc = 0, rc2 = 0;
239         ENTRY;
240
241         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
242         if (ctxt)
243                 rc = llog_cleanup(ctxt);
244
245         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
246         if (ctxt)
247                 rc2 = llog_cleanup(ctxt);
248         if (!rc)
249                 rc = rc2;
250
251         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
252         if (ctxt)
253                 rc2 = llog_cleanup(ctxt);
254         if (!rc)
255                 rc = rc2;
256
257         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
258         if (ctxt)
259                 rc2 = llog_cleanup(ctxt);
260         if (!rc)
261                 rc = rc2;
262
263         RETURN(rc);
264 }
265
266 static int mds_llog_add_unlink(struct obd_device *obd,
267                                struct lov_stripe_md *lsm, obd_count count,
268                                struct llog_cookie *logcookie, int cookies)
269 {
270         struct llog_unlink_rec *lur;
271         struct llog_ctxt *ctxt;
272         int rc;
273
274         /* first prepare unlink log record */
275         OBD_ALLOC_PTR(lur);
276         if (!lur)
277                 RETURN(rc = -ENOMEM);
278         lur->lur_hdr.lrh_len = lur->lur_tail.lrt_len = sizeof(*lur);
279         lur->lur_hdr.lrh_type = MDS_UNLINK_REC;
280         lur->lur_count = count;
281
282         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
283         rc = llog_add(ctxt, &lur->lur_hdr, lsm, logcookie, cookies);
284         llog_ctxt_put(ctxt);
285
286         OBD_FREE_PTR(lur);
287         RETURN(rc);
288 }
289
290 int mds_log_op_unlink(struct obd_device *obd,
291                       struct lov_mds_md *lmm, int lmm_size,
292                       struct llog_cookie *logcookies, int cookies_size)
293 {
294         struct mds_obd *mds = &obd->u.mds;
295         struct lov_stripe_md *lsm = NULL;
296         int rc;
297         ENTRY;
298
299         if (IS_ERR(mds->mds_osc_obd))
300                 RETURN(PTR_ERR(mds->mds_osc_obd));
301
302         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
303         if (rc < 0)
304                 RETURN(rc);
305         rc = mds_llog_add_unlink(obd, lsm, 0, logcookies,
306                                  cookies_size / sizeof(struct llog_cookie));
307         obd_free_memmd(mds->mds_osc_exp, &lsm);
308         RETURN(rc);
309 }
310 EXPORT_SYMBOL(mds_log_op_unlink);
311
312 int mds_log_op_orphan(struct obd_device *obd, struct lov_stripe_md *lsm,
313                       obd_count count)
314 {
315         struct mds_obd *mds = &obd->u.mds;
316         struct llog_cookie logcookie;
317         int rc;
318         ENTRY;
319
320         if (IS_ERR(mds->mds_osc_obd))
321                 RETURN(PTR_ERR(mds->mds_osc_obd));
322
323         rc = mds_llog_add_unlink(obd, lsm, count - 1, &logcookie, 1);
324         RETURN(rc);
325 }
326