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