Whamcloud - gitweb
LU-1302 llog: modify llog_write/llog_add to support OSD
[fs/lustre-release.git] / lustre / obdfilter / filter_log.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Whamcloud, Inc.
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 #include <linux/module.h>
46 #include <linux/version.h>
47
48 #include <libcfs/list.h>
49 #include <obd_class.h>
50 #include <lustre_log.h>
51 #include <lustre_fsfilt.h>
52 #include "filter_internal.h"
53
54 int filter_log_sz_change(struct llog_handle *cathandle,
55                          struct ll_fid *mds_fid,
56                          __u32 ioepoch,
57                          struct llog_cookie *logcookie,
58                          struct inode *inode)
59 {
60         struct llog_size_change_rec *lsc;
61         int rc;
62         struct ost_filterdata *ofd;
63         ENTRY;
64
65         mutex_lock(&inode->i_mutex);
66         ofd = inode->i_private;
67
68         if (ofd && ofd->ofd_epoch >= ioepoch) {
69                 if (ofd->ofd_epoch > ioepoch)
70                         CERROR("client sent old epoch %d for obj ino %ld\n",
71                                ioepoch, inode->i_ino);
72                 mutex_unlock(&inode->i_mutex);
73                 RETURN(0);
74         }
75
76         if (ofd && ofd->ofd_epoch < ioepoch) {
77                 ofd->ofd_epoch = ioepoch;
78         } else if (!ofd) {
79                 OBD_ALLOC(ofd, sizeof(*ofd));
80                 if (!ofd)
81                         GOTO(out, rc = -ENOMEM);
82                 igrab(inode);
83                 inode->i_private = ofd;
84                 ofd->ofd_epoch = ioepoch;
85         }
86         /* the decision to write a record is now made, unlock */
87         mutex_unlock(&inode->i_mutex);
88
89         OBD_ALLOC(lsc, sizeof(*lsc));
90         if (lsc == NULL)
91                 RETURN(-ENOMEM);
92         lsc->lsc_hdr.lrh_len = lsc->lsc_tail.lrt_len = sizeof(*lsc);
93         lsc->lsc_hdr.lrh_type =  OST_SZ_REC;
94         lsc->lsc_fid = *mds_fid;
95         lsc->lsc_ioepoch = ioepoch;
96
97         rc = llog_cat_add(NULL, cathandle, &lsc->lsc_hdr, logcookie, NULL);
98         OBD_FREE(lsc, sizeof(*lsc));
99
100         if (rc > 0) {
101                 LASSERT(rc == sizeof(*logcookie));
102                 rc = 0;
103         }
104
105         out:
106         RETURN(rc);
107 }
108
109 /* When this (destroy) operation is committed, return the cancel cookie */
110 void filter_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
111                               void *cb_data, int error)
112 {
113         struct llog_cookie *cookie = cb_data;
114         struct obd_llog_group *olg;
115         struct llog_ctxt *ctxt;
116         int rc;
117
118         /* we have to find context for right group */
119         if (error != 0 || obd->obd_stopping) {
120                 CDEBUG(D_INODE, "not cancel logcookie err %d stopping %d \n",
121                        error, obd->obd_stopping);
122                 GOTO (out, rc = 0);
123         }
124
125         olg = filter_find_olg(obd, cookie->lgc_lgl.lgl_oseq);
126         if (!olg) {
127                 CDEBUG(D_HA, "unknown group "LPU64"!\n", cookie->lgc_lgl.lgl_oseq);
128                 GOTO(out, rc = 0);
129         }
130
131         ctxt = llog_group_get_ctxt(olg, cookie->lgc_subsys + 1);
132         if (!ctxt) {
133                 CERROR("no valid context for group "LPU64"\n",
134                         cookie->lgc_lgl.lgl_oseq);
135                 GOTO(out, rc = 0);
136         }
137
138         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_CANCEL_COOKIE_TIMEOUT, 30);
139
140         rc = llog_cancel(NULL, ctxt, NULL, 1, cookie, 0);
141         if (rc)
142                 CERROR("error cancelling log cookies: rc = %d\n", rc);
143         llog_ctxt_put(ctxt);
144 out:
145         OBD_FREE(cookie, sizeof(*cookie));
146 }
147
148 /* Callback for processing the unlink log record received from MDS by
149  * llog_client_api. */
150 static int filter_recov_log_unlink_cb(struct llog_ctxt *ctxt,
151                                       struct llog_rec_hdr *rec,
152                                       struct llog_cookie *cookie)
153 {
154         struct obd_export *exp = ctxt->loc_obd->obd_self_export;
155         struct llog_unlink_rec *lur;
156         struct obdo *oa;
157         obd_id oid;
158         obd_count count;
159         int rc = 0;
160         ENTRY;
161
162         lur = (struct llog_unlink_rec *)rec;
163         OBDO_ALLOC(oa);
164         if (oa == NULL)
165                 RETURN(-ENOMEM);
166         oa->o_valid |= OBD_MD_FLCOOKIE;
167         oa->o_id = lur->lur_oid;
168         oa->o_seq = lur->lur_oseq;
169         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
170         oa->o_lcookie = *cookie;
171         oid = oa->o_id;
172         /* objid gap may require to destroy several objects in row */
173         count = lur->lur_count + 1;
174
175         /* This check is only valid before FID-on-OST and it should
176          * be removed after FID-on-OST is implemented */
177         if (oa->o_seq > FID_SEQ_OST_MAX) {
178                 CERROR("%s: invalid group number "LPU64" > MAX_CMD_GROUP %u\n",
179                         exp->exp_obd->obd_name, oa->o_seq, FID_SEQ_OST_MAX);
180                 OBDO_FREE(oa);
181                 RETURN(-EINVAL);
182         }
183
184         while (count > 0) {
185                 rc = filter_destroy(NULL, exp, oa, NULL, NULL, NULL, NULL);
186                 if (rc == 0)
187                         CDEBUG(D_RPCTRACE, "object "LPU64" is destroyed\n",
188                                oid);
189                 else if (rc != -ENOENT)
190                         CEMERG("error destroying object "LPU64": %d\n",
191                                oid, rc);
192                 else
193                         rc = 0;
194                 count--;
195                 oid++;
196         }
197         OBDO_FREE(oa);
198
199         RETURN(rc);
200 }
201
202 /* Callback for processing the setattr log record received from MDS by
203  * llog_client_api. */
204 static int filter_recov_log_setattr_cb(struct llog_ctxt *ctxt,
205                                        struct llog_rec_hdr *rec,
206                                        struct llog_cookie *cookie)
207 {
208         struct obd_device *obd = ctxt->loc_obd;
209         struct obd_export *exp = obd->obd_self_export;
210         struct obd_info oinfo = { { { 0 } } };
211         obd_id oid;
212         int rc = 0;
213         ENTRY;
214
215         OBDO_ALLOC(oinfo.oi_oa);
216         if (oinfo.oi_oa == NULL)
217                 RETURN(-ENOMEM);
218
219         if (rec->lrh_type == MDS_SETATTR64_REC) {
220                 struct llog_setattr64_rec *lsr = (struct llog_setattr64_rec *)rec;
221
222                 oinfo.oi_oa->o_id = lsr->lsr_oid;
223                 oinfo.oi_oa->o_seq = lsr->lsr_oseq;
224                 oinfo.oi_oa->o_uid = lsr->lsr_uid;
225                 oinfo.oi_oa->o_gid = lsr->lsr_gid;
226         } else {
227                 CERROR("%s: wrong llog type %#x\n", obd->obd_name,
228                        rec->lrh_type);
229                 RETURN(-EINVAL);
230         }
231
232         oinfo.oi_oa->o_valid |= (OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID |
233                                  OBD_MD_FLCOOKIE);
234         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
235         oinfo.oi_oa->o_lcookie = *cookie;
236         oid = oinfo.oi_oa->o_id;
237
238         rc = filter_setattr(NULL, exp, &oinfo, NULL);
239         OBDO_FREE(oinfo.oi_oa);
240
241         if (rc == -ENOENT) {
242                 CDEBUG(D_RPCTRACE, "object already removed, send cookie\n");
243                 llog_cancel(NULL, ctxt, NULL, 1, cookie, 0);
244                 RETURN(0);
245         }
246
247         if (rc == 0)
248                 CDEBUG(D_RPCTRACE, "object "LPU64" is chown/chgrp\n", oid);
249
250         RETURN(rc);
251 }
252
253 int filter_recov_log_mds_ost_cb(const struct lu_env *env,
254                                 struct llog_handle *llh,
255                                 struct llog_rec_hdr *rec, void *data)
256 {
257         struct llog_ctxt *ctxt = llh->lgh_ctxt;
258         struct llog_cookie cookie;
259         int rc = 0;
260         ENTRY;
261
262         if (ctxt->loc_obd->obd_stopping)
263                 RETURN(LLOG_PROC_BREAK);
264
265         if (rec == NULL) {
266                 cfs_spin_lock(&ctxt->loc_obd->u.filter.fo_flags_lock);
267                 ctxt->loc_obd->u.filter.fo_mds_ost_sync = 0;
268                 cfs_spin_unlock(&ctxt->loc_obd->u.filter.fo_flags_lock);
269                 RETURN(0);
270         }
271
272         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
273                 CERROR("log is not plain\n");
274                 RETURN(-EINVAL);
275         }
276
277         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_LLOG_RECOVERY_TIMEOUT, 30);
278         cookie.lgc_lgl = llh->lgh_id;
279         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
280         cookie.lgc_index = rec->lrh_index;
281
282         switch (rec->lrh_type) {
283         case MDS_UNLINK_REC:
284                 rc = filter_recov_log_unlink_cb(ctxt, rec, &cookie);
285                 break;
286         case MDS_SETATTR64_REC:
287                 rc = filter_recov_log_setattr_cb(ctxt, rec, &cookie);
288                 break;
289         case LLOG_GEN_REC: {
290                 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
291
292                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
293                         rc = 0;
294                 else
295                         rc = LLOG_PROC_BREAK;
296                 CDEBUG(D_HA, "fetch generation log, send cookie\n");
297                 llog_cancel(NULL, ctxt, NULL, 1, &cookie, 0);
298                 RETURN(rc);
299                 }
300                 break;
301         default:
302                 CERROR("log record type %08x unknown\n", rec->lrh_type);
303                 RETURN(-EINVAL);
304                 break;
305         }
306
307         RETURN(rc);
308 }