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