Whamcloud - gitweb
LU-1302 llog: modify llog_write/llog_add to support OSD
[fs/lustre-release.git] / lustre / obdclass / llog_cat.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel, Inc.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog_cat.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  *
40  * Invariants in implementation:
41  * - we do not share logs among different OST<->MDS connections, so that
42  *   if an OST or MDS fails it need only look at log(s) relevant to itself
43  *
44  * Author: Andreas Dilger <adilger@clusterfs.com>
45  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46  * Author: Mikhail Pershin <mike.pershin@intel.com>
47  */
48
49 #define DEBUG_SUBSYSTEM S_LOG
50
51 #ifndef __KERNEL__
52 #include <liblustre.h>
53 #endif
54
55 #include <obd_class.h>
56
57 #include "llog_internal.h"
58
59 /* Create a new log handle and add it to the open list.
60  * This log handle will be closed when all of the records in it are removed.
61  *
62  * Assumes caller has already pushed us into the kernel context and is locking.
63  */
64 static int llog_cat_new_log(const struct lu_env *env,
65                             struct llog_handle *cathandle,
66                             struct llog_handle *loghandle,
67                             struct thandle *th)
68 {
69
70         struct llog_log_hdr *llh;
71         struct llog_logid_rec rec = { { 0 }, };
72         int rc, index, bitmap_size;
73         ENTRY;
74
75         llh = cathandle->lgh_hdr;
76         bitmap_size = LLOG_BITMAP_SIZE(llh);
77
78         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
79
80         /* maximum number of available slots in catlog is bitmap_size - 2 */
81         if (llh->llh_cat_idx == index) {
82                 CERROR("no free catalog slots for log...\n");
83                 RETURN(-ENOSPC);
84         }
85
86         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
87                 RETURN(-ENOSPC);
88
89         rc = llog_create(env, loghandle, th);
90         /* if llog is already created, no need to initialize it */
91         if (rc == -EEXIST) {
92                 RETURN(0);
93         } else if (rc != 0) {
94                 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
95                        loghandle->lgh_ctxt->loc_obd->obd_name, rc);
96                 RETURN(rc);
97         }
98
99         rc = llog_init_handle(env, loghandle,
100                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
101                               &cathandle->lgh_hdr->llh_tgtuuid);
102         if (rc)
103                 GOTO(out_destroy, rc);
104
105         if (index == 0)
106                 index = 1;
107
108         cfs_spin_lock(&loghandle->lgh_hdr_lock);
109         llh->llh_count++;
110         if (ext2_set_bit(index, llh->llh_bitmap)) {
111                 CERROR("argh, index %u already set in log bitmap?\n",
112                        index);
113                 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
114                 LBUG(); /* should never happen */
115         }
116         cfs_spin_unlock(&loghandle->lgh_hdr_lock);
117
118         cathandle->lgh_last_idx = index;
119         llh->llh_tail.lrt_index = index;
120
121         CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
122                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
123                index, cathandle->lgh_id.lgl_oid);
124         /* build the record for this log in the catalog */
125         rec.lid_hdr.lrh_len = sizeof(rec);
126         rec.lid_hdr.lrh_index = index;
127         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
128         rec.lid_id = loghandle->lgh_id;
129         rec.lid_tail.lrt_len = sizeof(rec);
130         rec.lid_tail.lrt_index = index;
131
132         /* update the catalog: header and record */
133         rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
134                             &loghandle->u.phd.phd_cookie, 1, NULL, index, th);
135         if (rc < 0)
136                 GOTO(out_destroy, rc);
137
138         loghandle->lgh_hdr->llh_cat_idx = index;
139         RETURN(0);
140 out_destroy:
141         llog_destroy(env, loghandle);
142         RETURN(rc);
143 }
144
145 /* Open an existent log handle and add it to the open list.
146  * This log handle will be closed when all of the records in it are removed.
147  *
148  * Assumes caller has already pushed us into the kernel context and is locking.
149  * We return a lock on the handle to ensure nobody yanks it from us.
150  */
151 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
152                        struct llog_handle **res, struct llog_logid *logid)
153 {
154         struct llog_handle      *loghandle;
155         int                      rc = 0;
156
157         ENTRY;
158
159         if (cathandle == NULL)
160                 RETURN(-EBADF);
161
162         cfs_down_write(&cathandle->lgh_lock);
163         cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
164                                 u.phd.phd_entry) {
165                 struct llog_logid *cgl = &loghandle->lgh_id;
166
167                 if (cgl->lgl_oid == logid->lgl_oid) {
168                         if (cgl->lgl_ogen != logid->lgl_ogen) {
169                                 CERROR("%s: log "LPX64" generation %x != %x\n",
170                                        loghandle->lgh_ctxt->loc_obd->obd_name,
171                                        logid->lgl_oid, cgl->lgl_ogen,
172                                        logid->lgl_ogen);
173                                 continue;
174                         }
175                         loghandle->u.phd.phd_cat_handle = cathandle;
176                         cfs_up_write(&cathandle->lgh_lock);
177                         GOTO(out, rc = 0);
178                 }
179         }
180         cfs_up_write(&cathandle->lgh_lock);
181
182         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
183                        LLOG_OPEN_EXISTS);
184         if (rc < 0) {
185                 CERROR("%s: error opening log id "LPX64":%x: rc = %d\n",
186                        cathandle->lgh_ctxt->loc_obd->obd_name,
187                        logid->lgl_oid, logid->lgl_ogen, rc);
188                 GOTO(out, rc);
189         }
190
191         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
192         if (rc < 0) {
193                 llog_close(env, loghandle);
194                 GOTO(out, rc);
195         }
196
197         cfs_down_write(&cathandle->lgh_lock);
198         cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
199         cfs_up_write(&cathandle->lgh_lock);
200
201         loghandle->u.phd.phd_cat_handle = cathandle;
202         loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
203         loghandle->u.phd.phd_cookie.lgc_index =
204                                 loghandle->lgh_hdr->llh_cat_idx;
205         EXIT;
206 out:
207         *res = loghandle;
208         return rc;
209 }
210
211 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
212 {
213         struct llog_handle      *loghandle, *n;
214         int                      rc;
215
216         ENTRY;
217
218         cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
219                                      u.phd.phd_entry) {
220                 struct llog_log_hdr     *llh = loghandle->lgh_hdr;
221                 int                      index;
222
223                 /* unlink open-not-created llogs */
224                 cfs_list_del_init(&loghandle->u.phd.phd_entry);
225                 llh = loghandle->lgh_hdr;
226                 if (loghandle->lgh_obj != NULL && llh != NULL &&
227                     (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
228                     (llh->llh_count == 1)) {
229                         rc = llog_destroy(env, loghandle);
230                         if (rc)
231                                 CERROR("%s: failure destroying log during "
232                                        "cleanup: rc = %d\n",
233                                        loghandle->lgh_ctxt->loc_obd->obd_name,
234                                        rc);
235
236                         index = loghandle->u.phd.phd_cookie.lgc_index;
237
238                         LASSERT(index);
239                         llog_cat_set_first_idx(cathandle, index);
240                         rc = llog_cancel_rec(env, cathandle, index);
241                         if (rc == 0)
242                                 CDEBUG(D_RPCTRACE,
243                                        "cancel plain log at index %u of "
244                                        "catalog "LPX64"\n",
245                                        index, cathandle->lgh_id.lgl_oid);
246                 }
247                 llog_close(env, loghandle);
248         }
249         /* if handle was stored in ctxt, remove it too */
250         if (cathandle->lgh_ctxt->loc_handle == cathandle)
251                 cathandle->lgh_ctxt->loc_handle = NULL;
252         rc = llog_close(env, cathandle);
253         RETURN(rc);
254 }
255 EXPORT_SYMBOL(llog_cat_close);
256
257 /**
258  * lockdep markers for nested struct llog_handle::lgh_lock locking.
259  */
260 enum {
261         LLOGH_CAT,
262         LLOGH_LOG
263 };
264
265 /** Return the currently active log handle.  If the current log handle doesn't
266  * have enough space left for the current record, start a new one.
267  *
268  * If reclen is 0, we only want to know what the currently active log is,
269  * otherwise we get a lock on this log so nobody can steal our space.
270  *
271  * Assumes caller has already pushed us into the kernel context and is locking.
272  *
273  * NOTE: loghandle is write-locked upon successful return
274  */
275 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
276                                                 struct thandle *th)
277 {
278         struct llog_handle *loghandle = NULL;
279         ENTRY;
280
281         cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
282         loghandle = cathandle->u.chd.chd_current_log;
283         if (loghandle) {
284                 struct llog_log_hdr *llh;
285
286                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
287                 llh = loghandle->lgh_hdr;
288                 if (llh == NULL ||
289                     loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
290                         cfs_up_read(&cathandle->lgh_lock);
291                         RETURN(loghandle);
292                 } else {
293                         cfs_up_write(&loghandle->lgh_lock);
294                 }
295         }
296         cfs_up_read(&cathandle->lgh_lock);
297
298         /* time to use next log */
299
300         /* first, we have to make sure the state hasn't changed */
301         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
302         loghandle = cathandle->u.chd.chd_current_log;
303         if (loghandle) {
304                 struct llog_log_hdr *llh;
305
306                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
307                 llh = loghandle->lgh_hdr;
308                 LASSERT(llh);
309                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
310                         cfs_up_write(&cathandle->lgh_lock);
311                         RETURN(loghandle);
312                 } else {
313                         cfs_up_write(&loghandle->lgh_lock);
314                 }
315         }
316
317         CDEBUG(D_INODE, "use next log\n");
318
319         loghandle = cathandle->u.chd.chd_next_log;
320         cathandle->u.chd.chd_current_log = loghandle;
321         cathandle->u.chd.chd_next_log = NULL;
322         cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
323         cfs_up_write(&cathandle->lgh_lock);
324         LASSERT(loghandle);
325         RETURN(loghandle);
326 }
327
328 /* Add a single record to the recovery log(s) using a catalog
329  * Returns as llog_write_record
330  *
331  * Assumes caller has already pushed us into the kernel context.
332  */
333 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
334                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
335                      void *buf, struct thandle *th)
336 {
337         struct llog_handle *loghandle;
338         int rc;
339         ENTRY;
340
341         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
342         loghandle = llog_cat_current_log(cathandle, th);
343         LASSERT(!IS_ERR(loghandle));
344
345         /* loghandle is already locked by llog_cat_current_log() for us */
346         if (!llog_exist(loghandle)) {
347                 rc = llog_cat_new_log(env, cathandle, loghandle, th);
348                 if (rc < 0) {
349                         cfs_up_write(&loghandle->lgh_lock);
350                         RETURN(rc);
351                 }
352         }
353         /* now let's try to add the record */
354         rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
355         if (rc < 0)
356                 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
357         cfs_up_write(&loghandle->lgh_lock);
358         if (rc == -ENOSPC) {
359                 /* try to use next log */
360                 loghandle = llog_cat_current_log(cathandle, th);
361                 LASSERT(!IS_ERR(loghandle));
362                 /* new llog can be created concurrently */
363                 if (!llog_exist(loghandle)) {
364                         rc = llog_cat_new_log(env, cathandle, loghandle, th);
365                         if (rc < 0) {
366                                 cfs_up_write(&loghandle->lgh_lock);
367                                 RETURN(rc);
368                         }
369                 }
370                 /* now let's try to add the record */
371                 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
372                                     -1, th);
373                 if (rc < 0)
374                         CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
375                 cfs_up_write(&loghandle->lgh_lock);
376         }
377
378         RETURN(rc);
379 }
380 EXPORT_SYMBOL(llog_cat_add_rec);
381
382 int llog_cat_declare_add_rec(const struct lu_env *env,
383                              struct llog_handle *cathandle,
384                              struct llog_rec_hdr *rec, struct thandle *th)
385 {
386         struct llog_handle      *loghandle, *next;
387         int                      rc = 0;
388
389         ENTRY;
390
391         if (cathandle->u.chd.chd_current_log == NULL) {
392                 /* declare new plain llog */
393                 cfs_down_write(&cathandle->lgh_lock);
394                 if (cathandle->u.chd.chd_current_log == NULL) {
395                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
396                                        NULL, NULL, LLOG_OPEN_NEW);
397                         if (rc == 0) {
398                                 cathandle->u.chd.chd_current_log = loghandle;
399                                 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
400                                                   &cathandle->u.chd.chd_head);
401                         }
402                 }
403                 cfs_up_write(&cathandle->lgh_lock);
404         } else if (cathandle->u.chd.chd_next_log == NULL) {
405                 /* declare next plain llog */
406                 cfs_down_write(&cathandle->lgh_lock);
407                 if (cathandle->u.chd.chd_next_log == NULL) {
408                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
409                                        NULL, NULL, LLOG_OPEN_NEW);
410                         if (rc == 0) {
411                                 cathandle->u.chd.chd_next_log = loghandle;
412                                 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
413                                                   &cathandle->u.chd.chd_head);
414                         }
415                 }
416                 cfs_up_write(&cathandle->lgh_lock);
417         }
418         if (rc)
419                 GOTO(out, rc);
420
421         if (!llog_exist(cathandle->u.chd.chd_current_log)) {
422                 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
423                                          th);
424                 if (rc)
425                         GOTO(out, rc);
426                 llog_declare_write_rec(env, cathandle, NULL, -1, th);
427         }
428         /* declare records in the llogs */
429         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
430                                     rec, -1, th);
431         if (rc)
432                 GOTO(out, rc);
433
434         next = cathandle->u.chd.chd_next_log;
435         if (next) {
436                 if (!llog_exist(next)) {
437                         rc = llog_declare_create(env, next, th);
438                         llog_declare_write_rec(env, cathandle, NULL, -1, th);
439                 }
440                 llog_declare_write_rec(env, next, rec, -1, th);
441         }
442 out:
443         RETURN(rc);
444 }
445 EXPORT_SYMBOL(llog_cat_declare_add_rec);
446
447 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
448                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
449                  void *buf)
450 {
451         struct llog_ctxt        *ctxt;
452         struct dt_device        *dt;
453         struct thandle          *th = NULL;
454         int                      rc;
455
456         ctxt = cathandle->lgh_ctxt;
457         LASSERT(ctxt);
458         LASSERT(ctxt->loc_exp);
459
460         if (cathandle->lgh_obj != NULL) {
461                 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
462                 LASSERT(dt);
463
464                 th = dt_trans_create(env, dt);
465                 if (IS_ERR(th))
466                         RETURN(PTR_ERR(th));
467
468                 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
469                 if (rc)
470                         GOTO(out_trans, rc);
471
472                 rc = dt_trans_start_local(env, dt, th);
473                 if (rc)
474                         GOTO(out_trans, rc);
475                 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
476 out_trans:
477                 dt_trans_stop(env, dt, th);
478         } else { /* lvfs compat code */
479                 LASSERT(cathandle->lgh_file != NULL);
480                 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
481                 if (rc == 0)
482                         rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
483                                               buf, th);
484         }
485         RETURN(rc);
486 }
487 EXPORT_SYMBOL(llog_cat_add);
488
489 /* For each cookie in the cookie array, we clear the log in-use bit and either:
490  * - the log is empty, so mark it free in the catalog header and delete it
491  * - the log is not empty, just write out the log header
492  *
493  * The cookies may be in different log files, so we need to get new logs
494  * each time.
495  *
496  * Assumes caller has already pushed us into the kernel context.
497  */
498 int llog_cat_cancel_records(const struct lu_env *env,
499                             struct llog_handle *cathandle, int count,
500                             struct llog_cookie *cookies)
501 {
502         int i, index, rc = 0, failed = 0;
503
504         ENTRY;
505
506         for (i = 0; i < count; i++, cookies++) {
507                 struct llog_handle      *loghandle;
508                 struct llog_logid       *lgl = &cookies->lgc_lgl;
509                 int                      lrc;
510
511                 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
512                 if (rc) {
513                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
514                         break;
515                 }
516
517                 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
518                 if (lrc == 1) {          /* log has been destroyed */
519                         index = loghandle->u.phd.phd_cookie.lgc_index;
520                         cfs_down_write(&cathandle->lgh_lock);
521                         if (cathandle->u.chd.chd_current_log == loghandle)
522                                 cathandle->u.chd.chd_current_log = NULL;
523                         cfs_up_write(&cathandle->lgh_lock);
524                         llog_close(env, loghandle);
525
526                         LASSERT(index);
527                         llog_cat_set_first_idx(cathandle, index);
528                         lrc = llog_cancel_rec(env, cathandle, index);
529                         if (lrc == 0)
530                                 CDEBUG(D_RPCTRACE, "cancel plain log at index"
531                                        " %u of catalog "LPX64"\n",
532                                        index, cathandle->lgh_id.lgl_oid);
533                 } else if (lrc == -ENOENT) {
534                         if (rc == 0) /* ENOENT shouldn't rewrite any error */
535                                 rc = lrc;
536                 } else if (lrc < 0) {
537                         failed++;
538                         rc = lrc;
539                 }
540         }
541         if (rc)
542                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
543                        cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
544                        rc);
545
546         RETURN(rc);
547 }
548 EXPORT_SYMBOL(llog_cat_cancel_records);
549
550 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
551                         struct llog_rec_hdr *rec, void *data)
552 {
553         struct llog_process_data *d = data;
554         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
555         struct llog_handle *llh;
556         int rc;
557
558         ENTRY;
559         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
560                 CERROR("invalid record in catalog\n");
561                 RETURN(-EINVAL);
562         }
563         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
564                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
565                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
566
567         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
568         if (rc) {
569                 CERROR("Cannot find handle for log "LPX64"\n",
570                        lir->lid_id.lgl_oid);
571                 RETURN(rc);
572         }
573
574         if (rec->lrh_index < d->lpd_startcat)
575                 /* Skip processing of the logs until startcat */
576                 RETURN(0);
577
578         if (d->lpd_startidx > 0) {
579                 struct llog_process_cat_data cd;
580
581                 cd.lpcd_first_idx = d->lpd_startidx;
582                 cd.lpcd_last_idx = 0;
583                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
584                                           &cd, false);
585                 /* Continue processing the next log from idx 0 */
586                 d->lpd_startidx = 0;
587         } else {
588                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
589                                           NULL, false);
590         }
591
592         RETURN(rc);
593 }
594
595 int llog_cat_process_or_fork(const struct lu_env *env,
596                              struct llog_handle *cat_llh,
597                              llog_cb_t cb, void *data, int startcat,
598                              int startidx, bool fork)
599 {
600         struct llog_process_data d;
601         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
602         int rc;
603         ENTRY;
604
605         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
606         d.lpd_data = data;
607         d.lpd_cb = cb;
608         d.lpd_startcat = startcat;
609         d.lpd_startidx = startidx;
610
611         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
612                 struct llog_process_cat_data cd;
613
614                 CWARN("catlog "LPX64" crosses index zero\n",
615                       cat_llh->lgh_id.lgl_oid);
616
617                 cd.lpcd_first_idx = llh->llh_cat_idx;
618                 cd.lpcd_last_idx = 0;
619                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
620                                           &d, &cd, fork);
621                 if (rc != 0)
622                         RETURN(rc);
623
624                 cd.lpcd_first_idx = 0;
625                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
626                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
627                                           &d, &cd, fork);
628         } else {
629                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
630                                           &d, NULL, fork);
631         }
632
633         RETURN(rc);
634 }
635 EXPORT_SYMBOL(llog_cat_process_or_fork);
636
637 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
638                      llog_cb_t cb, void *data, int startcat, int startidx)
639 {
640         return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
641                                         startidx, 0);
642 }
643 EXPORT_SYMBOL(llog_cat_process);
644
645 #ifdef __KERNEL__
646 int llog_cat_process_thread(void *data)
647 {
648         struct llog_process_cat_args *args = data;
649         struct llog_ctxt *ctxt = args->lpca_ctxt;
650         struct llog_handle *llh = NULL;
651         llog_cb_t cb = args->lpca_cb;
652         struct llog_thread_info *lgi;
653         struct lu_env            env;
654         int rc;
655         ENTRY;
656
657         cfs_daemonize_ctxt("ll_log_process");
658
659         rc = lu_env_init(&env, LCT_LOCAL);
660         if (rc)
661                 GOTO(out, rc);
662         lgi = llog_info(&env);
663         LASSERT(lgi);
664
665         lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
666         rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
667                        LLOG_OPEN_EXISTS);
668         if (rc) {
669                 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
670                        ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
671                        lgi->lgi_logid.lgl_ogen, rc);
672                 GOTO(out_env, rc);
673         }
674         rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
675         if (rc) {
676                 CERROR("%s: llog_init_handle failed: rc = %d\n",
677                        llh->lgh_ctxt->loc_obd->obd_name, rc);
678                 GOTO(release_llh, rc);
679         }
680
681         if (cb) {
682                 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
683                 if (rc != LLOG_PROC_BREAK && rc != 0)
684                         CERROR("%s: llog_cat_process() failed: rc = %d\n",
685                                llh->lgh_ctxt->loc_obd->obd_name, rc);
686                 cb(&env, llh, NULL, NULL);
687         } else {
688                 CWARN("No callback function for recovery\n");
689         }
690
691         /*
692          * Make sure that all cached data is sent.
693          */
694         llog_sync(ctxt, NULL, 0);
695         GOTO(release_llh, rc);
696 release_llh:
697         rc = llog_cat_close(&env, llh);
698         if (rc)
699                 CERROR("%s: llog_cat_close() failed: rc = %d\n",
700                        llh->lgh_ctxt->loc_obd->obd_name, rc);
701 out_env:
702         lu_env_fini(&env);
703 out:
704         llog_ctxt_put(ctxt);
705         OBD_FREE_PTR(args);
706         return rc;
707 }
708 EXPORT_SYMBOL(llog_cat_process_thread);
709 #endif
710
711 static int llog_cat_reverse_process_cb(const struct lu_env *env,
712                                        struct llog_handle *cat_llh,
713                                        struct llog_rec_hdr *rec, void *data)
714 {
715         struct llog_process_data *d = data;
716         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
717         struct llog_handle *llh;
718         int rc;
719
720         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
721                 CERROR("invalid record in catalog\n");
722                 RETURN(-EINVAL);
723         }
724         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
725                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
726                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
727
728         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
729         if (rc) {
730                 CERROR("Cannot find handle for log "LPX64"\n",
731                        lir->lid_id.lgl_oid);
732                 RETURN(rc);
733         }
734
735         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
736         RETURN(rc);
737 }
738
739 int llog_cat_reverse_process(const struct lu_env *env,
740                              struct llog_handle *cat_llh,
741                              llog_cb_t cb, void *data)
742 {
743         struct llog_process_data d;
744         struct llog_process_cat_data cd;
745         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
746         int rc;
747         ENTRY;
748
749         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
750         d.lpd_data = data;
751         d.lpd_cb = cb;
752
753         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
754                 CWARN("catalog "LPX64" crosses index zero\n",
755                       cat_llh->lgh_id.lgl_oid);
756
757                 cd.lpcd_first_idx = 0;
758                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
759                 rc = llog_reverse_process(env, cat_llh,
760                                           llog_cat_reverse_process_cb,
761                                           &d, &cd);
762                 if (rc != 0)
763                         RETURN(rc);
764
765                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
766                 cd.lpcd_last_idx = 0;
767                 rc = llog_reverse_process(env, cat_llh,
768                                           llog_cat_reverse_process_cb,
769                                           &d, &cd);
770         } else {
771                 rc = llog_reverse_process(env, cat_llh,
772                                           llog_cat_reverse_process_cb,
773                                           &d, NULL);
774         }
775
776         RETURN(rc);
777 }
778 EXPORT_SYMBOL(llog_cat_reverse_process);
779
780 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
781 {
782         struct llog_log_hdr *llh = cathandle->lgh_hdr;
783         int i, bitmap_size, idx;
784         ENTRY;
785
786         bitmap_size = LLOG_BITMAP_SIZE(llh);
787         if (llh->llh_cat_idx == (index - 1)) {
788                 idx = llh->llh_cat_idx + 1;
789                 llh->llh_cat_idx = idx;
790                 if (idx == cathandle->lgh_last_idx)
791                         goto out;
792                 for (i = (index + 1) % bitmap_size;
793                      i != cathandle->lgh_last_idx;
794                      i = (i + 1) % bitmap_size) {
795                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
796                                 idx = llh->llh_cat_idx + 1;
797                                 llh->llh_cat_idx = idx;
798                         } else if (i == 0) {
799                                 llh->llh_cat_idx = 0;
800                         } else {
801                                 break;
802                         }
803                 }
804 out:
805                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
806                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
807         }
808
809         RETURN(0);
810 }
811
812 /* callback func for llog_process in llog_obd_origin_setup */
813 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
814                   struct llog_rec_hdr *rec, void *data)
815 {
816         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
817         struct llog_handle      *loghandle;
818         struct llog_log_hdr     *llh;
819         int                      rc, index;
820
821         ENTRY;
822
823         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
824                 CERROR("%s: invalid record in catalog\n",
825                        loghandle->lgh_ctxt->loc_obd->obd_name);
826                 RETURN(-EINVAL);
827         }
828         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
829                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
830                rec->lrh_index, cathandle->lgh_id.lgl_oid);
831
832         rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
833         if (rc) {
834                 CERROR("%s: cannot find handle for llog "LPX64"\n",
835                        loghandle->lgh_ctxt->loc_obd->obd_name,
836                        lir->lid_id.lgl_oid);
837                 if (rc == -ENOENT) {
838                         index = rec->lrh_index;
839                         goto cat_cleanup;
840                 }
841                 RETURN(rc);
842         }
843
844         llh = loghandle->lgh_hdr;
845         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
846             (llh->llh_count == 1)) {
847                 rc = llog_destroy(env, loghandle);
848                 if (rc)
849                         CERROR("%s: fail to destroy empty log: rc = %d\n",
850                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
851
852                 index = loghandle->u.phd.phd_cookie.lgc_index;
853                 llog_close(env, loghandle);
854
855 cat_cleanup:
856                 LASSERT(index);
857                 llog_cat_set_first_idx(cathandle, index);
858                 rc = llog_cancel_rec(env, cathandle, index);
859                 if (rc == 0)
860                         CDEBUG(D_HA,
861                                "cancel log "LPX64":%x at index %u of catalog "
862                                LPX64"\n", lir->lid_id.lgl_oid,
863                                lir->lid_id.lgl_ogen, rec->lrh_index,
864                                cathandle->lgh_id.lgl_oid);
865         }
866
867         RETURN(rc);
868 }