Whamcloud - gitweb
Changelog update
[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_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 int mds_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
93                           int count, struct llog_cookie *cookies, int flags)
94 {
95         struct obd_device *obd = ctxt->loc_obd;
96         struct obd_device *lov_obd = obd->u.mds.mds_lov_obd;
97         struct llog_ctxt *lctxt;
98         int rc;
99         ENTRY;
100
101         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
102         rc = llog_cancel(lctxt, lsm, count, cookies, flags);
103         llog_ctxt_put(lctxt);
104         RETURN(rc);
105 }
106
107 static int mds_llog_add_unlink(struct obd_device *obd,
108                                struct lov_stripe_md *lsm, obd_count count,
109                                struct llog_cookie *logcookie, int cookies)
110 {
111         struct llog_unlink_rec *lur;
112         struct llog_ctxt *ctxt;
113         int rc;
114
115         rc = obd_checkmd(obd->u.mds.mds_lov_exp, obd->obd_self_export, lsm);
116         if (rc)
117                 RETURN(rc);
118         /* first prepare unlink log record */
119         OBD_ALLOC_PTR(lur);
120         if (!lur)
121                 RETURN(rc = -ENOMEM);
122         lur->lur_hdr.lrh_len = lur->lur_tail.lrt_len = sizeof(*lur);
123         lur->lur_hdr.lrh_type = MDS_UNLINK_REC;
124         lur->lur_count = count;
125
126         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
127         rc = llog_add(ctxt, &lur->lur_hdr, lsm, logcookie, cookies);
128         llog_ctxt_put(ctxt);
129
130         OBD_FREE_PTR(lur);
131         RETURN(rc);
132 }
133
134 int mds_log_op_unlink(struct obd_device *obd,
135                       struct lov_mds_md *lmm, int lmm_size,
136                       struct llog_cookie *logcookies, int cookies_size)
137 {
138         struct mds_obd *mds = &obd->u.mds;
139         struct lov_stripe_md *lsm = NULL;
140         int rc;
141         ENTRY;
142
143         if (IS_ERR(mds->mds_lov_obd))
144                 RETURN(PTR_ERR(mds->mds_lov_obd));
145
146         rc = obd_unpackmd(mds->mds_lov_exp, &lsm, lmm, lmm_size);
147         if (rc < 0)
148                 RETURN(rc);
149         rc = mds_llog_add_unlink(obd, lsm, 0, logcookies,
150                                  cookies_size / sizeof(struct llog_cookie));
151         obd_free_memmd(mds->mds_lov_exp, &lsm);
152         RETURN(rc);
153 }
154
155 int mds_log_op_orphan(struct obd_device *obd, struct lov_stripe_md *lsm,
156                       obd_count count)
157 {
158         struct mds_obd *mds = &obd->u.mds;
159         struct llog_cookie logcookie;
160         int rc;
161         ENTRY;
162
163         if (IS_ERR(mds->mds_lov_obd))
164                 RETURN(PTR_ERR(mds->mds_lov_obd));
165
166         rc = obd_checkmd(mds->mds_lov_exp, obd->obd_self_export, lsm);
167         if (rc)
168                 RETURN(rc);
169         rc = mds_llog_add_unlink(obd, lsm, count - 1, &logcookie, 1);
170         RETURN(rc);
171 }
172
173 int mds_log_op_setattr(struct obd_device *obd, struct inode *inode,
174                       struct lov_mds_md *lmm, int lmm_size,
175                       struct llog_cookie *logcookies, int cookies_size)
176 {
177         struct mds_obd *mds = &obd->u.mds;
178         struct lov_stripe_md *lsm = NULL;
179         struct llog_setattr_rec *lsr;
180         struct llog_ctxt *ctxt;
181         int rc;
182         ENTRY;
183
184         if (IS_ERR(mds->mds_lov_obd))
185                 RETURN(PTR_ERR(mds->mds_lov_obd));
186
187         rc = obd_unpackmd(mds->mds_lov_exp, &lsm, lmm, lmm_size);
188         if (rc < 0)
189                 RETURN(rc);
190
191         rc = obd_checkmd(mds->mds_lov_exp, obd->obd_self_export, lsm);
192         if (rc)
193                 GOTO(out, rc);
194
195         OBD_ALLOC(lsr, sizeof(*lsr));
196         if (!lsr)
197                 GOTO(out, rc = -ENOMEM);
198
199         /* prepare setattr log record */
200         lsr->lsr_hdr.lrh_len = lsr->lsr_tail.lrt_len = sizeof(*lsr);
201         lsr->lsr_hdr.lrh_type = MDS_SETATTR_REC;
202         lsr->lsr_uid = inode->i_uid;
203         lsr->lsr_gid = inode->i_gid;
204
205         /* write setattr log */
206         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
207         rc = llog_add(ctxt, &lsr->lsr_hdr, lsm, logcookies,
208                       cookies_size / sizeof(struct llog_cookie));
209
210         llog_ctxt_put(ctxt);
211
212         OBD_FREE(lsr, sizeof(*lsr));
213  out:
214         obd_free_memmd(mds->mds_lov_exp, &lsm);
215         RETURN(rc);
216 }
217
218 static struct llog_operations mds_ost_orig_logops = {
219         lop_add:        mds_llog_origin_add,
220         lop_connect:    mds_llog_origin_connect,
221 };
222
223 static struct llog_operations mds_size_repl_logops = {
224         lop_cancel:     mds_llog_repl_cancel,
225 };
226
227 int mds_llog_init(struct obd_device *obd, struct obd_device *disk_obd,
228                   int *index)
229 {
230         struct obd_device *lov_obd = obd->u.mds.mds_lov_obd;
231         struct llog_ctxt *ctxt;
232         int rc;
233         ENTRY;
234
235         rc = llog_setup(obd, LLOG_MDS_OST_ORIG_CTXT, disk_obd, 0, NULL,
236                         &mds_ost_orig_logops);
237         if (rc)
238                 RETURN(rc);
239
240         rc = llog_setup(obd, LLOG_SIZE_REPL_CTXT, disk_obd, 0, NULL,
241                         &mds_size_repl_logops);
242         if (rc)
243                 GOTO(err_llog, rc);
244
245         rc = obd_llog_init(lov_obd, disk_obd, index);
246         if (rc) {
247                 CERROR("lov_llog_init err %d\n", rc);
248                 GOTO(err_cleanup, rc);
249         }
250         RETURN(rc);
251 err_cleanup:
252         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
253         if (ctxt)
254                 llog_cleanup(ctxt);
255 err_llog:
256         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
257         if (ctxt)
258                 llog_cleanup(ctxt);
259         return rc;
260 }
261
262 int mds_llog_finish(struct obd_device *obd, int count)
263 {
264         struct llog_ctxt *ctxt;
265         int rc = 0, rc2 = 0;
266         ENTRY;
267
268         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
269         if (ctxt)
270                 rc = llog_cleanup(ctxt);
271
272         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
273         if (ctxt)
274                 rc2 = llog_cleanup(ctxt);
275         if (!rc)
276                 rc = rc2;
277
278         RETURN(rc);
279 }