Whamcloud - gitweb
be4959d1ce2ee8110f161d5bc98371d6a337a82c
[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 #ifndef AUTOCONF_INCLUDED
33 #include <linux/config.h>
34 #endif
35 #include <linux/module.h>
36 #include <linux/version.h>
37
38 #include <libcfs/list.h>
39 #include <obd_class.h>
40 #include <lustre_fsfilt.h>
41 #include <lustre_mds.h>
42 #include <lustre_commit_confd.h>
43 #include <lustre_log.h>
44
45 #include "mds_internal.h"
46
47 static int mds_llog_origin_add(struct llog_ctxt *ctxt,
48                         struct llog_rec_hdr *rec, struct lov_stripe_md *lsm,
49                         struct llog_cookie *logcookies, int numcookies)
50 {
51         struct obd_device *obd = ctxt->loc_obd;
52         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
53         struct llog_ctxt *lctxt;
54         int rc;
55         ENTRY;
56
57         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
58         rc = llog_add(lctxt, rec, lsm, logcookies, numcookies);
59         RETURN(rc);
60 }
61
62 static int mds_llog_origin_connect(struct llog_ctxt *ctxt, int count,
63                                    struct llog_logid *logid,
64                                    struct llog_gen *gen,
65                                    struct obd_uuid *uuid)
66 {
67         struct obd_device *obd = ctxt->loc_obd;
68         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
69         struct llog_ctxt *lctxt;
70         int rc;
71         ENTRY;
72
73         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
74         rc = llog_connect(lctxt, count, logid, gen, uuid);
75         RETURN(rc);
76 }
77
78 static int mds_llog_repl_cancel(struct llog_ctxt *ctxt, struct lov_stripe_md *lsm,
79                           int count, struct llog_cookie *cookies, int flags)
80 {
81         struct obd_device *obd = ctxt->loc_obd;
82         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
83         struct llog_ctxt *lctxt;
84         int rc;
85         ENTRY;
86
87         lctxt = llog_get_context(lov_obd, ctxt->loc_idx);
88         rc = llog_cancel(lctxt, lsm, count, cookies, flags);
89         RETURN(rc);
90 }
91
92 int mds_log_op_unlink(struct obd_device *obd, 
93                       struct lov_mds_md *lmm, int lmm_size,
94                       struct llog_cookie *logcookies, int cookies_size)
95 {
96         struct mds_obd *mds = &obd->u.mds;
97         struct lov_stripe_md *lsm = NULL;
98         struct llog_unlink_rec *lur;
99         struct llog_ctxt *ctxt;
100         int rc;
101         ENTRY;
102
103         if (IS_ERR(mds->mds_osc_obd))
104                 RETURN(PTR_ERR(mds->mds_osc_obd));
105
106         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
107         if (rc < 0)
108                 RETURN(rc);
109         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
110         if (rc)
111                 GOTO(out, rc);
112         /* first prepare unlink log record */
113         OBD_ALLOC(lur, sizeof(*lur));
114         if (!lur)
115                 GOTO(out, rc = -ENOMEM);
116         lur->lur_hdr.lrh_len = lur->lur_tail.lrt_len = sizeof(*lur);
117         lur->lur_hdr.lrh_type = MDS_UNLINK_REC;
118
119         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
120         rc = llog_add(ctxt, &lur->lur_hdr, lsm, logcookies,
121                       cookies_size / sizeof(struct llog_cookie));
122
123         OBD_FREE(lur, sizeof(*lur));
124 out:
125         obd_free_memmd(mds->mds_osc_exp, &lsm);
126         RETURN(rc);
127 }
128 EXPORT_SYMBOL(mds_log_op_unlink);
129 int mds_log_op_setattr(struct obd_device *obd, __u32 uid, __u32 gid,
130                       struct lov_mds_md *lmm, int lmm_size,
131                       struct llog_cookie *logcookies, int cookies_size)
132 {
133         struct mds_obd *mds = &obd->u.mds;
134         struct lov_stripe_md *lsm = NULL;
135         struct llog_setattr_rec *lsr;
136         struct llog_ctxt *ctxt;
137         int rc;
138         ENTRY;
139
140         if (IS_ERR(mds->mds_osc_obd))
141                 RETURN(PTR_ERR(mds->mds_osc_obd));
142
143         rc = obd_unpackmd(mds->mds_osc_exp, &lsm, lmm, lmm_size);
144         if (rc < 0)
145                 RETURN(rc);
146
147         rc = obd_checkmd(mds->mds_osc_exp, obd->obd_self_export, lsm);
148         if (rc)
149                 GOTO(out, rc);
150
151         OBD_ALLOC(lsr, sizeof(*lsr));
152         if (!lsr)
153                 GOTO(out, rc = -ENOMEM);
154
155         /* prepare setattr log record */
156         lsr->lsr_hdr.lrh_len = lsr->lsr_tail.lrt_len = sizeof(*lsr);
157         lsr->lsr_hdr.lrh_type = MDS_SETATTR_REC;
158         lsr->lsr_uid = uid;
159         lsr->lsr_gid = gid;
160
161         /* write setattr log */
162         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
163         rc = llog_add(ctxt, &lsr->lsr_hdr, lsm, logcookies,
164                       cookies_size / sizeof(struct llog_cookie));
165
166         OBD_FREE(lsr, sizeof(*lsr));
167  out:
168         obd_free_memmd(mds->mds_osc_exp, &lsm);
169         RETURN(rc);
170 }
171 EXPORT_SYMBOL(mds_log_op_setattr);
172
173 static struct llog_operations mds_ost_orig_logops = {
174         lop_add:        mds_llog_origin_add,
175         lop_connect:    mds_llog_origin_connect,
176 };
177
178 static struct llog_operations mds_size_repl_logops = {
179         lop_cancel:     mds_llog_repl_cancel,
180 };
181
182 int mds_llog_init(struct obd_device *obd, struct obd_llogs *llogs,
183                   struct obd_device *tgt, int count, struct llog_catid *logid, 
184                   struct obd_uuid *uuid)
185 {
186         struct obd_device *lov_obd = obd->u.mds.mds_osc_obd;
187         int rc;
188         ENTRY;
189
190         rc = llog_setup(obd, llogs, LLOG_MDS_OST_ORIG_CTXT, tgt, 0, NULL,
191                         &mds_ost_orig_logops);
192         if (rc)
193                 RETURN(rc);
194
195         rc = llog_setup(obd, llogs, LLOG_SIZE_REPL_CTXT, tgt, 0, NULL,
196                         &mds_size_repl_logops);
197         if (rc)
198                 RETURN(rc);
199
200         rc = obd_llog_init(lov_obd, llogs, tgt, count, logid, uuid);
201         if (rc)
202                 CERROR("lov_llog_init err %d\n", rc);
203
204         RETURN(rc);
205 }
206
207 int mds_llog_finish(struct obd_device *obd, int count)
208 {
209         struct llog_ctxt *ctxt;
210         int rc = 0, rc2 = 0;
211         ENTRY;
212
213         ctxt = llog_get_context(obd, LLOG_MDS_OST_ORIG_CTXT);
214         if (ctxt)
215                 rc = llog_cleanup(ctxt);
216
217         ctxt = llog_get_context(obd, LLOG_SIZE_REPL_CTXT);
218         if (ctxt)
219                 rc2 = llog_cleanup(ctxt);
220         if (!rc)
221                 rc = rc2;
222
223         RETURN(rc);
224 }