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