Whamcloud - gitweb
LU-1347 style: removes obsolete EXPORT_SYMTAB macros
[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 #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_lov_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_lov_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_lov_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, err;
125         struct obd_device *obd;
126         void *trans_h;
127         struct inode *inode;
128         ENTRY;
129
130         /* This is always a (sub)log, not the catalog */
131         LASSERT(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN);
132
133         if (rec->cr.cr_index > endrec)
134                 /* records are in order, so we're done */
135                 RETURN(LLOG_PROC_BREAK);
136
137         cookie.lgc_lgl = llh->lgh_id;
138         cookie.lgc_index = hdr->lrh_index;
139         obd = llh->lgh_ctxt->loc_exp->exp_obd;
140         inode = llh->lgh_file->f_dentry->d_inode;
141
142         /* XXX This is a workaround for the deadlock of changelog adding vs.
143          * changelog cancelling. Changelog adding always start transaction
144          * before acquiring the catlog lock (lgh_lock), whereas, changelog
145          * cancelling do start transaction after holding catlog lock.
146          *
147          * We start the transaction earlier here to keep the locking ordering:
148          * 'start transaction -> catlog lock'. LU-81. */
149         trans_h = fsfilt_start_log(obd, inode, FSFILT_OP_CANCEL_UNLINK,
150                                    NULL, 1);
151         if (IS_ERR(trans_h)) {
152                 CERROR("fsfilt_start_log failed: %ld\n", PTR_ERR(trans_h));
153                 RETURN(PTR_ERR(trans_h));
154         }
155
156         /* cancel them one at a time.  I suppose we could store up the cookies
157            and cancel them all at once; probably more efficient, but this is
158            done as a user call, so who cares... */
159         rc = llog_cat_cancel_records(llh->u.phd.phd_cat_handle, 1, &cookie);
160
161         err = fsfilt_commit(obd, inode, trans_h, 0);
162         if (err) {
163                 CERROR("fsfilt_commit failed: %d\n", err);
164                 rc = (rc >= 0) ? err : rc;
165         }
166
167         RETURN(rc < 0 ? rc : 0);
168 }
169
170 static int llog_changelog_cancel(struct llog_ctxt *ctxt,
171                                  struct lov_stripe_md *lsm, int count,
172                                  struct llog_cookie *cookies, int flags)
173 {
174         struct llog_handle *cathandle = ctxt->loc_handle;
175         int rc;
176         ENTRY;
177
178         /* This should only be called with the catalog handle */
179         LASSERT(cathandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT);
180
181         rc = llog_cat_process(cathandle, llog_changelog_cancel_cb,
182                               (void *)cookies, 0, 0);
183         if (rc >= 0)
184                 /* 0 or 1 means we're done */
185                 rc = 0;
186         else
187                 CERROR("cancel idx %u of catalog "LPX64" rc=%d\n",
188                        cathandle->lgh_last_idx, cathandle->lgh_id.lgl_oid, rc);
189
190         RETURN(rc);
191 }
192
193 int mds_changelog_llog_init(struct obd_device *obd, struct obd_device *tgt)
194 {
195         int rc;
196
197         /* see osc_llog_init */
198         changelog_orig_logops = llog_lvfs_ops;
199         changelog_orig_logops.lop_setup = llog_obd_origin_setup;
200         changelog_orig_logops.lop_cleanup = llog_obd_origin_cleanup;
201         changelog_orig_logops.lop_add = llog_obd_origin_add;
202         changelog_orig_logops.lop_cancel = llog_changelog_cancel;
203
204         rc = llog_setup_named(obd, &obd->obd_olg, LLOG_CHANGELOG_ORIG_CTXT,
205                               tgt, 1, NULL, CHANGELOG_CATALOG,
206                               &changelog_orig_logops);
207         if (rc) {
208                 CERROR("changelog llog setup failed %d\n", rc);
209                 RETURN(rc);
210         }
211
212         rc = llog_setup_named(obd, &obd->obd_olg, LLOG_CHANGELOG_USER_ORIG_CTXT,
213                               tgt, 1, NULL, CHANGELOG_USERS,
214                               &changelog_orig_logops);
215         if (rc) {
216                 CERROR("changelog users llog setup failed %d\n", rc);
217                 RETURN(rc);
218         }
219
220         RETURN(rc);
221 }
222 EXPORT_SYMBOL(mds_changelog_llog_init);
223
224 int mds_llog_init(struct obd_device *obd, struct obd_llog_group *olg,
225                   struct obd_device *disk_obd, int *index)
226 {
227         struct obd_device *lov_obd = obd->u.mds.mds_lov_obd;
228         struct llog_ctxt *ctxt;
229         int rc;
230         ENTRY;
231
232         LASSERT(olg == &obd->obd_olg);
233         rc = llog_setup(obd, &obd->obd_olg, LLOG_MDS_OST_ORIG_CTXT, disk_obd,
234                         0, NULL, &mds_ost_orig_logops);
235         if (rc)
236                 RETURN(rc);
237
238         rc = llog_setup(obd, &obd->obd_olg, LLOG_SIZE_REPL_CTXT, disk_obd,
239                         0, NULL, &mds_size_repl_logops);
240         if (rc)
241                 GOTO(err_llog, rc);
242
243         rc = obd_llog_init(lov_obd, &lov_obd->obd_olg, disk_obd, index);
244         if (rc) {
245                 CERROR("lov_llog_init err %d\n", rc);
246                 GOTO(err_cleanup, rc);
247         }
248
249         RETURN(rc);
250 err_cleanup:
251         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
252         if (ctxt)
253                 llog_cleanup(ctxt);
254 err_llog:
255         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
256         if (ctxt)
257                 llog_cleanup(ctxt);
258         return rc;
259 }
260
261 int mds_llog_finish(struct obd_device *obd, int count)
262 {
263         struct llog_ctxt *ctxt;
264         int rc = 0, rc2 = 0;
265         ENTRY;
266
267         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
268         if (ctxt)
269                 rc = llog_cleanup(ctxt);
270
271         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
272         if (ctxt)
273                 rc2 = llog_cleanup(ctxt);
274         if (!rc)
275                 rc = rc2;
276
277         ctxt = llog_get_context(obd, LLOG_CHANGELOG_ORIG_CTXT);
278         if (ctxt)
279                 rc2 = llog_cleanup(ctxt);
280         if (!rc)
281                 rc = rc2;
282
283         ctxt = llog_get_context(obd, LLOG_CHANGELOG_USER_ORIG_CTXT);
284         if (ctxt)
285                 rc2 = llog_cleanup(ctxt);
286         if (!rc)
287                 rc = rc2;
288
289         RETURN(rc);
290 }
291
292 static int mds_llog_add_unlink(struct obd_device *obd,
293                                struct lov_stripe_md *lsm, obd_count count,
294                                struct llog_cookie *logcookie, int cookies)
295 {
296         struct llog_unlink_rec *lur;
297         struct llog_ctxt *ctxt;
298         int rc;
299
300         if (cookies < lsm->lsm_stripe_count)
301                 RETURN(rc = -EFBIG);
302
303         /* first prepare unlink log record */
304         OBD_ALLOC_PTR(lur);
305         if (!lur)
306                 RETURN(rc = -ENOMEM);
307         lur->lur_hdr.lrh_len = lur->lur_tail.lrt_len = sizeof(*lur);
308         lur->lur_hdr.lrh_type = MDS_UNLINK_REC;
309         lur->lur_count = count;
310
311         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
312         rc = llog_add(ctxt, &lur->lur_hdr, lsm, logcookie, cookies);
313         llog_ctxt_put(ctxt);
314
315         OBD_FREE_PTR(lur);
316         RETURN(rc);
317 }
318
319 int mds_log_op_unlink(struct obd_device *obd,
320                       struct lov_mds_md *lmm, int lmm_size,
321                       struct llog_cookie *logcookies, int cookies_size)
322 {
323         struct mds_obd *mds = &obd->u.mds;
324         struct lov_stripe_md *lsm = NULL;
325         int rc;
326         ENTRY;
327
328         if (IS_ERR(mds->mds_lov_obd))
329                 RETURN(PTR_ERR(mds->mds_lov_obd));
330
331         rc = obd_unpackmd(mds->mds_lov_exp, &lsm, lmm, lmm_size);
332         if (rc < 0)
333                 RETURN(rc);
334         rc = mds_llog_add_unlink(obd, lsm, 0, logcookies,
335                                  cookies_size / sizeof(struct llog_cookie));
336         obd_free_memmd(mds->mds_lov_exp, &lsm);
337         RETURN(rc);
338 }
339 EXPORT_SYMBOL(mds_log_op_unlink);
340
341 int mds_log_op_orphan(struct obd_device *obd, struct lov_stripe_md *lsm,
342                       obd_count count)
343 {
344         struct mds_obd *mds = &obd->u.mds;
345         struct llog_cookie logcookie;
346         int rc;
347         ENTRY;
348
349         if (IS_ERR(mds->mds_lov_obd))
350                 RETURN(PTR_ERR(mds->mds_lov_obd));
351
352         rc = mds_llog_add_unlink(obd, lsm, count - 1, &logcookie, 1);
353         RETURN(rc);
354 }
355