Whamcloud - gitweb
Branch b1_4
[fs/lustre-release.git] / lustre / obdfilter / filter_log.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  linux/fs/obdfilter/filter_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 Lustre, http://www.lustre.org.
12  *
13  *   Lustre is free software; you can redistribute it and/or
14  *   modify it under the terms of version 2 of the GNU General Public
15  *   License as published by the Free Software Foundation.
16  *
17  *   Lustre is distributed in the hope that it will be useful,
18  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
19  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  *   GNU General Public License for more details.
21  *
22  *   You should have received a copy of the GNU General Public License
23  *   along with Lustre; if not, write to the Free Software
24  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25  */
26
27 #define DEBUG_SUBSYSTEM S_FILTER
28
29 #include <linux/config.h>
30 #include <linux/module.h>
31 #include <linux/version.h>
32
33 #include <libcfs/list.h>
34 #include <linux/obd_class.h>
35 #include <linux/lustre_fsfilt.h>
36 #include <linux/lustre_commit_confd.h>
37
38 #include "filter_internal.h"
39
40 int filter_log_sz_change(struct llog_handle *cathandle, 
41                          struct ll_fid *mds_fid,
42                          __u32 io_epoch,
43                          struct llog_cookie *logcookie, 
44                          struct inode *inode)
45 {
46         struct llog_size_change_rec *lsc;
47         int rc;
48         struct ost_filterdata *ofd;
49         ENTRY;
50
51         down(&inode->i_sem);
52         ofd = inode->i_filterdata;
53         
54         if (ofd && ofd->ofd_epoch >= io_epoch) {
55                 if (ofd->ofd_epoch > io_epoch)
56                         CERROR("client sent old epoch %d for obj ino %ld\n", 
57                                io_epoch, inode->i_ino);
58                 up(&inode->i_sem);
59                 RETURN(0);
60         }
61
62         if (ofd && ofd->ofd_epoch < io_epoch) {
63                 ofd->ofd_epoch = io_epoch;
64         } else if (!ofd) {
65                 OBD_ALLOC(ofd, sizeof(*ofd));
66                 if (!ofd)
67                         GOTO(out, rc = -ENOMEM);
68                 igrab(inode);
69                 inode->i_filterdata = ofd;
70                 ofd->ofd_epoch = io_epoch;
71         }
72         /* the decision to write a record is now made, unlock */
73         up(&inode->i_sem);
74
75         OBD_ALLOC(lsc, sizeof(*lsc));
76         if (lsc == NULL)
77                 RETURN(-ENOMEM);
78         lsc->lsc_hdr.lrh_len = lsc->lsc_tail.lrt_len = sizeof(*lsc);
79         lsc->lsc_hdr.lrh_type =  OST_SZ_REC;
80         lsc->lsc_fid = *mds_fid;
81         lsc->lsc_io_epoch = io_epoch;
82
83         rc = llog_cat_add_rec(cathandle, &lsc->lsc_hdr, logcookie, NULL);
84         OBD_FREE(lsc, sizeof(*lsc));
85
86         if (rc > 0) {
87                 LASSERT(rc == sizeof(*logcookie));
88                 rc = 0;
89         }
90
91         out:
92         RETURN(rc);
93 }
94
95 /* When this (destroy) operation is committed, return the cancel cookie */
96 void filter_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
97                               void *cb_data, int error)
98 {
99         struct llog_cookie *cookie = cb_data;
100         int rc;
101
102         if (error != 0) {
103                 CDEBUG(D_INODE, "not cancelling llog cookie on error %d\n",
104                        error);
105                 return;
106         }
107
108         rc = llog_cancel(llog_get_context(obd, cookie->lgc_subsys + 1),
109                          NULL, 1, cookie, 0);
110         if (rc)
111                 CERROR("error cancelling log cookies: rc = %d\n", rc);
112         OBD_FREE(cookie, sizeof(*cookie));
113 }
114
115 /* Callback for processing the unlink log record received from MDS by 
116  * llog_client_api.
117  */
118 static int filter_recov_log_unlink_cb(struct llog_ctxt *ctxt,
119                                       struct llog_rec_hdr *rec,
120                                       struct llog_cookie *cookie)
121 {
122         struct obd_device *obd = ctxt->loc_obd;
123         struct obd_export *exp = obd->obd_self_export;
124         struct llog_unlink_rec *lur;
125         struct obdo *oa;
126         obd_id oid;
127         int rc = 0;
128         ENTRY;
129
130         lur = (struct llog_unlink_rec *)rec;
131         oa = obdo_alloc();
132         if (oa == NULL) 
133                 RETURN(-ENOMEM);
134         oa->o_valid |= OBD_MD_FLCOOKIE;
135         oa->o_id = lur->lur_oid;
136         oa->o_gr = lur->lur_ogen;
137         memcpy(obdo_logcookie(oa), cookie, sizeof(*cookie));
138         oid = oa->o_id;
139
140         rc = filter_destroy(exp, oa, NULL, NULL);
141         obdo_free(oa);
142         if (rc == -ENOENT) {
143                 CDEBUG(D_HA, "object already removed, send cookie\n");
144                 llog_cancel(ctxt, NULL, 1, cookie, 0);
145                 RETURN(0);
146         }
147
148         if (rc == 0)
149                 CDEBUG(D_HA, "object: "LPU64" in record is destroyed\n", oid);
150
151         RETURN(rc);
152 }
153
154 /* Callback for processing the setattr log record received from MDS by
155  * llog_client_api.
156  */
157 static int filter_recov_log_setattr_cb(struct llog_ctxt *ctxt,
158                                        struct llog_rec_hdr *rec,
159                                        struct llog_cookie *cookie)
160 {
161         struct obd_device *obd = ctxt->loc_obd;
162         struct obd_export *exp = obd->obd_self_export;
163         struct llog_setattr_rec *lsr;
164         struct obdo *oa;
165         obd_id oid;
166         int rc = 0;
167         ENTRY;
168
169         lsr = (struct llog_setattr_rec *)rec;
170         oa = obdo_alloc();
171
172         oa->o_valid |= (OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID |
173                         OBD_MD_FLCOOKIE);
174         oa->o_id = lsr->lsr_oid;
175         oa->o_gr = lsr->lsr_ogen;
176         oa->o_uid = lsr->lsr_uid;
177         oa->o_gid = lsr->lsr_gid;
178         memcpy(obdo_logcookie(oa), cookie, sizeof(*cookie));
179         oid = oa->o_id;
180
181         rc = filter_setattr(exp, oa, NULL, NULL);
182         obdo_free(oa);
183
184         if (rc == -ENOENT) {
185                 CDEBUG(D_HA, "object already removed, send cookie\n");
186                 llog_cancel(ctxt, NULL, 1, cookie, 0);
187                 RETURN(0);
188         }
189
190         if (rc == 0)
191                 CDEBUG(D_HA, "object: "LPU64" in record is chown/chgrp\n", oid);
192
193         RETURN(rc);
194 }
195
196 int filter_recov_log_mds_ost_cb(struct llog_handle *llh,
197                                struct llog_rec_hdr *rec, void *data)
198 {
199         struct llog_ctxt *ctxt = llh->lgh_ctxt;
200         struct llog_cookie cookie;
201         int rc = 0;
202         ENTRY;
203
204         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
205                 CERROR("log is not plain\n");
206                 RETURN(-EINVAL);
207         }
208         if (rec->lrh_type != MDS_UNLINK_REC &&
209             rec->lrh_type != MDS_SETATTR_REC &&
210             rec->lrh_type != LLOG_GEN_REC) {
211                 CERROR("log record type error\n");
212                 RETURN(-EINVAL);
213         }
214
215         cookie.lgc_lgl = llh->lgh_id;
216         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
217         cookie.lgc_index = rec->lrh_index;
218
219         switch (rec->lrh_type) {
220         case MDS_UNLINK_REC:
221                 rc = filter_recov_log_unlink_cb(ctxt, rec, &cookie);
222                 break;
223         case MDS_SETATTR_REC:
224                 rc = filter_recov_log_setattr_cb(ctxt, rec, &cookie);
225                 break;
226         case LLOG_GEN_REC: {
227                 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
228                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
229                         rc = 0;
230                 else
231                         rc = LLOG_PROC_BREAK;
232                 CWARN("fetch generation log, send cookie\n");
233                 llog_cancel(ctxt, NULL, 1, &cookie, 0);
234                 RETURN(rc);
235                 }
236         default:
237                 break;
238         }
239
240         RETURN(rc);
241 }