Whamcloud - gitweb
LU-1146 build: batch update copyright messages
[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
20  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
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 (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  *
32  * Copyright (c) 2011, Whamcloud, Inc.
33  */
34 /*
35  * This file is part of Lustre, http://www.lustre.org/
36  * Lustre is a trademark of Sun Microsystems, Inc.
37  *
38  * lustre/obdfilter/filter_log.c
39  *
40  * Author: Peter Braam <braam@clusterfs.com>
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Phil Schwan <phil@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_FILTER
46
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
49 #endif
50 #include <linux/module.h>
51 #include <linux/version.h>
52
53 #include <libcfs/list.h>
54 #include <obd_class.h>
55 #include <lustre_log.h>
56 #include <lustre_fsfilt.h>
57 #include "filter_internal.h"
58
59 int filter_log_sz_change(struct llog_handle *cathandle,
60                          struct ll_fid *mds_fid,
61                          __u32 ioepoch,
62                          struct llog_cookie *logcookie,
63                          struct inode *inode)
64 {
65         struct llog_size_change_rec *lsc;
66         int rc;
67         struct ost_filterdata *ofd;
68         ENTRY;
69
70         LOCK_INODE_MUTEX(inode);
71         ofd = INODE_PRIVATE_DATA(inode);
72
73         if (ofd && ofd->ofd_epoch >= ioepoch) {
74                 if (ofd->ofd_epoch > ioepoch)
75                         CERROR("client sent old epoch %d for obj ino %ld\n",
76                                ioepoch, inode->i_ino);
77                 UNLOCK_INODE_MUTEX(inode);
78                 RETURN(0);
79         }
80
81         if (ofd && ofd->ofd_epoch < ioepoch) {
82                 ofd->ofd_epoch = ioepoch;
83         } else if (!ofd) {
84                 OBD_ALLOC(ofd, sizeof(*ofd));
85                 if (!ofd)
86                         GOTO(out, rc = -ENOMEM);
87                 igrab(inode);
88                 INODE_PRIVATE_DATA(inode) = ofd;
89                 ofd->ofd_epoch = ioepoch;
90         }
91         /* the decision to write a record is now made, unlock */
92         UNLOCK_INODE_MUTEX(inode);
93
94         OBD_ALLOC(lsc, sizeof(*lsc));
95         if (lsc == NULL)
96                 RETURN(-ENOMEM);
97         lsc->lsc_hdr.lrh_len = lsc->lsc_tail.lrt_len = sizeof(*lsc);
98         lsc->lsc_hdr.lrh_type =  OST_SZ_REC;
99         lsc->lsc_fid = *mds_fid;
100         lsc->lsc_ioepoch = ioepoch;
101
102         rc = llog_cat_add_rec(cathandle, &lsc->lsc_hdr, logcookie, NULL);
103         OBD_FREE(lsc, sizeof(*lsc));
104
105         if (rc > 0) {
106                 LASSERT(rc == sizeof(*logcookie));
107                 rc = 0;
108         }
109
110         out:
111         RETURN(rc);
112 }
113
114 /* When this (destroy) operation is committed, return the cancel cookie */
115 void filter_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
116                               void *cb_data, int error)
117 {
118         struct llog_cookie *cookie = cb_data;
119         struct obd_llog_group *olg;
120         struct llog_ctxt *ctxt;
121         int rc;
122
123         /* we have to find context for right group */
124         if (error != 0 || obd->obd_stopping) {
125                 CDEBUG(D_INODE, "not cancel logcookie err %d stopping %d \n",
126                        error, obd->obd_stopping);
127                 GOTO (out, rc = 0);
128         }
129
130         olg = filter_find_olg(obd, cookie->lgc_lgl.lgl_oseq);
131         if (!olg) {
132                 CDEBUG(D_HA, "unknown group "LPU64"!\n", cookie->lgc_lgl.lgl_oseq);
133                 GOTO(out, rc = 0);
134         }
135
136         ctxt = llog_group_get_ctxt(olg, cookie->lgc_subsys + 1);
137         if (!ctxt) {
138                 CERROR("no valid context for group "LPU64"\n",
139                         cookie->lgc_lgl.lgl_oseq);
140                 GOTO(out, rc = 0);
141         }
142
143         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_CANCEL_COOKIE_TIMEOUT, 30);
144
145         rc = llog_cancel(ctxt, NULL, 1, cookie, 0);
146         if (rc)
147                 CERROR("error cancelling log cookies: rc = %d\n", rc);
148         llog_ctxt_put(ctxt);
149 out:
150         OBD_FREE(cookie, sizeof(*cookie));
151 }
152
153 /* Callback for processing the unlink log record received from MDS by
154  * llog_client_api. */
155 static int filter_recov_log_unlink_cb(struct llog_ctxt *ctxt,
156                                       struct llog_rec_hdr *rec,
157                                       struct llog_cookie *cookie)
158 {
159         struct obd_export *exp = ctxt->loc_obd->obd_self_export;
160         struct llog_unlink_rec *lur;
161         struct obdo *oa;
162         obd_id oid;
163         obd_count count;
164         int rc = 0;
165         ENTRY;
166
167         lur = (struct llog_unlink_rec *)rec;
168         OBDO_ALLOC(oa);
169         if (oa == NULL)
170                 RETURN(-ENOMEM);
171         oa->o_valid |= OBD_MD_FLCOOKIE;
172         oa->o_id = lur->lur_oid;
173         oa->o_seq = lur->lur_oseq;
174         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
175         oa->o_lcookie = *cookie;
176         oid = oa->o_id;
177         /* objid gap may require to destroy several objects in row */
178         count = lur->lur_count + 1;
179
180         /* This check is only valid before FID-on-OST and it should
181          * be removed after FID-on-OST is implemented */
182         if (oa->o_seq > FID_SEQ_OST_MAX) {
183                 CERROR("%s: invalid group number "LPU64" > MAX_CMD_GROUP %u\n",
184                         exp->exp_obd->obd_name, oa->o_seq, FID_SEQ_OST_MAX);
185                 RETURN(-EINVAL);
186         }
187
188         while (count > 0) {
189                 rc = filter_destroy(exp, oa, NULL, NULL, NULL, NULL);
190                 if (rc == 0)
191                         CDEBUG(D_RPCTRACE, "object "LPU64" is destroyed\n",
192                                oid);
193                 else if (rc != -ENOENT)
194                         CEMERG("error destroying object "LPU64": %d\n",
195                                oid, rc);
196                 else
197                         rc = 0;
198                 count--;
199                 oid++;
200         }
201         OBDO_FREE(oa);
202
203         RETURN(rc);
204 }
205
206 /* Callback for processing the setattr log record received from MDS by
207  * llog_client_api. */
208 static int filter_recov_log_setattr_cb(struct llog_ctxt *ctxt,
209                                        struct llog_rec_hdr *rec,
210                                        struct llog_cookie *cookie)
211 {
212         struct obd_device *obd = ctxt->loc_obd;
213         struct obd_export *exp = obd->obd_self_export;
214         struct obd_info oinfo = { { { 0 } } };
215         obd_id oid;
216         int rc = 0;
217         ENTRY;
218
219         OBDO_ALLOC(oinfo.oi_oa);
220         if (oinfo.oi_oa == NULL)
221                 RETURN(-ENOMEM);
222
223         if (rec->lrh_type == MDS_SETATTR_REC) {
224                 struct llog_setattr_rec *lsr = (struct llog_setattr_rec *)rec;
225
226                 oinfo.oi_oa->o_id = lsr->lsr_oid;
227                 oinfo.oi_oa->o_seq = lsr->lsr_oseq;
228                 oinfo.oi_oa->o_uid = lsr->lsr_uid;
229                 oinfo.oi_oa->o_gid = lsr->lsr_gid;
230         } else {
231                 struct llog_setattr64_rec *lsr = (struct llog_setattr64_rec *)rec;
232
233                 oinfo.oi_oa->o_id = lsr->lsr_oid;
234                 oinfo.oi_oa->o_seq = lsr->lsr_oseq;
235                 oinfo.oi_oa->o_uid = lsr->lsr_uid;
236                 oinfo.oi_oa->o_gid = lsr->lsr_gid;
237         }
238
239         oinfo.oi_oa->o_valid |= (OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID |
240                                  OBD_MD_FLCOOKIE);
241         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
242         oinfo.oi_oa->o_lcookie = *cookie;
243         oid = oinfo.oi_oa->o_id;
244
245         rc = filter_setattr(exp, &oinfo, NULL);
246         OBDO_FREE(oinfo.oi_oa);
247
248         if (rc == -ENOENT) {
249                 CDEBUG(D_RPCTRACE, "object already removed, send cookie\n");
250                 llog_cancel(ctxt, NULL, 1, cookie, 0);
251                 RETURN(0);
252         }
253
254         if (rc == 0)
255                 CDEBUG(D_RPCTRACE, "object "LPU64" is chown/chgrp\n", oid);
256
257         RETURN(rc);
258 }
259
260 int filter_recov_log_mds_ost_cb(struct llog_handle *llh,
261                                 struct llog_rec_hdr *rec, void *data)
262 {
263         struct llog_ctxt *ctxt = llh->lgh_ctxt;
264         struct llog_cookie cookie;
265         int rc = 0;
266         ENTRY;
267
268         if (ctxt->loc_obd->obd_stopping)
269                 RETURN(LLOG_PROC_BREAK);
270
271         if (rec == NULL) {
272                 cfs_spin_lock(&ctxt->loc_obd->u.filter.fo_flags_lock);
273                 ctxt->loc_obd->u.filter.fo_mds_ost_sync = 0;
274                 cfs_spin_unlock(&ctxt->loc_obd->u.filter.fo_flags_lock);
275                 RETURN(0);
276         }
277
278         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
279                 CERROR("log is not plain\n");
280                 RETURN(-EINVAL);
281         }
282
283         OBD_FAIL_TIMEOUT(OBD_FAIL_OST_LLOG_RECOVERY_TIMEOUT, 30);
284         cookie.lgc_lgl = llh->lgh_id;
285         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
286         cookie.lgc_index = rec->lrh_index;
287
288         switch (rec->lrh_type) {
289         case MDS_UNLINK_REC:
290                 rc = filter_recov_log_unlink_cb(ctxt, rec, &cookie);
291                 break;
292         case MDS_SETATTR_REC:
293         case MDS_SETATTR64_REC:
294                 rc = filter_recov_log_setattr_cb(ctxt, rec, &cookie);
295                 break;
296         case LLOG_GEN_REC: {
297                 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
298                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
299                         rc = 0;
300                 else
301                         rc = LLOG_PROC_BREAK;
302                 CDEBUG(D_HA, "fetch generation log, send cookie\n");
303                 llog_cancel(ctxt, NULL, 1, &cookie, 0);
304                 RETURN(rc);
305                 }
306                 break;
307         default:
308                 CERROR("log record type %08x unknown\n", rec->lrh_type);
309                 RETURN(-EINVAL);
310                 break;
311         }
312
313         RETURN(rc);
314 }