Whamcloud - gitweb
d997ae8b680d0d2feb9295d6b4170f1afe607a1a
[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_rec(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                 RETURN(-EINVAL);
181         }
182
183         while (count > 0) {
184                 rc = filter_destroy(NULL, exp, oa, NULL, NULL, NULL, NULL);
185                 if (rc == 0)
186                         CDEBUG(D_RPCTRACE, "object "LPU64" is destroyed\n",
187                                oid);
188                 else if (rc != -ENOENT)
189                         CEMERG("error destroying object "LPU64": %d\n",
190                                oid, rc);
191                 else
192                         rc = 0;
193                 count--;
194                 oid++;
195         }
196         OBDO_FREE(oa);
197
198         RETURN(rc);
199 }
200
201 /* Callback for processing the setattr log record received from MDS by
202  * llog_client_api. */
203 static int filter_recov_log_setattr_cb(struct llog_ctxt *ctxt,
204                                        struct llog_rec_hdr *rec,
205                                        struct llog_cookie *cookie)
206 {
207         struct obd_device *obd = ctxt->loc_obd;
208         struct obd_export *exp = obd->obd_self_export;
209         struct obd_info oinfo = { { { 0 } } };
210         obd_id oid;
211         int rc = 0;
212         ENTRY;
213
214         OBDO_ALLOC(oinfo.oi_oa);
215         if (oinfo.oi_oa == NULL)
216                 RETURN(-ENOMEM);
217
218         if (rec->lrh_type == MDS_SETATTR64_REC) {
219                 struct llog_setattr64_rec *lsr = (struct llog_setattr64_rec *)rec;
220
221                 oinfo.oi_oa->o_id = lsr->lsr_oid;
222                 oinfo.oi_oa->o_seq = lsr->lsr_oseq;
223                 oinfo.oi_oa->o_uid = lsr->lsr_uid;
224                 oinfo.oi_oa->o_gid = lsr->lsr_gid;
225         } else {
226                 CERROR("%s: wrong llog type %#x\n", obd->obd_name,
227                        rec->lrh_type);
228                 RETURN(-EINVAL);
229         }
230
231         oinfo.oi_oa->o_valid |= (OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID |
232                                  OBD_MD_FLCOOKIE);
233         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
234         oinfo.oi_oa->o_lcookie = *cookie;
235         oid = oinfo.oi_oa->o_id;
236
237         rc = filter_setattr(NULL, exp, &oinfo, NULL);
238         OBDO_FREE(oinfo.oi_oa);
239
240         if (rc == -ENOENT) {
241                 CDEBUG(D_RPCTRACE, "object already removed, send cookie\n");
242                 llog_cancel(NULL, ctxt, NULL, 1, cookie, 0);
243                 RETURN(0);
244         }
245
246         if (rc == 0)
247                 CDEBUG(D_RPCTRACE, "object "LPU64" is chown/chgrp\n", oid);
248
249         RETURN(rc);
250 }
251
252 int filter_recov_log_mds_ost_cb(const struct lu_env *env,
253                                 struct llog_handle *llh,
254                                 struct llog_rec_hdr *rec, void *data)
255 {
256         struct llog_ctxt *ctxt = llh->lgh_ctxt;
257         struct llog_cookie cookie;
258         int rc = 0;
259         ENTRY;
260
261         if (ctxt->loc_obd->obd_stopping)
262                 RETURN(LLOG_PROC_BREAK);
263
264         if (rec == NULL) {
265                 cfs_spin_lock(&ctxt->loc_obd->u.filter.fo_flags_lock);
266                 ctxt->loc_obd->u.filter.fo_mds_ost_sync = 0;
267                 cfs_spin_unlock(&ctxt->loc_obd->u.filter.fo_flags_lock);
268                 RETURN(0);
269         }
270
271         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
272                 CERROR("log is not plain\n");
273                 RETURN(-EINVAL);
274         }
275
276         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_LLOG_RECOVERY_TIMEOUT, 30);
277         cookie.lgc_lgl = llh->lgh_id;
278         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
279         cookie.lgc_index = rec->lrh_index;
280
281         switch (rec->lrh_type) {
282         case MDS_UNLINK_REC:
283                 rc = filter_recov_log_unlink_cb(ctxt, rec, &cookie);
284                 break;
285         case MDS_SETATTR64_REC:
286                 rc = filter_recov_log_setattr_cb(ctxt, rec, &cookie);
287                 break;
288         case LLOG_GEN_REC: {
289                 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
290
291                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
292                         rc = 0;
293                 else
294                         rc = LLOG_PROC_BREAK;
295                 CDEBUG(D_HA, "fetch generation log, send cookie\n");
296                 llog_cancel(NULL, ctxt, NULL, 1, &cookie, 0);
297                 RETURN(rc);
298                 }
299                 break;
300         default:
301                 CERROR("log record type %08x unknown\n", rec->lrh_type);
302                 RETURN(-EINVAL);
303                 break;
304         }
305
306         RETURN(rc);
307 }