Whamcloud - gitweb
- mds->lmv->mdc propagate lower timeout down to import
[fs/lustre-release.git] / lustre / lvfs / 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/lustre_log.h>
42
43 /* Allocate a new log or catalog handle */
44 struct llog_handle *llog_alloc_handle(void)
45 {
46         struct llog_handle *loghandle;
47         ENTRY;
48
49         OBD_ALLOC(loghandle, sizeof(*loghandle));
50         if (loghandle == NULL)
51                 RETURN(ERR_PTR(-ENOMEM));
52
53         init_rwsem(&loghandle->lgh_lock);
54
55         RETURN(loghandle);
56 }
57 EXPORT_SYMBOL(llog_alloc_handle);
58
59 void llog_free_handle(struct llog_handle *loghandle)
60 {
61         if (!loghandle)
62                 return;
63
64         if (!loghandle->lgh_hdr)
65                 goto out;
66         if (le32_to_cpu(loghandle->lgh_hdr->llh_flags) & LLOG_F_IS_PLAIN)
67                 list_del_init(&loghandle->u.phd.phd_entry);
68         if (le32_to_cpu(loghandle->lgh_hdr->llh_flags) & LLOG_F_IS_CAT)
69                 LASSERT(list_empty(&loghandle->u.chd.chd_head));
70         OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
71
72  out:
73         OBD_FREE(loghandle, sizeof(*loghandle));
74 }
75 EXPORT_SYMBOL(llog_free_handle);
76
77 /* returns negative on error; 0 if success; 1 if success & log destroyed */
78 int llog_cancel_rec(struct llog_handle *loghandle, int index)
79 {
80         struct llog_log_hdr *llh = loghandle->lgh_hdr;
81         int rc = 0;
82         ENTRY;
83
84         CDEBUG(D_HA, "canceling %d in log "LPX64"\n",
85                index, loghandle->lgh_id.lgl_oid);
86
87         if (index == 0) {
88                 CERROR("cannot cancel index 0 (which is header)\n");
89                 RETURN(-EINVAL);
90         }
91
92         if (!ext2_clear_bit(index, llh->llh_bitmap)) {
93                 CERROR("catalog index %u already clear?\n", index);
94                 RETURN(-EINVAL);
95         }
96
97         llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) - 1);
98
99         if ((le32_to_cpu(llh->llh_flags) & LLOG_F_ZAP_WHEN_EMPTY) &&
100             (le32_to_cpu(llh->llh_count) == 1) &&
101             (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
102                 rc = llog_destroy(loghandle);
103                 if (rc)
104                         CERROR("failure destroying log after last cancel: %d\n",
105                                rc);
106                 LASSERT(rc == 0);
107                 RETURN(1);
108         }
109
110         rc = llog_write_rec(loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
111         if (rc)
112                 CERROR("failure re-writing header %d\n", rc);
113         LASSERT(rc == 0);
114         RETURN(rc);
115 }
116 EXPORT_SYMBOL(llog_cancel_rec);
117
118 int llog_init_handle(struct llog_handle *handle, int flags,
119                      struct obd_uuid *uuid)
120 {
121         int rc;
122         struct llog_log_hdr *llh;
123         ENTRY;
124         LASSERT(handle->lgh_hdr == NULL);
125
126         OBD_ALLOC(llh, sizeof(*llh));
127         if (llh == NULL)
128                 RETURN(-ENOMEM);
129         handle->lgh_hdr = llh;
130         /* first assign flags to use llog_client_ops */
131         llh->llh_flags = cpu_to_le32(flags);
132         rc = llog_read_header(handle);
133         if (rc == 0) {
134                 flags = le32_to_cpu(llh->llh_flags);
135                 if (uuid)
136                         LASSERT(obd_uuid_equals(uuid, &llh->llh_tgtuuid));
137                 GOTO(out, rc);
138         } else if (rc != LLOG_EEMPTY || !flags) {
139                 /* set a pesudo flag for initialization */
140                 flags = LLOG_F_IS_CAT;
141                 GOTO(out, rc);
142         }
143         rc = 0;
144
145         handle->lgh_last_idx = 0; /* header is record with index 0 */
146         llh->llh_count = cpu_to_le32(1);         /* for the header record */
147         llh->llh_hdr.lrh_type = cpu_to_le32(LLOG_HDR_MAGIC);
148         llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len =
149                 cpu_to_le32(LLOG_CHUNK_SIZE);
150         llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
151         llh->llh_timestamp = cpu_to_le64(LTIME_S(CURRENT_TIME));
152         if (uuid)
153                 memcpy(&llh->llh_tgtuuid, uuid, sizeof(llh->llh_tgtuuid));
154         llh->llh_bitmap_offset = cpu_to_le32(offsetof(typeof(*llh),llh_bitmap));
155         ext2_set_bit(0, llh->llh_bitmap);
156
157 out:
158         if (flags & LLOG_F_IS_CAT) {
159                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
160                 llh->llh_size = cpu_to_le32(sizeof(struct llog_logid_rec));
161         }
162         else if (flags & LLOG_F_IS_PLAIN)
163                 INIT_LIST_HEAD(&handle->u.phd.phd_entry);
164         else
165                 LBUG();
166
167         if (rc) {
168                 OBD_FREE(llh, sizeof(*llh));
169                 handle->lgh_hdr = NULL;
170         }
171         RETURN(rc);
172 }
173 EXPORT_SYMBOL(llog_init_handle);
174
175 int llog_close(struct llog_handle *loghandle)
176 {
177         struct llog_operations *lop;
178         int rc;
179         ENTRY;
180
181         rc = llog_handle2ops(loghandle, &lop);
182         if (rc)
183                 GOTO(out, rc);
184         if (lop->lop_close == NULL)
185                 GOTO(out, -EOPNOTSUPP);
186         rc = lop->lop_close(loghandle);
187  out:
188         llog_free_handle(loghandle);
189         RETURN(rc);
190 }
191 EXPORT_SYMBOL(llog_close);
192
193 int llog_process(struct llog_handle *loghandle, llog_cb_t cb,
194                  void *data, void *catdata)
195 {
196         struct llog_log_hdr *llh = loghandle->lgh_hdr;
197         struct llog_process_cat_data *cd = catdata;
198         void *buf;
199         __u64 cur_offset = LLOG_CHUNK_SIZE;
200         int rc = 0, index = 1, last_index, idx;
201         int saved_index = 0;
202         ENTRY;
203
204         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
205         if (!buf)
206                 RETURN(-ENOMEM);
207
208         if (cd != NULL)
209                 index = cd->first_idx + 1;
210         if (cd != NULL && cd->last_idx)
211                 last_index = cd->last_idx;
212         else
213                 last_index = LLOG_BITMAP_BYTES * 8 - 1;
214
215         while (rc == 0) {
216                 struct llog_rec_hdr *rec;
217
218                 /* skip records not set in bitmap */
219                 while (index <= last_index &&
220                        !ext2_test_bit(index, llh->llh_bitmap))
221                         ++index;
222
223                 LASSERT(index <= last_index + 1);
224                 if (index == last_index + 1)
225                         break;
226
227                 /* get the buf with our target record; avoid old garbage */
228                 memset(buf, 0, LLOG_CHUNK_SIZE);
229                 rc = llog_next_block(loghandle, &saved_index, index,
230                                      &cur_offset, buf, LLOG_CHUNK_SIZE);
231                 if (rc)
232                         GOTO(out, rc);
233
234                 rec = buf;
235                 idx = le32_to_cpu(rec->lrh_index);
236                 if (idx < index)
237                         CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
238                 while (idx < index) {
239                         rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
240                         idx ++;
241                 }
242
243                 /* process records in buffer, starting where we found one */
244                 while ((void *)rec < buf + LLOG_CHUNK_SIZE) {
245                         if (rec->lrh_index == 0)
246                                 GOTO(out, 0); /* no more records */
247
248                         /* if set, process the callback on this record */
249                         if (ext2_test_bit(index, llh->llh_bitmap)) {
250                                 rc = cb(loghandle, rec, data);
251                                 if (rc == LLOG_PROC_BREAK) {
252                                         CWARN("recovery from log: "LPX64":%x"
253                                               " stopped\n",
254                                               loghandle->lgh_id.lgl_oid,
255                                               loghandle->lgh_id.lgl_ogen);
256                                         GOTO(out, rc);
257                                 } else if (rc == LLOG_DEL_RECORD) {
258                                         llog_cancel_rec(loghandle, rec->lrh_index);
259                                         rc = 0;
260                                 } 
261                                 if (rc)
262                                         GOTO(out, rc);
263                         }
264                         /* next record, still in buffer? */
265                         ++index;
266                         if (index > last_index)
267                                 GOTO(out, rc = 0);
268                         rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
269                 }
270         }
271
272  out:
273         if (buf)
274                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
275         RETURN(rc);
276 }
277 EXPORT_SYMBOL(llog_process);
278
279 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
280                          void *data, void *catdata)
281 {
282         struct llog_log_hdr *llh = loghandle->lgh_hdr;
283         struct llog_process_cat_data *cd = catdata;
284         void *buf;
285         int rc = 0, first_index = 1, index, idx;
286         struct llog_rec_tail *tail;
287         ENTRY;
288
289         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
290         if (!buf)
291                 RETURN(-ENOMEM);
292
293         if (cd != NULL)
294                 first_index = cd->first_idx + 1;
295         if (cd != NULL && cd->last_idx)
296                 index = cd->last_idx;
297         else
298                 index = LLOG_BITMAP_BYTES * 8 - 1;
299
300         while (rc == 0) {
301                 struct llog_rec_hdr *rec;
302
303                 /* skip records not set in bitmap */
304                 while (index >= first_index &&
305                        !ext2_test_bit(index, llh->llh_bitmap))
306                         --index;
307
308                 LASSERT(index >= first_index - 1);
309                 if (index == first_index - 1)
310                         break;
311
312                 /* get the buf with our target record; avoid old garbage */
313                 memset(buf, 0, LLOG_CHUNK_SIZE);
314                 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
315                 if (rc)
316                         GOTO(out, rc);
317
318                 rec = buf;
319                 idx = le32_to_cpu(rec->lrh_index);
320                 if (idx < index)
321                         CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
322                 while (idx < index) {
323                         rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
324                         idx ++;
325                 }
326
327                 /* process records in buffer, starting where we found one */
328                 while ((void *)rec >= buf) {
329                         if (rec->lrh_index == 0)
330                                 GOTO(out, 0); /* no more records */
331
332                         /* if set, process the callback on this record */
333                         if (ext2_test_bit(index, llh->llh_bitmap)) {
334                                 rc = cb(loghandle, rec, data);
335                                 if (rc == LLOG_PROC_BREAK) {
336                                         CWARN("recovery from log: "LPX64":%x"
337                                               " stopped\n",
338                                               loghandle->lgh_id.lgl_oid,
339                                               loghandle->lgh_id.lgl_ogen);
340                                         GOTO(out, rc);
341                                 }
342                                 if (rc)
343                                         GOTO(out, rc);
344                         }
345
346                         /* previous record, still in buffer? */
347                         --index;
348                         if (index < first_index)
349                                 GOTO(out, rc = 0);
350
351                         if ((void *)rec == buf)
352                                 break;
353
354                         tail = (void *)rec - sizeof(struct llog_rec_tail);
355                         rec = ((void *)rec - le32_to_cpu(tail->lrt_len));
356                 }
357         }
358
359  out:
360         if (buf)
361                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
362         RETURN(rc);
363 }
364 EXPORT_SYMBOL(llog_reverse_process);