Whamcloud - gitweb
Branch b_new_cmd
[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 the Lustre file system, http://www.lustre.org
12  *   Lustre is a trademark of Cluster File Systems, Inc.
13  *
14  *   You may have signed or agreed to another license before downloading
15  *   this software.  If so, you are bound by the terms and conditions
16  *   of that agreement, and the following does not apply to you.  See the
17  *   LICENSE file included with this distribution for more information.
18  *
19  *   If you did not agree to a different license, then this copy of Lustre
20  *   is open source software; you can redistribute it and/or modify it
21  *   under the terms of version 2 of the GNU General Public License as
22  *   published by the Free Software Foundation.
23  *
24  *   In either case, Lustre is distributed in the hope that it will be
25  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
26  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
27  *   license text for more details.
28  */
29
30 #define DEBUG_SUBSYSTEM S_FILTER
31
32 #include <linux/config.h>
33 #include <linux/module.h>
34 #include <linux/version.h>
35
36 #include <libcfs/list.h>
37 #include <obd_class.h>
38 #include <lustre_fsfilt.h>
39 #include <lustre_commit_confd.h>
40
41 #include "filter_internal.h"
42
43 int filter_log_sz_change(struct llog_handle *cathandle,
44                          struct ll_fid *mds_fid,
45                          __u32 ioepoch,
46                          struct llog_cookie *logcookie,
47                          struct inode *inode)
48 {
49         struct llog_size_change_rec *lsc;
50         int rc;
51         struct ost_filterdata *ofd;
52         ENTRY;
53
54         LOCK_INODE_MUTEX(inode);
55         ofd = inode->i_filterdata;
56
57         if (ofd && ofd->ofd_epoch >= ioepoch) {
58                 if (ofd->ofd_epoch > ioepoch)
59                         CERROR("client sent old epoch %d for obj ino %ld\n",
60                                ioepoch, inode->i_ino);
61                 UNLOCK_INODE_MUTEX(inode);
62                 RETURN(0);
63         }
64
65         if (ofd && ofd->ofd_epoch < ioepoch) {
66                 ofd->ofd_epoch = ioepoch;
67         } else if (!ofd) {
68                 OBD_ALLOC(ofd, sizeof(*ofd));
69                 if (!ofd)
70                         GOTO(out, rc = -ENOMEM);
71                 igrab(inode);
72                 inode->i_filterdata = ofd;
73                 ofd->ofd_epoch = ioepoch;
74         }
75         /* the decision to write a record is now made, unlock */
76         UNLOCK_INODE_MUTEX(inode);
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_fid = *mds_fid;
84         lsc->lsc_ioepoch = ioepoch;
85
86         rc = llog_cat_add_rec(cathandle, &lsc->lsc_hdr, logcookie, NULL);
87         OBD_FREE(lsc, sizeof(*lsc));
88
89         if (rc > 0) {
90                 LASSERT(rc == sizeof(*logcookie));
91                 rc = 0;
92         }
93
94         out:
95         RETURN(rc);
96 }
97
98 /* When this (destroy) operation is committed, return the cancel cookie */
99 void filter_cancel_cookies_cb(struct obd_device *obd, __u64 transno,
100                               void *cb_data, int error)
101 {
102         struct llog_cookie *cookie = cb_data;
103         int rc;
104
105         if (error != 0) {
106                 CDEBUG(D_INODE, "not cancelling llog cookie on error %d\n",
107                        error);
108                 return;
109         }
110
111         rc = llog_cancel(llog_get_context(obd, cookie->lgc_subsys + 1),
112                          NULL, 1, cookie, 0);
113         if (rc)
114                 CERROR("error cancelling log cookies: rc = %d\n", rc);
115         OBD_FREE(cookie, sizeof(*cookie));
116 }
117
118 /* Callback for processing the unlink log record received from MDS by 
119  * llog_client_api. */
120 static int filter_recov_log_unlink_cb(struct llog_ctxt *ctxt,
121                                       struct llog_rec_hdr *rec,
122                                       struct llog_cookie *cookie)
123 {
124         struct obd_device *obd = ctxt->loc_obd;
125         struct obd_export *exp = obd->obd_self_export;
126         struct llog_unlink_rec *lur;
127         struct obdo *oa;
128         obd_id oid;
129         int rc = 0;
130         ENTRY;
131
132         lur = (struct llog_unlink_rec *)rec;
133         oa = obdo_alloc();
134         if (oa == NULL) 
135                 RETURN(-ENOMEM);
136         oa->o_valid |= OBD_MD_FLCOOKIE;
137         oa->o_id = lur->lur_oid;
138         oa->o_gr = lur->lur_ogen;
139         oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
140         memcpy(obdo_logcookie(oa), cookie, sizeof(*cookie));
141         oid = oa->o_id;
142
143         rc = filter_destroy(exp, oa, NULL, NULL, NULL);
144         obdo_free(oa);
145         if (rc == -ENOENT) {
146                 CDEBUG(D_HA, "object already removed, send cookie\n");
147                 llog_cancel(ctxt, NULL, 1, cookie, 0);
148                 RETURN(0);
149         }
150
151         if (rc == 0)
152                 CDEBUG(D_HA, "object: "LPU64" in record is destroyed\n", oid);
153
154         RETURN(rc);
155 }
156
157 /* Callback for processing the setattr log record received from MDS by
158  * llog_client_api. */
159 static int filter_recov_log_setattr_cb(struct llog_ctxt *ctxt,
160                                        struct llog_rec_hdr *rec,
161                                        struct llog_cookie *cookie)
162 {
163         struct obd_device *obd = ctxt->loc_obd;
164         struct obd_export *exp = obd->obd_self_export;
165         struct llog_setattr_rec *lsr;
166         struct obd_info oinfo = { { { 0 } } };
167         obd_id oid;
168         int rc = 0;
169         ENTRY;
170
171         lsr = (struct llog_setattr_rec *)rec;
172         oinfo.oi_oa = obdo_alloc();
173
174         oinfo.oi_oa->o_valid |= (OBD_MD_FLID | OBD_MD_FLUID | OBD_MD_FLGID |
175                                  OBD_MD_FLCOOKIE);
176         oinfo.oi_oa->o_id = lsr->lsr_oid;
177         oinfo.oi_oa->o_gr = lsr->lsr_ogen;
178         oinfo.oi_oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP;
179         oinfo.oi_oa->o_uid = lsr->lsr_uid;
180         oinfo.oi_oa->o_gid = lsr->lsr_gid;
181         memcpy(obdo_logcookie(oinfo.oi_oa), cookie, sizeof(*cookie));
182         oid = oinfo.oi_oa->o_id;
183
184         rc = filter_setattr(exp, &oinfo, NULL);
185         obdo_free(oinfo.oi_oa);
186
187         if (rc == -ENOENT) {
188                 CDEBUG(D_HA, "object already removed, send cookie\n");
189                 llog_cancel(ctxt, NULL, 1, cookie, 0);
190                 RETURN(0);
191         }
192
193         if (rc == 0)
194                 CDEBUG(D_HA, "object: "LPU64" in record is chown/chgrp\n", oid);
195
196         RETURN(rc);
197 }
198
199 int filter_recov_log_mds_ost_cb(struct llog_handle *llh,
200                                struct llog_rec_hdr *rec, void *data)
201 {
202         struct llog_ctxt *ctxt = llh->lgh_ctxt;
203         struct llog_cookie cookie;
204         int rc = 0;
205         ENTRY;
206
207         if (!(llh->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)) {
208                 CERROR("log is not plain\n");
209                 RETURN(-EINVAL);
210         }
211
212         cookie.lgc_lgl = llh->lgh_id;
213         cookie.lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
214         cookie.lgc_index = rec->lrh_index;
215
216         switch (rec->lrh_type) {
217         case MDS_UNLINK_REC:
218                 rc = filter_recov_log_unlink_cb(ctxt, rec, &cookie);
219                 break;
220         case MDS_SETATTR_REC:
221                 rc = filter_recov_log_setattr_cb(ctxt, rec, &cookie);
222                 break;
223         case LLOG_GEN_REC: {
224                 struct llog_gen_rec *lgr = (struct llog_gen_rec *)rec;
225                 if (llog_gen_lt(lgr->lgr_gen, ctxt->loc_gen))
226                         rc = 0;
227                 else
228                         rc = LLOG_PROC_BREAK;
229                 CWARN("fetch generation log, send cookie\n");
230                 llog_cancel(ctxt, NULL, 1, &cookie, 0);
231                 RETURN(rc);
232                 }
233                 break;
234         default:
235                 CERROR("log record type %08x unknown\n", rec->lrh_type);
236                 RETURN(-EINVAL);
237                 break;
238         }
239
240         RETURN(rc);
241 }