Whamcloud - gitweb
port llog fixes from b1_6 into HEAD
[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 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_FILTER
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_commit_confd.h>
42
43 #include "filter_internal.h"
44
45 int filter_log_sz_change(struct llog_handle *cathandle,
46                          struct ll_fid *mds_fid,
47                          __u32 ioepoch,
48                          struct llog_cookie *logcookie,
49                          struct inode *inode)
50 {
51         struct llog_size_change_rec *lsc;
52         int rc;
53         struct ost_filterdata *ofd;
54         ENTRY;
55
56         LOCK_INODE_MUTEX(inode);
57         ofd = inode->i_filterdata;
58
59         if (ofd && ofd->ofd_epoch >= ioepoch) {
60                 if (ofd->ofd_epoch > ioepoch)
61                         CERROR("client sent old epoch %d for obj ino %ld\n",
62                                ioepoch, inode->i_ino);
63                 UNLOCK_INODE_MUTEX(inode);
64                 RETURN(0);
65         }
66
67         if (ofd && ofd->ofd_epoch < ioepoch) {
68                 ofd->ofd_epoch = ioepoch;
69         } else if (!ofd) {
70                 OBD_ALLOC(ofd, sizeof(*ofd));
71                 if (!ofd)
72                         GOTO(out, rc = -ENOMEM);
73                 igrab(inode);
74                 inode->i_filterdata = ofd;
75                 ofd->ofd_epoch = ioepoch;
76         }
77         /* the decision to write a record is now made, unlock */
78         UNLOCK_INODE_MUTEX(inode);
79
80         OBD_ALLOC(lsc, sizeof(*lsc));
81         if (lsc == NULL)
82                 RETURN(-ENOMEM);
83         lsc->lsc_hdr.lrh_len = lsc->lsc_tail.lrt_len = sizeof(*lsc);
84         lsc->lsc_hdr.lrh_type =  OST_SZ_REC;
85         lsc->lsc_fid = *mds_fid;
86         lsc->lsc_ioepoch = ioepoch;
87
88         rc = llog_cat_add_rec(cathandle, &lsc->lsc_hdr, logcookie, NULL);
89         OBD_FREE(lsc, sizeof(*lsc));
90
91         if (rc > 0) {
92                 LASSERT(rc == sizeof(*logcookie));
93                 rc = 0;
94         }
95
96         out:
97         RETURN(rc);
98 }
99
100 /* When this (destroy) operation is committed, return the cancel cookie */
101 void filter_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
102                               void *cb_data, int error)
103 {
104         struct llog_cookie *cookie = cb_data;
105         struct obd_llog_group *olg;
106         struct llog_ctxt *ctxt;
107         int rc;        
108         
109         /* we have to find context for right group */
110         if (error != 0 || obd->obd_stopping) {
111                 CDEBUG(D_INODE, "not cancel logcookie err %d stopping %d \n",
112                        error, obd->obd_stopping);
113                 GOTO (out, rc = 0);
114         }
115
116         olg = filter_find_olg(obd, cookie->lgc_lgl.lgl_ogr);
117         if (!IS_ERR(olg)) {
118                 ctxt = llog_group_get_ctxt(olg, cookie->lgc_subsys + 1);
119                 if (!ctxt) {
120                         CERROR("no valid context for group "LPU64"\n",
121                                 cookie->lgc_lgl.lgl_ogr);
122                         GOTO(out, rc = 0);
123                 }
124
125                 OBD_FAIL_TIMEOUT(OBD_FAIL_OST_CANCEL_COOKIE_TIMEOUT, 30);
126
127                 rc = llog_cancel(ctxt, NULL, 1, cookie, 0);
128                 if (rc)
129                         CERROR("error cancelling log cookies: rc = %d\n", rc);
130                 llog_ctxt_put(ctxt);
131         } else {
132                 CDEBUG(D_HA, "unknown group "LPU64"!\n", cookie->lgc_lgl.lgl_ogr);
133         }
134 out:
135         OBD_FREE(cookie, sizeof(*cookie));
136 }
137
138 /* Callback for processing the unlink log record received from MDS by 
139  * llog_client_api. */
140 static int filter_recov_log_unlink_cb(struct llog_ctxt *ctxt,
141                                       struct llog_rec_hdr *rec,
142                                       struct llog_cookie *cookie)
143 {
144         struct obd_device *obd = ctxt->loc_obd;
145         struct obd_export *exp = obd->obd_self_export;
146         struct llog_unlink_rec *lur;
147         struct obdo *oa;
148         obd_id oid;
149         int rc = 0;
150         ENTRY;
151
152         lur = (struct llog_unlink_rec *)rec;
153         OBDO_ALLOC(oa);
154         if (oa == NULL) 
155                 RETURN(-ENOMEM);
156         oa->o_valid |= OBD_MD_FLCOOKIE;
157         oa->o_id = lur->lur_oid;
158         oa->o_gr = lur->lur_ogen;
159         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
160         memcpy(obdo_logcookie(oa), cookie, sizeof(*cookie));
161         oid = oa->o_id;
162
163         rc = filter_destroy(exp, oa, NULL, NULL, NULL);
164         OBDO_FREE(oa);
165         if (rc == -ENOENT) {
166                 CDEBUG(D_RPCTRACE, "object already removed, send cookie\n");
167                 llog_cancel(ctxt, NULL, 1, cookie, 0);
168                 RETURN(0);
169         }
170
171         if (rc == 0)
172                 CDEBUG(D_RPCTRACE, "object "LPU64" is destroyed\n", oid);
173
174         RETURN(rc);
175 }
176
177 /* Callback for processing the setattr log record received from MDS by
178  * llog_client_api. */
179 static int filter_recov_log_setattr_cb(struct llog_ctxt *ctxt,
180                                        struct llog_rec_hdr *rec,
181                                        struct llog_cookie *cookie)
182 {
183         struct obd_device *obd = ctxt->loc_obd;
184         struct obd_export *exp = obd->obd_self_export;
185         struct llog_setattr_rec *lsr;
186         struct obd_info oinfo = { { { 0 } } };
187         obd_id oid;
188         int rc = 0;
189         ENTRY;
190
191         lsr = (struct llog_setattr_rec *)rec;
192         OBDO_ALLOC(oinfo.oi_oa);
193         if (oinfo.oi_oa == NULL)
194                 RETURN(-ENOMEM);
195
196         oinfo.oi_oa->o_valid |= (OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID |
197                                  OBD_MD_FLCOOKIE);
198         oinfo.oi_oa->o_id = lsr->lsr_oid;
199         oinfo.oi_oa->o_gr = lsr->lsr_ogen;
200         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
201         oinfo.oi_oa->o_uid = lsr->lsr_uid;
202         oinfo.oi_oa->o_gid = lsr->lsr_gid;
203         memcpy(obdo_logcookie(oinfo.oi_oa), cookie, sizeof(*cookie));
204         oid = oinfo.oi_oa->o_id;
205
206         rc = filter_setattr(exp, &oinfo, NULL);
207         OBDO_FREE(oinfo.oi_oa);
208
209         if (rc == -ENOENT) {
210                 CDEBUG(D_RPCTRACE, "object already removed, send cookie\n");
211                 llog_cancel(ctxt, NULL, 1, cookie, 0);
212                 RETURN(0);
213         }
214
215         if (rc == 0)
216                 CDEBUG(D_RPCTRACE, "object "LPU64" is chown/chgrp\n", oid);
217
218         RETURN(rc);
219 }
220
221 int filter_recov_log_mds_ost_cb(struct llog_handle *llh,
222                                struct llog_rec_hdr *rec, void *data)
223 {
224         struct llog_ctxt *ctxt = llh->lgh_ctxt;
225         struct llog_cookie cookie;
226         int rc = 0;
227         ENTRY;
228
229         if (ctxt->loc_obd->obd_stopping)
230                 RETURN(LLOG_PROC_BREAK);
231
232         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
233                 CERROR("log is not plain\n");
234                 RETURN(-EINVAL);
235         }
236
237         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_LLOG_RECOVERY_TIMEOUT, 30);
238         cookie.lgc_lgl = llh->lgh_id;
239         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
240         cookie.lgc_index = rec->lrh_index;
241
242         switch (rec->lrh_type) {
243         case MDS_UNLINK_REC:
244                 rc = filter_recov_log_unlink_cb(ctxt, rec, &cookie);
245                 break;
246         case MDS_SETATTR_REC:
247                 rc = filter_recov_log_setattr_cb(ctxt, rec, &cookie);
248                 break;
249         case LLOG_GEN_REC: {
250                 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
251                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
252                         rc = 0;
253                 else
254                         rc = LLOG_PROC_BREAK;
255                 CDEBUG(D_HA, "fetch generation log, send cookie\n");
256                 llog_cancel(ctxt, NULL, 1, &cookie, 0);
257                 RETURN(rc);
258                 }
259                 break;
260         default:
261                 CERROR("log record type %08x unknown\n", rec->lrh_type);
262                 RETURN(-EINVAL);
263                 break;
264         }
265
266         RETURN(rc);
267 }