Whamcloud - gitweb
* Compiles after merging b1_4
[fs/lustre-release.git] / lustre / obdclass / llog.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  *  Copyright (C) 2001-2003 Cluster File Systems, Inc.
5  *   Author: Andreas Dilger <adilger@clusterfs.com>
6  *
7  *   This file is part of Lustre, http://www.lustre.org.
8  *
9  *   Lustre is free software; you can redistribute it and/or
10  *   modify it under the terms of version 2 of the GNU General Public
11  *   License as published by the Free Software Foundation.
12  *
13  *   Lustre is distributed in the hope that it will be useful,
14  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *   GNU General Public License for more details.
17  *
18  *   You should have received a copy of the GNU General Public License
19  *   along with Lustre; if not, write to the Free Software
20  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  *
22  * OST<->MDS recovery logging infrastructure.
23  *
24  * Invariants in implementation:
25  * - we do not share logs among different OST<->MDS connections, so that
26  *   if an OST or MDS fails it need only look at log(s) relevant to itself
27  */
28
29 #define DEBUG_SUBSYSTEM S_LOG
30
31 #ifndef EXPORT_SYMTAB
32 #define EXPORT_SYMTAB
33 #endif
34
35 #ifdef __KERNEL__
36 #include <linux/fs.h>
37 #else
38 #include <liblustre.h>
39 #endif
40
41 #include <linux/obd_class.h>
42 #include <linux/lustre_log.h>
43 #include <libcfs/list.h>
44
45 /* Allocate a new log or catalog handle */
46 struct llog_handle *llog_alloc_handle(void)
47 {
48         struct llog_handle *loghandle;
49         ENTRY;
50
51         OBD_ALLOC(loghandle, sizeof(*loghandle));
52         if (loghandle == NULL)
53                 RETURN(ERR_PTR(-ENOMEM));
54
55         init_rwsem(&loghandle->lgh_lock);
56
57         RETURN(loghandle);
58 }
59 EXPORT_SYMBOL(llog_alloc_handle);
60
61
62 void llog_free_handle(struct llog_handle *loghandle)
63 {
64         if (!loghandle)
65                 return;
66
67         if (!loghandle->lgh_hdr)
68                 goto out;
69         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
70                 list_del_init(&loghandle->u.phd.phd_entry);
71         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
72                 LASSERT(list_empty(&loghandle->u.chd.chd_head));
73         OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
74
75  out:
76         OBD_FREE(loghandle, sizeof(*loghandle));
77 }
78 EXPORT_SYMBOL(llog_free_handle);
79
80 /* returns negative on error; 0 if success; 1 if success & log destroyed */
81 int llog_cancel_rec(struct llog_handle *loghandle, int index)
82 {
83         struct llog_log_hdr *llh = loghandle->lgh_hdr;
84         int rc = 0;
85         ENTRY;
86
87         CDEBUG(D_HA, "canceling %d in log "LPX64"\n",
88                index, loghandle->lgh_id.lgl_oid);
89
90         if (index == 0) {
91                 CERROR("cannot cancel index 0 (which is header)\n");
92                 RETURN(-EINVAL);
93         }
94
95         if (!ext2_clear_bit(index, llh->llh_bitmap)) {
96                 CDEBUG(D_HA, "catalog index %u already clear?\n", index);
97                 RETURN(-EINVAL);
98         }
99
100         llh->llh_count--;
101
102         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
103             (llh->llh_count == 1) &&
104             (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
105                 rc = llog_destroy(loghandle);
106                 if (rc) {
107                         CERROR("failure destroying log after last cancel: %d\n",
108                                rc);
109                         ext2_set_bit(index, llh->llh_bitmap);
110                         llh->llh_count++;
111                 } else {
112                         rc = 1;
113                 }
114                 RETURN(rc);
115         }
116
117         rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
118         if (rc) {
119                 CERROR("failure re-writing header %d\n", rc);
120                 ext2_set_bit(index, llh->llh_bitmap);
121                 llh->llh_count++;
122         }
123         RETURN(rc);
124 }
125 EXPORT_SYMBOL(llog_cancel_rec);
126
127 int llog_init_handle(struct llog_handle *handle, int flags,
128                      struct obd_uuid *uuid)
129 {
130         int rc;
131         struct llog_log_hdr *llh;
132         ENTRY;
133         LASSERT(handle->lgh_hdr == NULL);
134
135         OBD_ALLOC(llh, sizeof(*llh));
136         if (llh == NULL)
137                 RETURN(-ENOMEM);
138         handle->lgh_hdr = llh;
139         /* first assign flags to use llog_client_ops */
140         llh->llh_flags = flags;
141         rc = llog_read_header(handle);
142         if (rc == 0) {
143                 flags = llh->llh_flags;
144                 if (uuid)
145                         LASSERT(obd_uuid_equals(uuid, &llh->llh_tgtuuid));
146                 GOTO(out, rc);
147         } else if (rc != LLOG_EEMPTY || !flags) {
148                 /* set a pesudo flag for initialization */
149                 flags = LLOG_F_IS_CAT;
150                 GOTO(out, rc);
151         }
152         rc = 0;
153
154         handle->lgh_last_idx = 0; /* header is record with index 0 */
155         llh->llh_count = 1;         /* for the header record */
156         llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
157         llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
158         llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
159         llh->llh_timestamp = CURRENT_SECONDS;
160         if (uuid)
161                 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
162         llh->llh_bitmap_offset = offsetof(typeof(*llh),llh_bitmap);
163         ext2_set_bit(0, llh->llh_bitmap);
164
165 out:
166         if (flags & LLOG_F_IS_CAT) {
167                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
168                 llh->llh_size = sizeof(struct llog_logid_rec);
169         } else if (flags & LLOG_F_IS_PLAIN) {
170                 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
171         } else {
172                 CERROR("Unknown flags: %#x (Expected %#x or %#x\n",
173                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
174                 LBUG();
175         }
176
177         if (rc) {
178                 OBD_FREE(llh, sizeof(*llh));
179                 handle->lgh_hdr = NULL;
180         }
181         RETURN(rc);
182 }
183 EXPORT_SYMBOL(llog_init_handle);
184
185 int llog_close(struct llog_handle *loghandle)
186 {
187         struct llog_operations *lop;
188         int rc;
189         ENTRY;
190
191         rc = llog_handle2ops(loghandle, &lop);
192         if (rc)
193                 GOTO(out, rc);
194         if (lop->lop_close == NULL)
195                 GOTO(out, -EOPNOTSUPP);
196         rc = lop->lop_close(loghandle);
197  out:
198         llog_free_handle(loghandle);
199         RETURN(rc);
200 }
201 EXPORT_SYMBOL(llog_close);
202
203 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
204                  void *data, void *catdata)
205 {
206         struct llog_log_hdr *llh = loghandle->lgh_hdr;
207         struct llog_process_cat_data *cd = catdata;
208         char *buf;
209         __u64 cur_offset = LLOG_CHUNK_SIZE;
210         int rc = 0, index = 1, last_index;
211         int saved_index = 0;
212         ENTRY;
213
214         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
215         if (!buf)
216                 RETURN(-ENOMEM);
217
218         if (cd != NULL)
219                 index = cd->first_idx + 1;
220         if (cd != NULL && cd->last_idx)
221                 last_index = cd->last_idx;
222         else
223                 last_index = LLOG_BITMAP_BYTES * 8 - 1;
224
225         while (rc == 0) {
226                 struct llog_rec_hdr *rec;
227
228                 /* skip records not set in bitmap */
229                 while (index <= last_index &&
230                        !ext2_test_bit(index, llh->llh_bitmap))
231                         ++index;
232
233                 LASSERT(index <= last_index + 1);
234                 if (index == last_index + 1)
235                         break;
236
237                 CDEBUG(D_OTHER, "index: %d last_index %d\n",
238                        index, last_index);
239
240                 /* get the buf with our target record; avoid old garbage */
241                 memset(buf, 0, LLOG_CHUNK_SIZE);
242                 rc = llog_next_block(loghandle, &saved_index, index,
243                                      &cur_offset, buf, LLOG_CHUNK_SIZE);
244                 if (rc)
245                         GOTO(out, rc);
246
247                 /* NB: when rec->lrh_len is accessed it is already swabbed
248                  * since it is used at the "end" of the loop and the rec
249                  * swabbing is done at the beginning of the loop. */
250                 for (rec = (struct llog_rec_hdr *)buf;
251                      (char *)rec < buf + LLOG_CHUNK_SIZE;
252                      rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
253
254                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
255                                rec, rec->lrh_type);
256
257                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
258                                 lustre_swab_llog_rec(rec, NULL);
259
260                         CDEBUG(D_OTHER, "after swabbing, type: %#x\n",
261                                rec->lrh_type);
262
263                         if (rec->lrh_index == 0)
264                                 GOTO(out, 0); /* no more records */
265
266                         if (rec->lrh_index < index) {
267                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
268                                        rec->lrh_index);
269                                 continue;
270                         }
271
272                         CDEBUG(D_OTHER,
273                                "lrh_index: %d lrh_len: %d (%d remains)\n",
274                                rec->lrh_index, rec->lrh_len,
275                                (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
276
277                         /* if set, process the callback on this record */
278                         if (ext2_test_bit(index, llh->llh_bitmap)) {
279                                 rc = cb(loghandle, rec, data);
280                                 if (rc == LLOG_PROC_BREAK) {
281                                         CWARN("recovery from log: "LPX64":%x"
282                                               " stopped\n",
283                                               loghandle->lgh_id.lgl_oid,
284                                               loghandle->lgh_id.lgl_ogen);
285                                         GOTO(out, rc);
286                                 }
287                                 if (rc)
288                                         GOTO(out, rc);
289                         } else {
290                                 CDEBUG(D_OTHER, "Skipped index %d\n", index);
291                         }
292
293                         /* next record, still in buffer? */
294                         ++index;
295                         if (index > last_index)
296                                 GOTO(out, rc = 0);
297                 }
298         }
299
300  out:
301         if (buf)
302                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
303         RETURN(rc);
304 }
305 EXPORT_SYMBOL(llog_process);