Whamcloud - gitweb
file loop-sync-2.4.21-suse.patch was initially added on branch b1_2_smallfix.
[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                                 }
258                                 if (rc)
259                                         GOTO(out, rc);
260                         }
261
262                         /* next record, still in buffer? */
263                         ++index;
264                         if (index > last_index)
265                                 GOTO(out, rc = 0);
266                         rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
267                 }
268         }
269
270  out:
271         if (buf)
272                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
273         RETURN(rc);
274 }
275 EXPORT_SYMBOL(llog_process);
276
277 int llog_reverse_process(struct llog_handle *loghandle, llog_cb_t cb,
278                          void *data, void *catdata)
279 {
280         struct llog_log_hdr *llh = loghandle->lgh_hdr;
281         struct llog_process_cat_data *cd = catdata;
282         void *buf;
283         int rc = 0, first_index = 1, index, idx;
284         struct llog_rec_tail *tail;
285         ENTRY;
286
287         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
288         if (!buf)
289                 RETURN(-ENOMEM);
290
291         if (cd != NULL)
292                 first_index = cd->first_idx + 1;
293         if (cd != NULL && cd->last_idx)
294                 index = cd->last_idx;
295         else
296                 index = LLOG_BITMAP_BYTES * 8 - 1;
297
298         while (rc == 0) {
299                 struct llog_rec_hdr *rec;
300
301                 /* skip records not set in bitmap */
302                 while (index >= first_index &&
303                        !ext2_test_bit(index, llh->llh_bitmap))
304                         --index;
305
306                 LASSERT(index >= first_index - 1);
307                 if (index == first_index - 1)
308                         break;
309
310                 /* get the buf with our target record; avoid old garbage */
311                 memset(buf, 0, LLOG_CHUNK_SIZE);
312                 rc = llog_prev_block(loghandle, index, buf, LLOG_CHUNK_SIZE);
313                 if (rc)
314                         GOTO(out, rc);
315
316                 rec = buf;
317                 idx = le32_to_cpu(rec->lrh_index);
318                 if (idx < index)
319                         CDEBUG(D_HA, "index %u : idx %u\n", index, idx);
320                 while (idx < index) {
321                         rec = ((void *)rec + le32_to_cpu(rec->lrh_len));
322                         idx ++;
323                 }
324
325                 /* process records in buffer, starting where we found one */
326                 while ((void *)rec >= buf) {
327                         if (rec->lrh_index == 0)
328                                 GOTO(out, 0); /* no more records */
329
330                         /* if set, process the callback on this record */
331                         if (ext2_test_bit(index, llh->llh_bitmap)) {
332                                 rc = cb(loghandle, rec, data);
333                                 if (rc == LLOG_PROC_BREAK) {
334                                         CWARN("recovery from log: "LPX64":%x"
335                                               " stopped\n",
336                                               loghandle->lgh_id.lgl_oid,
337                                               loghandle->lgh_id.lgl_ogen);
338                                         GOTO(out, rc);
339                                 }
340                                 if (rc)
341                                         GOTO(out, rc);
342                         }
343
344                         /* previous record, still in buffer? */
345                         --index;
346                         if (index < first_index)
347                                 GOTO(out, rc = 0);
348
349                         if ((void *)rec == buf)
350                                 break;
351
352                         tail = (void *)rec - sizeof(struct llog_rec_tail);
353                         rec = ((void *)rec - le32_to_cpu(tail->lrt_len));
354                 }
355         }
356
357  out:
358         if (buf)
359                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
360         RETURN(rc);
361 }
362 EXPORT_SYMBOL(llog_reverse_process);