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