Whamcloud - gitweb
34897996fe6188c400b5c7bb8b85f73053319bde
[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  *  lustre/mds/mds_log.c
5  *
6  *  Copyright (c) 2001-2003 Cluster File Systems, Inc.
7  *   Author: Peter Braam <braam@clusterfs.com>
8  *   Author: Andreas Dilger <adilger@clusterfs.com>
9  *   Author: Phil Schwan <phil@clusterfs.com>
10  *
11  *   This file is part of the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 #define DEBUG_SUBSYSTEM S_MDS
31
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35
36 #include <libcfs/list.h>
37 #include <obd_class.h>
38 #include <lustre_fsfilt.h>
39 #include <lustre_mds.h>
40 #include <lustre_commit_confd.h>
41 #include <lustre_log.h>
42
43 #include "mds_internal.h"
44
45 static int mds_llog_origin_add(struct llog_ctxt *ctxt,
46                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
47                         struct llog_cookie *logcookies, int numcookies)
48 {
49         struct obd_device *obd = ctxt->loc_obd;
50         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
51         struct llog_ctxt *lctxt;
52         int rc;
53         ENTRY;
54
55         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
56         rc = llog_add(lctxt, rec, lsm, logcookies, numcookies);
57         RETURN(rc);
58 }
59
60 static int mds_llog_origin_connect(struct llog_ctxt *ctxt, int count,
61                                    struct llog_logid *logid,
62                                    struct llog_gen *gen,
63                                    struct obd_uuid *uuid)
64 {
65         struct obd_device *obd = ctxt->loc_obd;
66         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
67         struct llog_ctxt *lctxt;
68         int rc;
69         ENTRY;
70
71         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
72         rc = llog_connect(lctxt, count, logid, gen, uuid);
73         RETURN(rc);
74 }
75
76 static int mds_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
77                           int count, struct llog_cookie *cookies, int flags)
78 {
79         struct obd_device *obd = ctxt->loc_obd;
80         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
81         struct llog_ctxt *lctxt;
82         int rc;
83         ENTRY;
84
85         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
86         rc = llog_cancel(lctxt, lsm, count, cookies, flags);
87         RETURN(rc);
88 }
89
90 int mds_log_op_unlink(struct obd_device *obd, 
91                       struct lov_mds_md *lmm, int lmm_size,
92                       struct llog_cookie *logcookies, int cookies_size)
93 {
94         struct mds_obd *mds = &obd->u.mds;
95         struct lov_stripe_md *lsm = NULL;
96         struct llog_unlink_rec *lur;
97         struct llog_ctxt *ctxt;
98         int rc;
99         ENTRY;
100
101         if (IS_ERR(mds->mds_osc_obd))
102                 RETURN(PTR_ERR(mds->mds_osc_obd));
103
104         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
105         if (rc < 0)
106                 RETURN(rc);
107         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
108         if (rc)
109                 GOTO(out, rc);
110         /* first prepare unlink log record */
111         OBD_ALLOC(lur, sizeof(*lur));
112         if (!lur)
113                 GOTO(out, rc = -ENOMEM);
114         lur->lur_hdr.lrh_len = lur->lur_tail.lrt_len = sizeof(*lur);
115         lur->lur_hdr.lrh_type = MDS_UNLINK_REC;
116
117         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
118         rc = llog_add(ctxt, &lur->lur_hdr, lsm, logcookies,
119                       cookies_size / sizeof(struct llog_cookie));
120
121         OBD_FREE(lur, sizeof(*lur));
122 out:
123         obd_free_memmd(mds->mds_osc_exp, &lsm);
124         RETURN(rc);
125 }
126 EXPORT_SYMBOL(mds_log_op_unlink);
127 int mds_log_op_setattr(struct obd_device *obd, struct inode *inode,
128                       struct lov_mds_md *lmm, int lmm_size,
129                       struct llog_cookie *logcookies, int cookies_size)
130 {
131         struct mds_obd *mds = &obd->u.mds;
132         struct lov_stripe_md *lsm = NULL;
133         struct llog_setattr_rec *lsr;
134         struct llog_ctxt *ctxt;
135         int rc;
136         ENTRY;
137
138         if (IS_ERR(mds->mds_osc_obd))
139                 RETURN(PTR_ERR(mds->mds_osc_obd));
140
141         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
142         if (rc < 0)
143                 RETURN(rc);
144
145         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
146         if (rc)
147                 GOTO(out, rc);
148
149         OBD_ALLOC(lsr, sizeof(*lsr));
150         if (!lsr)
151                 GOTO(out, rc = -ENOMEM);
152
153         /* prepare setattr log record */
154         lsr->lsr_hdr.lrh_len = lsr->lsr_tail.lrt_len = sizeof(*lsr);
155         lsr->lsr_hdr.lrh_type = MDS_SETATTR_REC;
156         lsr->lsr_uid = inode->i_uid;
157         lsr->lsr_gid = inode->i_gid;
158
159         /* write setattr log */
160         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
161         rc = llog_add(ctxt, &lsr->lsr_hdr, lsm, logcookies,
162                       cookies_size / sizeof(struct llog_cookie));
163
164         OBD_FREE(lsr, sizeof(*lsr));
165  out:
166         obd_free_memmd(mds->mds_osc_exp, &lsm);
167         RETURN(rc);
168 }
169 EXPORT_SYMBOL(mds_log_op_setattr);
170
171 static struct llog_operations mds_ost_orig_logops = {
172         lop_add:        mds_llog_origin_add,
173         lop_connect:    mds_llog_origin_connect,
174 };
175
176 static struct llog_operations mds_size_repl_logops = {
177         lop_cancel:     mds_llog_repl_cancel,
178 };
179
180 int mds_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
181                   struct obd_device *tgt, int count, struct llog_catid *logid, 
182                   struct obd_uuid *uuid)
183 {
184         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
185         int rc;
186         ENTRY;
187
188         rc = llog_setup(obd, llogs, LLOG_MDS_OST_ORIG_CTXT, tgt, 0, NULL,
189                         &mds_ost_orig_logops);
190         if (rc)
191                 RETURN(rc);
192
193         rc = llog_setup(obd, llogs, LLOG_SIZE_REPL_CTXT, tgt, 0, NULL,
194                         &mds_size_repl_logops);
195         if (rc)
196                 RETURN(rc);
197
198         rc = obd_llog_init(lov_obd, llogs, tgt, count, logid, uuid);
199         if (rc)
200                 CERROR("lov_llog_init err %d\n", rc);
201
202         RETURN(rc);
203 }
204
205 int mds_llog_finish(struct obd_device *obd, int count)
206 {
207         struct llog_ctxt *ctxt;
208         int rc = 0, rc2 = 0;
209         ENTRY;
210
211         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
212         if (ctxt)
213                 rc = llog_cleanup(ctxt);
214
215         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
216         if (ctxt)
217                 rc2 = llog_cleanup(ctxt);
218         if (!rc)
219                 rc = rc2;
220
221         RETURN(rc);
222 }