Whamcloud - gitweb
Branch b1_4_mountconf
[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 the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  * OST<->MDS recovery logging infrastructure.
26  *
27  * Invariants in implementation:
28  * - we do not share logs among different OST<->MDS connections, so that
29  *   if an OST or MDS fails it need only look at log(s) relevant to itself
30  */
31
32 #define DEBUG_SUBSYSTEM S_LOG
33
34 #ifndef EXPORT_SYMTAB
35 #define EXPORT_SYMTAB
36 #endif
37
38 #ifdef __KERNEL__
39 #include <linux/fs.h>
40 #else
41 #include <liblustre.h>
42 #endif
43
44 #include <linux/obd_class.h>
45 #include <linux/lustre_log.h>
46 #include <libcfs/list.h>
47
48 /* Allocate a new log or catalog handle */
49 struct llog_handle *llog_alloc_handle(void)
50 {
51         struct llog_handle *loghandle;
52         ENTRY;
53
54         OBD_ALLOC(loghandle, sizeof(*loghandle));
55         if (loghandle == NULL)
56                 RETURN(ERR_PTR(-ENOMEM));
57
58         init_rwsem(&loghandle->lgh_lock);
59
60         RETURN(loghandle);
61 }
62 EXPORT_SYMBOL(llog_alloc_handle);
63
64
65 void llog_free_handle(struct llog_handle *loghandle)
66 {
67         if (!loghandle)
68                 return;
69
70         if (!loghandle->lgh_hdr)
71                 goto out;
72         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
73                 list_del_init(&loghandle->u.phd.phd_entry);
74         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
75                 LASSERT(list_empty(&loghandle->u.chd.chd_head));
76         OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
77
78  out:
79         OBD_FREE(loghandle, sizeof(*loghandle));
80 }
81 EXPORT_SYMBOL(llog_free_handle);
82
83 /* returns negative on error; 0 if success; 1 if success & log destroyed */
84 int llog_cancel_rec(struct llog_handle *loghandle, int index)
85 {
86         struct llog_log_hdr *llh = loghandle->lgh_hdr;
87         int rc = 0;
88         ENTRY;
89
90         CDEBUG(D_HA, "canceling %d in log "LPX64"\n",
91                index, loghandle->lgh_id.lgl_oid);
92
93         if (index == 0) {
94                 CERROR("cannot cancel index 0 (which is header)\n");
95                 RETURN(-EINVAL);
96         }
97
98         if (!ext2_clear_bit(index, llh->llh_bitmap)) {
99                 CDEBUG(D_HA, "catalog index %u already clear?\n", index);
100                 RETURN(-EINVAL);
101         }
102
103         llh->llh_count--;
104
105         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
106             (llh->llh_count == 1) &&
107             (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
108                 rc = llog_destroy(loghandle);
109                 if (rc) {
110                         CERROR("failure destroying log after last cancel: %d\n",
111                                rc);
112                         ext2_set_bit(index, llh->llh_bitmap);
113                         llh->llh_count++;
114                 } else {
115                         rc = 1;
116                 }
117                 RETURN(rc);
118         }
119
120         rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
121         if (rc) {
122                 CERROR("failure re-writing header %d\n", rc);
123                 ext2_set_bit(index, llh->llh_bitmap);
124                 llh->llh_count++;
125         }
126         RETURN(rc);
127 }
128 EXPORT_SYMBOL(llog_cancel_rec);
129
130 int llog_init_handle(struct llog_handle *handle, int flags,
131                      struct obd_uuid *uuid)
132 {
133         int rc;
134         struct llog_log_hdr *llh;
135         ENTRY;
136         LASSERT(handle->lgh_hdr == NULL);
137
138         OBD_ALLOC(llh, sizeof(*llh));
139         if (llh == NULL)
140                 RETURN(-ENOMEM);
141         handle->lgh_hdr = llh;
142         /* first assign flags to use llog_client_ops */
143         llh->llh_flags = flags;
144         rc = llog_read_header(handle);
145         if (rc == 0) {
146                 flags = llh->llh_flags;
147                 if (uuid)
148                         LASSERT(obd_uuid_equals(uuid, &llh->llh_tgtuuid));
149                 GOTO(out, rc);
150         } else if (rc != LLOG_EEMPTY || !flags) {
151                 /* set a pesudo flag for initialization */
152                 flags = LLOG_F_IS_CAT;
153                 GOTO(out, rc);
154         }
155         rc = 0;
156
157         handle->lgh_last_idx = 0; /* header is record with index 0 */
158         llh->llh_count = 1;         /* for the header record */
159         llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
160         llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
161         llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
162         llh->llh_timestamp = CURRENT_SECONDS;
163         if (uuid)
164                 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
165         llh->llh_bitmap_offset = offsetof(typeof(*llh),llh_bitmap);
166         ext2_set_bit(0, llh->llh_bitmap);
167
168 out:
169         if (flags & LLOG_F_IS_CAT) {
170                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
171                 llh->llh_size = sizeof(struct llog_logid_rec);
172         } else if (flags & LLOG_F_IS_PLAIN) {
173                 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
174         } else {
175                 CERROR("Unknown flags: %#x (Expected %#x or %#x\n",
176                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
177                 LBUG();
178         }
179
180         if (rc) {
181                 OBD_FREE(llh, sizeof(*llh));
182                 handle->lgh_hdr = NULL;
183         }
184         RETURN(rc);
185 }
186 EXPORT_SYMBOL(llog_init_handle);
187
188 int llog_close(struct llog_handle *loghandle)
189 {
190         struct llog_operations *lop;
191         int rc;
192         ENTRY;
193
194         rc = llog_handle2ops(loghandle, &lop);
195         if (rc)
196                 GOTO(out, rc);
197         if (lop->lop_close == NULL)
198                 GOTO(out, -EOPNOTSUPP);
199         rc = lop->lop_close(loghandle);
200  out:
201         llog_free_handle(loghandle);
202         RETURN(rc);
203 }
204 EXPORT_SYMBOL(llog_close);
205
206 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
207                  void *data, void *catdata)
208 {
209         struct llog_log_hdr *llh = loghandle->lgh_hdr;
210         struct llog_process_cat_data *cd = catdata;
211         char *buf;
212         __u64 cur_offset = LLOG_CHUNK_SIZE;
213         int rc = 0, index = 1, last_index;
214         int saved_index = 0, last_called_index = 0;
215         ENTRY;
216
217         LASSERT(llh);
218
219         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
220         if (!buf)
221                 RETURN(-ENOMEM);
222
223         if (cd != NULL) {
224                 last_called_index = cd->first_idx;
225                 index = cd->first_idx + 1;
226         }
227         if (cd != NULL && cd->last_idx)
228                 last_index = cd->last_idx;
229         else
230                 last_index = LLOG_BITMAP_BYTES * 8 - 1;
231
232         while (rc == 0) {
233                 struct llog_rec_hdr *rec;
234
235                 /* skip records not set in bitmap */
236                 while (index <= last_index &&
237                        !ext2_test_bit(index, llh->llh_bitmap))
238                         ++index;
239
240                 LASSERT(index <= last_index + 1);
241                 if (index == last_index + 1)
242                         break;
243
244                 CDEBUG(D_OTHER, "index: %d last_index %d\n",
245                        index, last_index);
246
247                 /* get the buf with our target record; avoid old garbage */
248                 memset(buf, 0, LLOG_CHUNK_SIZE);
249                 rc = llog_next_block(loghandle, &saved_index, index,
250                                      &cur_offset, buf, LLOG_CHUNK_SIZE);
251                 if (rc)
252                         GOTO(out, rc);
253
254                 /* NB: when rec->lrh_len is accessed it is already swabbed
255                  * since it is used at the "end" of the loop and the rec
256                  * swabbing is done at the beginning of the loop. */
257                 for (rec = (struct llog_rec_hdr *)buf;
258                      (char *)rec < buf + LLOG_CHUNK_SIZE;
259                      rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
260
261                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
262                                rec, rec->lrh_type);
263
264                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
265                                 lustre_swab_llog_rec(rec, NULL);
266
267                         CDEBUG(D_OTHER, "after swabbing, type: %#x\n",
268                                rec->lrh_type);
269
270                         if (rec->lrh_index == 0)
271                                 GOTO(out, 0); /* no more records */
272
273                         if (rec->lrh_len == 0 || rec->lrh_len >LLOG_CHUNK_SIZE){
274                                 CWARN("invalid length %d in llog record for "
275                                       "index %d\n", rec->lrh_len,
276                                 rec->lrh_index);
277                                 GOTO(out, 0);
278                         }
279
280                         if (rec->lrh_index < index) {
281                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
282                                        rec->lrh_index);
283                                 continue;
284                         }
285
286                         CDEBUG(D_OTHER,
287                                "lrh_index: %d lrh_len: %d (%d remains)\n",
288                                rec->lrh_index, rec->lrh_len,
289                                (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
290
291                         /* if set, process the callback on this record */
292                         if (ext2_test_bit(index, llh->llh_bitmap)) {
293                                 rc = cb(loghandle, rec, data);
294                                 last_called_index = index;
295                                 if (rc == LLOG_PROC_BREAK) {
296                                         CWARN("recovery from log: "LPX64":%x"
297                                               " stopped\n",
298                                               loghandle->lgh_id.lgl_oid,
299                                               loghandle->lgh_id.lgl_ogen);
300                                         GOTO(out, rc);
301                                 } else if (rc == LLOG_DEL_RECORD) {
302                                         llog_cancel_rec(loghandle, rec->lrh_index);
303                                         rc = 0;
304                                 }
305                                 if (rc)
306                                         GOTO(out, rc);
307                         } else {
308                                 CDEBUG(D_OTHER, "Skipped index %d\n", index);
309                         }
310
311                         /* next record, still in buffer? */
312                         ++index;
313                         if (index > last_index)
314                                 GOTO(out, rc = 0);
315                 }
316         }
317
318  out:
319         if (cd != NULL)
320                 cd->last_idx = last_called_index;
321         if (buf)
322                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
323         RETURN(rc);
324 }
325 EXPORT_SYMBOL(llog_process);
326
327 int llog_get_size(struct llog_handle *loghandle)
328 {
329         if (loghandle && loghandle->lgh_hdr)
330                 return loghandle->lgh_hdr->llh_count;
331         return 0;
332 }
333 EXPORT_SYMBOL(llog_get_size);
334
335 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
336                          void *data, void *catdata)
337 {
338         struct llog_log_hdr *llh = loghandle->lgh_hdr;
339         struct llog_process_cat_data *cd = catdata;
340         void *buf;
341         int rc = 0, first_index = 1, index, idx;
342         struct llog_rec_tail *tail;
343         ENTRY;
344
345         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
346         if (!buf)
347                 RETURN(-ENOMEM);
348
349         if (cd != NULL)
350                 first_index = cd->first_idx + 1;
351         if (cd != NULL && cd->last_idx)
352                 index = cd->last_idx;
353         else
354                 index = LLOG_BITMAP_BYTES * 8 - 1;
355
356         while (rc == 0) {
357                 struct llog_rec_hdr *rec;
358
359                 /* skip records not set in bitmap */
360                 while (index >= first_index &&
361                        !ext2_test_bit(index, llh->llh_bitmap))
362                         --index;
363
364                 LASSERT(index >= first_index - 1);
365                 if (index == first_index - 1)
366                         break;
367
368                 /* get the buf with our target record; avoid old garbage */
369                 memset(buf, 0, LLOG_CHUNK_SIZE);
370                 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
371                 if (rc)
372                         GOTO(out, rc);
373
374                 rec = buf;
375                 idx = le32_to_cpu(rec->lrh_index);
376                 if (idx < index)
377                         CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
378                 while (idx < index) {
379                         rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
380                         idx ++;
381                 }
382
383                 /* process records in buffer, starting where we found one */
384                 while ((void *)rec >= buf) {
385                         if (rec->lrh_index == 0)
386                                 GOTO(out, 0); /* no more records */
387
388                         /* if set, process the callback on this record */
389                         if (ext2_test_bit(index, llh->llh_bitmap)) {
390                                 rc = cb(loghandle, rec, data);
391                                 if (rc == LLOG_PROC_BREAK) {
392                                         CWARN("recovery from log: "LPX64":%x"
393                                               " stopped\n",
394                                               loghandle->lgh_id.lgl_oid,
395                                               loghandle->lgh_id.lgl_ogen);
396                                         GOTO(out, rc);
397                                 }
398                                 if (rc)
399                                         GOTO(out, rc);
400                         }
401
402                         /* previous record, still in buffer? */
403                         --index;
404                         if (index < first_index)
405                                 GOTO(out, rc = 0);
406                         tail = (void *)rec - sizeof(struct llog_rec_tail);
407                         rec = ((void *)rec - le32_to_cpu(tail->lrt_len));
408                 }
409         }
410
411 out:
412         if (buf)
413                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
414         RETURN(rc);
415 }
416 EXPORT_SYMBOL(llog_reverse_process);