Whamcloud - gitweb
land lustre part of b_hd_sec on 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 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 lustre_id *id, __u32 io_epoch,
42                          struct llog_cookie *logcookie, 
43                          struct inode *inode)
44 {
45         struct llog_size_change_rec *lsc;
46 #ifdef IFILTERDATA_ACTUALLY_USED
47         struct ost_filterdata *ofd;
48 #endif
49         int rc;
50         ENTRY;
51
52         down(&inode->i_sem);
53 #ifdef IFILTERDATA_ACTUALLY_USED
54         ofd = inode->i_filterdata;
55         
56         if (ofd && ofd->ofd_epoch >= io_epoch) {
57                 if (ofd->ofd_epoch > io_epoch)
58                         CERROR("client sent old epoch %d for obj ino %ld\n", 
59                                io_epoch, inode->i_ino);
60                 up(&inode->i_sem);
61                 RETURN(0);
62         }
63
64         if (ofd && ofd->ofd_epoch < io_epoch) {
65                 ofd->ofd_epoch = io_epoch;
66         } else if (!ofd) {
67                 OBD_ALLOC(ofd, sizeof(*ofd));
68                 if (!ofd)
69                         GOTO(out, rc = -ENOMEM);
70                 igrab(inode);
71                 inode->i_filterdata = ofd;
72                 ofd->ofd_epoch = io_epoch;
73         }
74 #endif
75         /* the decision to write a record is now made, unlock */
76         up(&inode->i_sem);
77
78         OBD_ALLOC(lsc, sizeof(*lsc));
79         if (lsc == NULL)
80                 RETURN(-ENOMEM);
81         lsc->lsc_hdr.lrh_len = lsc->lsc_tail.lrt_len = sizeof(*lsc);
82         lsc->lsc_hdr.lrh_type =  OST_SZ_REC;
83         lsc->lsc_id = *id;
84         lsc->lsc_io_epoch = io_epoch;
85
86         rc = llog_cat_add_rec(cathandle, &lsc->lsc_hdr, logcookie,
87                               NULL, NULL, NULL);
88         OBD_FREE(lsc, sizeof(*lsc));
89
90         if (rc > 0) {
91                 LASSERT(rc == sizeof(*logcookie));
92                 rc = 0;
93         }
94
95 #ifdef IFILTERDATA_ACTUALLY_USED
96 out:
97 #endif
98         RETURN(rc);
99 }
100 struct obd_llogs * filter_grab_llog_for_group(struct obd_device *,
101                                               int, struct obd_export *);
102
103 /* When this (destroy) operation is committed, return the cancel cookie */
104 void filter_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
105                               void *cb_data, int error)
106 {
107         struct llog_cookie *cookie = cb_data;
108         struct obd_llogs *llogs = NULL;
109         struct llog_ctxt *ctxt;
110
111         /* we have to find context for right group */
112         llogs = filter_grab_llog_for_group(obd, cookie->lgc_lgl.lgl_ogr, NULL);
113
114         if (llogs) {
115                 ctxt = llog_get_context(llogs, cookie->lgc_subsys + 1);
116                 if (ctxt) {
117                         llog_cancel(ctxt, 1, cookie, 0, NULL);
118                 } else
119                         CERROR("no valid context for group "LPU64"\n",
120                                cookie->lgc_lgl.lgl_ogr);
121         } else {
122                 CDEBUG(D_HA, "unknown group "LPU64"!\n", cookie->lgc_lgl.lgl_ogr);
123         }
124
125         OBD_FREE(cb_data, sizeof(struct llog_cookie));
126 }
127
128 /* Callback for processing the unlink log record received from MDS by 
129  * llog_client_api.
130  */
131 int filter_recov_log_unlink_cb(struct llog_handle *llh, 
132                                struct llog_rec_hdr *rec, void *data)
133 {
134         struct llog_ctxt *ctxt = llh->lgh_ctxt;
135         struct obd_device *obd = ctxt->loc_obd;
136         struct obd_export *exp = obd->obd_self_export;
137         struct llog_cookie cookie;
138         struct llog_gen_rec *lgr;
139         struct llog_unlink_rec *lur;
140         struct obdo *oa;
141         obd_id oid;
142         int rc = 0;
143         ENTRY;
144     
145         if (!(le32_to_cpu(llh->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)) {
146                 CERROR("log is not plain\n");
147                 RETURN(-EINVAL);
148         }
149         if (rec->lrh_type != MDS_UNLINK_REC &&
150             rec->lrh_type != LLOG_GEN_REC) {
151                 CERROR("log record type error\n");
152                 RETURN(-EINVAL);
153         }
154  
155         cookie.lgc_lgl = llh->lgh_id;
156         cookie.lgc_subsys = LLOG_UNLINK_ORIG_CTXT;
157         cookie.lgc_index = le32_to_cpu(rec->lrh_index);
158
159         if (rec->lrh_type == LLOG_GEN_REC) {
160                 lgr = (struct llog_gen_rec *)rec;
161                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
162                         rc = 0;
163                 else
164                         rc = LLOG_PROC_BREAK;
165                 CWARN("fetch generation log, send cookie\n");
166                 llog_cancel(ctxt, 1, &cookie, 0, NULL);
167                 RETURN(rc);
168         }
169
170         lur = (struct llog_unlink_rec *)rec;
171         oa = obdo_alloc();
172         if (oa == NULL) 
173                 RETURN(-ENOMEM);
174         oa->o_valid |= OBD_MD_FLCOOKIE;
175         oa->o_id = lur->lur_oid;
176         oa->o_gr = lur->lur_ogen;
177         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
178         memcpy(obdo_logcookie(oa), &cookie, sizeof(cookie));
179         oid = oa->o_id;
180
181         rc = obd_destroy(exp, oa, NULL, NULL);
182         obdo_free(oa);
183         if (rc == -ENOENT) {
184                 CDEBUG(D_HA, "object already removed, send cookie\n");
185                 llog_cancel(ctxt, 1, &cookie, 0, NULL);
186                 RETURN(0);
187         }
188
189         if (rc == 0)
190                 CDEBUG(D_HA, "object: "LPU64" in record is destroyed\n", oid);
191
192         RETURN(rc);
193 }