Whamcloud - gitweb
3bbb80139feeb417f176ccd4be22f60fbed3ad2c
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/llog_cat.c
35  *
36  * OST<->MDS recovery logging infrastructure.
37  *
38  * Invariants in implementation:
39  * - we do not share logs among different OST<->MDS connections, so that
40  *   if an OST or MDS fails it need only look at log(s) relevant to itself
41  *
42  * Author: Andreas Dilger <adilger@clusterfs.com>
43  */
44
45 #define DEBUG_SUBSYSTEM S_LOG
46
47 #ifndef __KERNEL__
48 #include <liblustre.h>
49 #endif
50
51 #include <obd_class.h>
52 #include <lustre_log.h>
53
54 #include "llog_internal.h"
55
56 /* Create a new log handle and add it to the open list.
57  * This log handle will be closed when all of the records in it are removed.
58  *
59  * Assumes caller has already pushed us into the kernel context and is locking.
60  */
61 static struct llog_handle *llog_cat_new_log(const struct lu_env *env,
62                                             struct llog_handle *cathandle)
63 {
64         struct llog_handle *loghandle;
65         struct llog_log_hdr *llh;
66         struct llog_logid_rec rec = { { 0 }, };
67         int rc, index, bitmap_size;
68         ENTRY;
69
70         llh = cathandle->lgh_hdr;
71         bitmap_size = LLOG_BITMAP_SIZE(llh);
72
73         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
74
75         /* maximum number of available slots in catlog is bitmap_size - 2 */
76         if (llh->llh_cat_idx == index) {
77                 CERROR("no free catalog slots for log...\n");
78                 RETURN(ERR_PTR(-ENOSPC));
79         }
80
81         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
82                 RETURN(ERR_PTR(-ENOSPC));
83
84         rc = llog_create(env, cathandle->lgh_ctxt, &loghandle, NULL, NULL);
85         if (rc)
86                 RETURN(ERR_PTR(rc));
87
88         rc = llog_init_handle(env, loghandle,
89                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
90                               &cathandle->lgh_hdr->llh_tgtuuid);
91         if (rc)
92                 GOTO(out_destroy, rc);
93
94         if (index == 0)
95                 index = 1;
96
97         cfs_spin_lock(&loghandle->lgh_hdr_lock);
98         llh->llh_count++;
99         if (ext2_set_bit(index, llh->llh_bitmap)) {
100                 CERROR("argh, index %u already set in log bitmap?\n",
101                        index);
102                 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
103                 LBUG(); /* should never happen */
104         }
105         cfs_spin_unlock(&loghandle->lgh_hdr_lock);
106
107         cathandle->lgh_last_idx = index;
108         llh->llh_tail.lrt_index = index;
109
110         CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
111                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
112                index, cathandle->lgh_id.lgl_oid);
113         /* build the record for this log in the catalog */
114         rec.lid_hdr.lrh_len = sizeof(rec);
115         rec.lid_hdr.lrh_index = index;
116         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
117         rec.lid_id = loghandle->lgh_id;
118         rec.lid_tail.lrt_len = sizeof(rec);
119         rec.lid_tail.lrt_index = index;
120
121         /* update the catalog: header and record */
122         rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
123                             &loghandle->u.phd.phd_cookie, 1, NULL, index);
124         if (rc < 0) {
125                 GOTO(out_destroy, rc);
126         }
127
128         loghandle->lgh_hdr->llh_cat_idx = index;
129         cathandle->u.chd.chd_current_log = loghandle;
130         LASSERT(cfs_list_empty(&loghandle->u.phd.phd_entry));
131         cfs_list_add_tail(&loghandle->u.phd.phd_entry,
132                           &cathandle->u.chd.chd_head);
133
134 out_destroy:
135         if (rc < 0)
136                 llog_destroy(env, loghandle);
137
138         RETURN(loghandle);
139 }
140
141 /* Open an existent log handle and add it to the open list.
142  * This log handle will be closed when all of the records in it are removed.
143  *
144  * Assumes caller has already pushed us into the kernel context and is locking.
145  * We return a lock on the handle to ensure nobody yanks it from us.
146  */
147 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
148                        struct llog_handle **res, struct llog_logid *logid)
149 {
150         struct llog_handle *loghandle;
151         int rc = 0;
152         ENTRY;
153
154         if (cathandle == NULL)
155                 RETURN(-EBADF);
156
157         cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
158                                 u.phd.phd_entry) {
159                 struct llog_logid *cgl = &loghandle->lgh_id;
160
161                 if (cgl->lgl_oid == logid->lgl_oid) {
162                         if (cgl->lgl_ogen != logid->lgl_ogen) {
163                                 CERROR("log "LPX64" generation %x != %x\n",
164                                        logid->lgl_oid, cgl->lgl_ogen,
165                                        logid->lgl_ogen);
166                                 continue;
167                         }
168                         loghandle->u.phd.phd_cat_handle = cathandle;
169                         GOTO(out, rc = 0);
170                 }
171         }
172
173         rc = llog_create(env, cathandle->lgh_ctxt, &loghandle, logid, NULL);
174         if (rc) {
175                 CERROR("error opening log id "LPX64":%x: rc %d\n",
176                        logid->lgl_oid, logid->lgl_ogen, rc);
177         } else {
178                 rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
179                 if (!rc) {
180                         cfs_list_add(&loghandle->u.phd.phd_entry,
181                                      &cathandle->u.chd.chd_head);
182                 }
183         }
184         if (!rc) {
185                 loghandle->u.phd.phd_cat_handle = cathandle;
186                 loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
187                 loghandle->u.phd.phd_cookie.lgc_index =
188                         loghandle->lgh_hdr->llh_cat_idx;
189         }
190
191 out:
192         *res = loghandle;
193         RETURN(rc);
194 }
195
196 int llog_cat_put(const struct lu_env *env, struct llog_handle *cathandle)
197 {
198         struct llog_handle      *loghandle, *n;
199         int                      rc;
200
201         ENTRY;
202
203         cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
204                                      u.phd.phd_entry) {
205                 int err = llog_close(env, loghandle);
206                 if (err)
207                         CERROR("error closing loghandle\n");
208         }
209         rc = llog_close(env, cathandle);
210         RETURN(rc);
211 }
212 EXPORT_SYMBOL(llog_cat_put);
213
214 /**
215  * lockdep markers for nested struct llog_handle::lgh_lock locking.
216  */
217 enum {
218         LLOGH_CAT,
219         LLOGH_LOG
220 };
221
222 /** Return the currently active log handle.  If the current log handle doesn't
223  * have enough space left for the current record, start a new one.
224  *
225  * If reclen is 0, we only want to know what the currently active log is,
226  * otherwise we get a lock on this log so nobody can steal our space.
227  *
228  * Assumes caller has already pushed us into the kernel context and is locking.
229  *
230  * NOTE: loghandle is write-locked upon successful return
231  */
232 static struct llog_handle *llog_cat_current_log(const struct lu_env *env,
233                                                 struct llog_handle *cathandle,
234                                                 int create)
235 {
236         struct llog_handle *loghandle = NULL;
237         ENTRY;
238
239         cfs_down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
240         loghandle = cathandle->u.chd.chd_current_log;
241         if (loghandle) {
242                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
243
244                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
245                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
246                         cfs_up_read(&cathandle->lgh_lock);
247                         RETURN(loghandle);
248                 } else {
249                         cfs_up_write(&loghandle->lgh_lock);
250                 }
251         }
252         if (!create) {
253                 if (loghandle)
254                         cfs_down_write(&loghandle->lgh_lock);
255                 cfs_up_read(&cathandle->lgh_lock);
256                 RETURN(loghandle);
257         }
258         cfs_up_read(&cathandle->lgh_lock);
259
260         /* time to create new log */
261
262         /* first, we have to make sure the state hasn't changed */
263         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
264         loghandle = cathandle->u.chd.chd_current_log;
265         if (loghandle) {
266                 struct llog_log_hdr *llh = loghandle->lgh_hdr;
267
268                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
269                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
270                         cfs_up_write(&cathandle->lgh_lock);
271                         RETURN(loghandle);
272                 } else {
273                         cfs_up_write(&loghandle->lgh_lock);
274                 }
275         }
276
277         CDEBUG(D_INODE, "creating new log\n");
278         loghandle = llog_cat_new_log(env, cathandle);
279         if (!IS_ERR(loghandle))
280                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
281         cfs_up_write(&cathandle->lgh_lock);
282         RETURN(loghandle);
283 }
284
285 /* Add a single record to the recovery log(s) using a catalog
286  * Returns as llog_write_record
287  *
288  * Assumes caller has already pushed us into the kernel context.
289  */
290 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
291                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
292                      void *buf)
293 {
294         struct llog_handle *loghandle;
295         int rc;
296         ENTRY;
297
298         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
299         loghandle = llog_cat_current_log(env, cathandle, 1);
300         if (IS_ERR(loghandle))
301                 RETURN(PTR_ERR(loghandle));
302         /* loghandle is already locked by llog_cat_current_log() for us */
303         rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1);
304         if (rc < 0)
305                 CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
306         cfs_up_write(&loghandle->lgh_lock);
307         if (rc == -ENOSPC) {
308                 /* to create a new plain log */
309                 loghandle = llog_cat_current_log(env, cathandle, 1);
310                 if (IS_ERR(loghandle))
311                         RETURN(PTR_ERR(loghandle));
312                 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
313                                     -1);
314                 cfs_up_write(&loghandle->lgh_lock);
315         }
316
317         RETURN(rc);
318 }
319 EXPORT_SYMBOL(llog_cat_add_rec);
320
321 /* For each cookie in the cookie array, we clear the log in-use bit and either:
322  * - the log is empty, so mark it free in the catalog header and delete it
323  * - the log is not empty, just write out the log header
324  *
325  * The cookies may be in different log files, so we need to get new logs
326  * each time.
327  *
328  * Assumes caller has already pushed us into the kernel context.
329  */
330 int llog_cat_cancel_records(const struct lu_env *env,
331                             struct llog_handle *cathandle, int count,
332                             struct llog_cookie *cookies)
333 {
334         int i, index, rc = 0;
335         ENTRY;
336
337         cfs_down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
338         for (i = 0; i < count; i++, cookies++) {
339                 struct llog_handle *loghandle;
340                 struct llog_logid *lgl = &cookies->lgc_lgl;
341
342                 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
343                 if (rc) {
344                         CERROR("Cannot find log "LPX64"\n", lgl->lgl_oid);
345                         break;
346                 }
347
348                 cfs_down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
349                 rc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
350                 cfs_up_write(&loghandle->lgh_lock);
351
352                 if (rc == 1) {          /* log has been destroyed */
353                         index = loghandle->u.phd.phd_cookie.lgc_index;
354                         if (cathandle->u.chd.chd_current_log == loghandle)
355                                 cathandle->u.chd.chd_current_log = NULL;
356                         llog_free_handle(loghandle);
357
358                         LASSERT(index);
359                         llog_cat_set_first_idx(cathandle, index);
360                         rc = llog_cancel_rec(env, cathandle, index);
361                         if (rc == 0)
362                                 CDEBUG(D_RPCTRACE,"cancel plain log at index %u"
363                                        " of catalog "LPX64"\n",
364                                        index, cathandle->lgh_id.lgl_oid);
365                 }
366         }
367         cfs_up_write(&cathandle->lgh_lock);
368
369         RETURN(rc);
370 }
371 EXPORT_SYMBOL(llog_cat_cancel_records);
372
373 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
374                         struct llog_rec_hdr *rec, void *data)
375 {
376         struct llog_process_data *d = data;
377         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
378         struct llog_handle *llh;
379         int rc;
380
381         ENTRY;
382         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
383                 CERROR("invalid record in catalog\n");
384                 RETURN(-EINVAL);
385         }
386         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
387                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
388                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
389
390         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
391         if (rc) {
392                 CERROR("Cannot find handle for log "LPX64"\n",
393                        lir->lid_id.lgl_oid);
394                 RETURN(rc);
395         }
396
397         if (rec->lrh_index < d->lpd_startcat)
398                 /* Skip processing of the logs until startcat */
399                 RETURN(0);
400
401         if (d->lpd_startidx > 0) {
402                 struct llog_process_cat_data cd;
403
404                 cd.lpcd_first_idx = d->lpd_startidx;
405                 cd.lpcd_last_idx = 0;
406                 rc = llog_process(env, llh, d->lpd_cb, d->lpd_data, &cd);
407                 /* Continue processing the next log from idx 0 */
408                 d->lpd_startidx = 0;
409         } else {
410                 rc = llog_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
411         }
412
413         RETURN(rc);
414 }
415
416 int llog_cat_process_or_fork(const struct lu_env *env,
417                              struct llog_handle *cat_llh,
418                              llog_cb_t cb, void *data, int startcat,
419                              int startidx, bool fork)
420 {
421         struct llog_process_data d;
422         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
423         int rc;
424         ENTRY;
425
426         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
427         d.lpd_data = data;
428         d.lpd_cb = cb;
429         d.lpd_startcat = startcat;
430         d.lpd_startidx = startidx;
431
432         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
433                 struct llog_process_cat_data cd;
434
435                 CWARN("catlog "LPX64" crosses index zero\n",
436                       cat_llh->lgh_id.lgl_oid);
437
438                 cd.lpcd_first_idx = llh->llh_cat_idx;
439                 cd.lpcd_last_idx = 0;
440                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
441                                           &d, &cd, fork);
442                 if (rc != 0)
443                         RETURN(rc);
444
445                 cd.lpcd_first_idx = 0;
446                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
447                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
448                                           &d, &cd, fork);
449         } else {
450                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
451                                           &d, NULL, fork);
452         }
453
454         RETURN(rc);
455 }
456 EXPORT_SYMBOL(llog_cat_process_or_fork);
457
458 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
459                      llog_cb_t cb, void *data, int startcat, int startidx)
460 {
461         return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
462                                         startidx, 0);
463 }
464 EXPORT_SYMBOL(llog_cat_process);
465
466 #ifdef __KERNEL__
467 int llog_cat_process_thread(void *data)
468 {
469         struct llog_process_cat_args *args = data;
470         struct llog_ctxt *ctxt = args->lpca_ctxt;
471         struct llog_handle *llh = NULL;
472         llog_cb_t cb = args->lpca_cb;
473         struct llog_thread_info *lgi;
474         struct lu_env            env;
475         int rc;
476         ENTRY;
477
478         cfs_daemonize_ctxt("ll_log_process");
479
480         rc = lu_env_init(&env, LCT_LOCAL);
481         if (rc)
482                 GOTO(out, rc);
483         lgi = llog_info(&env);
484         LASSERT(lgi);
485
486         lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
487         rc = llog_create(&env, ctxt, &llh, &lgi->lgi_logid, NULL);
488         if (rc) {
489                 CERROR("llog_create() failed %d\n", rc);
490                 GOTO(out_env, rc);
491         }
492         rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
493         if (rc) {
494                 CERROR("llog_init_handle failed %d\n", rc);
495                 GOTO(release_llh, rc);
496         }
497
498         if (cb) {
499                 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
500                 if (rc != LLOG_PROC_BREAK && rc != 0)
501                         CERROR("llog_cat_process() failed %d\n", rc);
502                 cb(&env, llh, NULL, NULL);
503         } else {
504                 CWARN("No callback function for recovery\n");
505         }
506
507         /*
508          * Make sure that all cached data is sent.
509          */
510         llog_sync(ctxt, NULL, 0);
511         GOTO(release_llh, rc);
512 release_llh:
513         rc = llog_cat_put(&env, llh);
514         if (rc)
515                 CERROR("llog_cat_put() failed %d\n", rc);
516 out_env:
517         lu_env_fini(&env);
518 out:
519         llog_ctxt_put(ctxt);
520         OBD_FREE_PTR(args);
521         return rc;
522 }
523 EXPORT_SYMBOL(llog_cat_process_thread);
524 #endif
525
526 static int llog_cat_reverse_process_cb(const struct lu_env *env,
527                                        struct llog_handle *cat_llh,
528                                        struct llog_rec_hdr *rec, void *data)
529 {
530         struct llog_process_data *d = data;
531         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
532         struct llog_handle *llh;
533         int rc;
534
535         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
536                 CERROR("invalid record in catalog\n");
537                 RETURN(-EINVAL);
538         }
539         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
540                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
541                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
542
543         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
544         if (rc) {
545                 CERROR("Cannot find handle for log "LPX64"\n",
546                        lir->lid_id.lgl_oid);
547                 RETURN(rc);
548         }
549
550         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
551         RETURN(rc);
552 }
553
554 int llog_cat_reverse_process(const struct lu_env *env,
555                              struct llog_handle *cat_llh,
556                              llog_cb_t cb, void *data)
557 {
558         struct llog_process_data d;
559         struct llog_process_cat_data cd;
560         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
561         int rc;
562         ENTRY;
563
564         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
565         d.lpd_data = data;
566         d.lpd_cb = cb;
567
568         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
569                 CWARN("catalog "LPX64" crosses index zero\n",
570                       cat_llh->lgh_id.lgl_oid);
571
572                 cd.lpcd_first_idx = 0;
573                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
574                 rc = llog_reverse_process(env, cat_llh,
575                                           llog_cat_reverse_process_cb,
576                                           &d, &cd);
577                 if (rc != 0)
578                         RETURN(rc);
579
580                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
581                 cd.lpcd_last_idx = 0;
582                 rc = llog_reverse_process(env, cat_llh,
583                                           llog_cat_reverse_process_cb,
584                                           &d, &cd);
585         } else {
586                 rc = llog_reverse_process(env, cat_llh,
587                                           llog_cat_reverse_process_cb,
588                                           &d, NULL);
589         }
590
591         RETURN(rc);
592 }
593 EXPORT_SYMBOL(llog_cat_reverse_process);
594
595 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
596 {
597         struct llog_log_hdr *llh = cathandle->lgh_hdr;
598         int i, bitmap_size, idx;
599         ENTRY;
600
601         bitmap_size = LLOG_BITMAP_SIZE(llh);
602         if (llh->llh_cat_idx == (index - 1)) {
603                 idx = llh->llh_cat_idx + 1;
604                 llh->llh_cat_idx = idx;
605                 if (idx == cathandle->lgh_last_idx)
606                         goto out;
607                 for (i = (index + 1) % bitmap_size;
608                      i != cathandle->lgh_last_idx;
609                      i = (i + 1) % bitmap_size) {
610                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
611                                 idx = llh->llh_cat_idx + 1;
612                                 llh->llh_cat_idx = idx;
613                         } else if (i == 0) {
614                                 llh->llh_cat_idx = 0;
615                         } else {
616                                 break;
617                         }
618                 }
619 out:
620                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
621                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
622         }
623
624         RETURN(0);
625 }
626
627 /* callback func for llog_process in llog_obd_origin_setup */
628 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
629                   struct llog_rec_hdr *rec, void *data)
630 {
631         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
632         struct llog_handle      *loghandle;
633         struct llog_log_hdr     *llh;
634         int                      rc, index;
635
636         ENTRY;
637
638         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
639                 CERROR("%s: invalid record in catalog\n",
640                        loghandle->lgh_ctxt->loc_obd->obd_name);
641                 RETURN(-EINVAL);
642         }
643         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
644                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
645                rec->lrh_index, cathandle->lgh_id.lgl_oid);
646
647         rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
648         if (rc) {
649                 CERROR("%s: cannot find handle for llog "LPX64"\n",
650                        loghandle->lgh_ctxt->loc_obd->obd_name,
651                        lir->lid_id.lgl_oid);
652                 if (rc == -ENOENT) {
653                         index = rec->lrh_index;
654                         goto cat_cleanup;
655                 }
656                 RETURN(rc);
657         }
658
659         llh = loghandle->lgh_hdr;
660         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
661             (llh->llh_count == 1)) {
662                 rc = llog_destroy(env, loghandle);
663                 if (rc)
664                         CERROR("%s: fail to destroy empty log: rc = %d\n",
665                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
666
667                 index = loghandle->u.phd.phd_cookie.lgc_index;
668                 llog_free_handle(loghandle);
669
670 cat_cleanup:
671                 LASSERT(index);
672                 llog_cat_set_first_idx(cathandle, index);
673                 rc = llog_cancel_rec(env, cathandle, index);
674                 if (rc == 0)
675                         CDEBUG(D_HA,
676                                "cancel log "LPX64":%x at index %u of catalog "
677                                LPX64"\n", lir->lid_id.lgl_oid,
678                                lir->lid_id.lgl_ogen, rec->lrh_index,
679                                cathandle->lgh_id.lgl_oid);
680         }
681
682         RETURN(rc);
683 }