]> Pileus Git - ~andy/linux/blob - drivers/staging/lustre/lustre/obdclass/llog_cat.c
Linux 3.14
[~andy/linux] / drivers / staging / lustre / 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) 2012, Intel Corporation.
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
52 #include <obd_class.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 int llog_cat_new_log(const struct lu_env *env,
62                             struct llog_handle *cathandle,
63                             struct llog_handle *loghandle,
64                             struct thandle *th)
65 {
66
67         struct llog_log_hdr *llh;
68         struct llog_logid_rec rec = { { 0 }, };
69         int rc, index, bitmap_size;
70
71         llh = cathandle->lgh_hdr;
72         bitmap_size = LLOG_BITMAP_SIZE(llh);
73
74         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
75
76         /* maximum number of available slots in catlog is bitmap_size - 2 */
77         if (llh->llh_cat_idx == index) {
78                 CERROR("no free catalog slots for log...\n");
79                 return -ENOSPC;
80         }
81
82         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
83                 return -ENOSPC;
84
85         rc = llog_create(env, loghandle, th);
86         /* if llog is already created, no need to initialize it */
87         if (rc == -EEXIST) {
88                 return 0;
89         } else if (rc != 0) {
90                 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
91                        loghandle->lgh_ctxt->loc_obd->obd_name, rc);
92                 return rc;
93         }
94
95         rc = llog_init_handle(env, loghandle,
96                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
97                               &cathandle->lgh_hdr->llh_tgtuuid);
98         if (rc)
99                 GOTO(out_destroy, rc);
100
101         if (index == 0)
102                 index = 1;
103
104         spin_lock(&loghandle->lgh_hdr_lock);
105         llh->llh_count++;
106         if (ext2_set_bit(index, llh->llh_bitmap)) {
107                 CERROR("argh, index %u already set in log bitmap?\n",
108                        index);
109                 spin_unlock(&loghandle->lgh_hdr_lock);
110                 LBUG(); /* should never happen */
111         }
112         spin_unlock(&loghandle->lgh_hdr_lock);
113
114         cathandle->lgh_last_idx = index;
115         llh->llh_tail.lrt_index = index;
116
117         CDEBUG(D_RPCTRACE,"new recovery log "DOSTID":%x for index %u of catalog"
118                DOSTID"\n", POSTID(&loghandle->lgh_id.lgl_oi),
119                loghandle->lgh_id.lgl_ogen, index,
120                POSTID(&cathandle->lgh_id.lgl_oi));
121         /* build the record for this log in the catalog */
122         rec.lid_hdr.lrh_len = sizeof(rec);
123         rec.lid_hdr.lrh_index = index;
124         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
125         rec.lid_id = loghandle->lgh_id;
126         rec.lid_tail.lrt_len = sizeof(rec);
127         rec.lid_tail.lrt_index = index;
128
129         /* update the catalog: header and record */
130         rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
131                             &loghandle->u.phd.phd_cookie, 1, NULL, index, th);
132         if (rc < 0)
133                 GOTO(out_destroy, rc);
134
135         loghandle->lgh_hdr->llh_cat_idx = index;
136         return 0;
137 out_destroy:
138         llog_destroy(env, loghandle);
139         return rc;
140 }
141
142 /* Open an existent log handle and add it to the open list.
143  * This log handle will be closed when all of the records in it are removed.
144  *
145  * Assumes caller has already pushed us into the kernel context and is locking.
146  * We return a lock on the handle to ensure nobody yanks it from us.
147  *
148  * This takes extra reference on llog_handle via llog_handle_get() and require
149  * this reference to be put by caller using llog_handle_put()
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         if (cathandle == NULL)
158                 return -EBADF;
159
160         down_write(&cathandle->lgh_lock);
161         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
162                                 u.phd.phd_entry) {
163                 struct llog_logid *cgl = &loghandle->lgh_id;
164
165                 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
166                     ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
167                         if (cgl->lgl_ogen != logid->lgl_ogen) {
168                                 CERROR("%s: log "DOSTID" generation %x != %x\n",
169                                        loghandle->lgh_ctxt->loc_obd->obd_name,
170                                        POSTID(&logid->lgl_oi), cgl->lgl_ogen,
171                                        logid->lgl_ogen);
172                                 continue;
173                         }
174                         loghandle->u.phd.phd_cat_handle = cathandle;
175                         up_write(&cathandle->lgh_lock);
176                         GOTO(out, rc = 0);
177                 }
178         }
179         up_write(&cathandle->lgh_lock);
180
181         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
182                        LLOG_OPEN_EXISTS);
183         if (rc < 0) {
184                 CERROR("%s: error opening log id "DOSTID":%x: rc = %d\n",
185                        cathandle->lgh_ctxt->loc_obd->obd_name,
186                        POSTID(&logid->lgl_oi), logid->lgl_ogen, rc);
187                 return rc;
188         }
189
190         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
191         if (rc < 0) {
192                 llog_close(env, loghandle);
193                 loghandle = NULL;
194                 return rc;
195         }
196
197         down_write(&cathandle->lgh_lock);
198         list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
199         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 out:
206         llog_handle_get(loghandle);
207         *res = loghandle;
208         return 0;
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         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
217                                      u.phd.phd_entry) {
218                 struct llog_log_hdr     *llh = loghandle->lgh_hdr;
219                 int                      index;
220
221                 /* unlink open-not-created llogs */
222                 list_del_init(&loghandle->u.phd.phd_entry);
223                 llh = loghandle->lgh_hdr;
224                 if (loghandle->lgh_obj != NULL && llh != NULL &&
225                     (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
226                     (llh->llh_count == 1)) {
227                         rc = llog_destroy(env, loghandle);
228                         if (rc)
229                                 CERROR("%s: failure destroying log during "
230                                        "cleanup: rc = %d\n",
231                                        loghandle->lgh_ctxt->loc_obd->obd_name,
232                                        rc);
233
234                         index = loghandle->u.phd.phd_cookie.lgc_index;
235                         llog_cat_cleanup(env, cathandle, NULL, index);
236                 }
237                 llog_close(env, loghandle);
238         }
239         /* if handle was stored in ctxt, remove it too */
240         if (cathandle->lgh_ctxt->loc_handle == cathandle)
241                 cathandle->lgh_ctxt->loc_handle = NULL;
242         rc = llog_close(env, cathandle);
243         return rc;
244 }
245 EXPORT_SYMBOL(llog_cat_close);
246
247 /**
248  * lockdep markers for nested struct llog_handle::lgh_lock locking.
249  */
250 enum {
251         LLOGH_CAT,
252         LLOGH_LOG
253 };
254
255 /** Return the currently active log handle.  If the current log handle doesn't
256  * have enough space left for the current record, start a new one.
257  *
258  * If reclen is 0, we only want to know what the currently active log is,
259  * otherwise we get a lock on this log so nobody can steal our space.
260  *
261  * Assumes caller has already pushed us into the kernel context and is locking.
262  *
263  * NOTE: loghandle is write-locked upon successful return
264  */
265 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
266                                                 struct thandle *th)
267 {
268         struct llog_handle *loghandle = NULL;
269
270         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
271         loghandle = cathandle->u.chd.chd_current_log;
272         if (loghandle) {
273                 struct llog_log_hdr *llh;
274
275                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
276                 llh = loghandle->lgh_hdr;
277                 if (llh == NULL ||
278                     loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
279                         up_read(&cathandle->lgh_lock);
280                         return loghandle;
281                 } else {
282                         up_write(&loghandle->lgh_lock);
283                 }
284         }
285         up_read(&cathandle->lgh_lock);
286
287         /* time to use next log */
288
289         /* first, we have to make sure the state hasn't changed */
290         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
291         loghandle = cathandle->u.chd.chd_current_log;
292         if (loghandle) {
293                 struct llog_log_hdr *llh;
294
295                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
296                 llh = loghandle->lgh_hdr;
297                 LASSERT(llh);
298                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
299                         up_write(&cathandle->lgh_lock);
300                         return loghandle;
301                 } else {
302                         up_write(&loghandle->lgh_lock);
303                 }
304         }
305
306         CDEBUG(D_INODE, "use next log\n");
307
308         loghandle = cathandle->u.chd.chd_next_log;
309         cathandle->u.chd.chd_current_log = loghandle;
310         cathandle->u.chd.chd_next_log = NULL;
311         down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
312         up_write(&cathandle->lgh_lock);
313         LASSERT(loghandle);
314         return loghandle;
315 }
316
317 /* Add a single record to the recovery log(s) using a catalog
318  * Returns as llog_write_record
319  *
320  * Assumes caller has already pushed us into the kernel context.
321  */
322 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
323                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
324                      void *buf, struct thandle *th)
325 {
326         struct llog_handle *loghandle;
327         int rc;
328
329         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
330         loghandle = llog_cat_current_log(cathandle, th);
331         LASSERT(!IS_ERR(loghandle));
332
333         /* loghandle is already locked by llog_cat_current_log() for us */
334         if (!llog_exist(loghandle)) {
335                 rc = llog_cat_new_log(env, cathandle, loghandle, th);
336                 if (rc < 0) {
337                         up_write(&loghandle->lgh_lock);
338                         return rc;
339                 }
340         }
341         /* now let's try to add the record */
342         rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
343         if (rc < 0)
344                 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
345                              "llog_write_rec %d: lh=%p\n", rc, loghandle);
346         up_write(&loghandle->lgh_lock);
347         if (rc == -ENOSPC) {
348                 /* try to use next log */
349                 loghandle = llog_cat_current_log(cathandle, th);
350                 LASSERT(!IS_ERR(loghandle));
351                 /* new llog can be created concurrently */
352                 if (!llog_exist(loghandle)) {
353                         rc = llog_cat_new_log(env, cathandle, loghandle, th);
354                         if (rc < 0) {
355                                 up_write(&loghandle->lgh_lock);
356                                 return rc;
357                         }
358                 }
359                 /* now let's try to add the record */
360                 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
361                                     -1, th);
362                 if (rc < 0)
363                         CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
364                 up_write(&loghandle->lgh_lock);
365         }
366
367         return rc;
368 }
369 EXPORT_SYMBOL(llog_cat_add_rec);
370
371 int llog_cat_declare_add_rec(const struct lu_env *env,
372                              struct llog_handle *cathandle,
373                              struct llog_rec_hdr *rec, struct thandle *th)
374 {
375         struct llog_handle      *loghandle, *next;
376         int                      rc = 0;
377
378         if (cathandle->u.chd.chd_current_log == NULL) {
379                 /* declare new plain llog */
380                 down_write(&cathandle->lgh_lock);
381                 if (cathandle->u.chd.chd_current_log == NULL) {
382                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
383                                        NULL, NULL, LLOG_OPEN_NEW);
384                         if (rc == 0) {
385                                 cathandle->u.chd.chd_current_log = loghandle;
386                                 list_add_tail(&loghandle->u.phd.phd_entry,
387                                                   &cathandle->u.chd.chd_head);
388                         }
389                 }
390                 up_write(&cathandle->lgh_lock);
391         } else if (cathandle->u.chd.chd_next_log == NULL) {
392                 /* declare next plain llog */
393                 down_write(&cathandle->lgh_lock);
394                 if (cathandle->u.chd.chd_next_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_next_log = loghandle;
399                                 list_add_tail(&loghandle->u.phd.phd_entry,
400                                                   &cathandle->u.chd.chd_head);
401                         }
402                 }
403                 up_write(&cathandle->lgh_lock);
404         }
405         if (rc)
406                 GOTO(out, rc);
407
408         if (!llog_exist(cathandle->u.chd.chd_current_log)) {
409                 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
410                                          th);
411                 if (rc)
412                         GOTO(out, rc);
413                 llog_declare_write_rec(env, cathandle, NULL, -1, th);
414         }
415         /* declare records in the llogs */
416         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
417                                     rec, -1, th);
418         if (rc)
419                 GOTO(out, rc);
420
421         next = cathandle->u.chd.chd_next_log;
422         if (next) {
423                 if (!llog_exist(next)) {
424                         rc = llog_declare_create(env, next, th);
425                         llog_declare_write_rec(env, cathandle, NULL, -1, th);
426                 }
427                 llog_declare_write_rec(env, next, rec, -1, th);
428         }
429 out:
430         return rc;
431 }
432 EXPORT_SYMBOL(llog_cat_declare_add_rec);
433
434 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
435                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
436                  void *buf)
437 {
438         struct llog_ctxt        *ctxt;
439         struct dt_device        *dt;
440         struct thandle          *th = NULL;
441         int                      rc;
442
443         ctxt = cathandle->lgh_ctxt;
444         LASSERT(ctxt);
445         LASSERT(ctxt->loc_exp);
446
447         if (cathandle->lgh_obj != NULL) {
448                 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
449                 LASSERT(dt);
450
451                 th = dt_trans_create(env, dt);
452                 if (IS_ERR(th))
453                         return PTR_ERR(th);
454
455                 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
456                 if (rc)
457                         GOTO(out_trans, rc);
458
459                 rc = dt_trans_start_local(env, dt, th);
460                 if (rc)
461                         GOTO(out_trans, rc);
462                 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
463 out_trans:
464                 dt_trans_stop(env, dt, th);
465         } else { /* lvfs compat code */
466                 LASSERT(cathandle->lgh_file != NULL);
467                 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
468                 if (rc == 0)
469                         rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
470                                               buf, th);
471         }
472         return rc;
473 }
474 EXPORT_SYMBOL(llog_cat_add);
475
476 /* For each cookie in the cookie array, we clear the log in-use bit and either:
477  * - the log is empty, so mark it free in the catalog header and delete it
478  * - the log is not empty, just write out the log header
479  *
480  * The cookies may be in different log files, so we need to get new logs
481  * each time.
482  *
483  * Assumes caller has already pushed us into the kernel context.
484  */
485 int llog_cat_cancel_records(const struct lu_env *env,
486                             struct llog_handle *cathandle, int count,
487                             struct llog_cookie *cookies)
488 {
489         int i, index, rc = 0, failed = 0;
490
491         for (i = 0; i < count; i++, cookies++) {
492                 struct llog_handle      *loghandle;
493                 struct llog_logid       *lgl = &cookies->lgc_lgl;
494                 int                      lrc;
495
496                 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
497                 if (rc) {
498                         CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
499                                cathandle->lgh_ctxt->loc_obd->obd_name,
500                                POSTID(&lgl->lgl_oi), rc);
501                         failed++;
502                         continue;
503                 }
504
505                 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
506                 if (lrc == 1) {   /* log has been destroyed */
507                         index = loghandle->u.phd.phd_cookie.lgc_index;
508                         rc = llog_cat_cleanup(env, cathandle, loghandle,
509                                               index);
510                 } else if (lrc == -ENOENT) {
511                         if (rc == 0) /* ENOENT shouldn't rewrite any error */
512                                 rc = lrc;
513                 } else if (lrc < 0) {
514                         failed++;
515                         rc = lrc;
516                 }
517                 llog_handle_put(loghandle);
518         }
519         if (rc)
520                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
521                        cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
522                        rc);
523
524         return rc;
525 }
526 EXPORT_SYMBOL(llog_cat_cancel_records);
527
528 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
529                         struct llog_rec_hdr *rec, void *data)
530 {
531         struct llog_process_data *d = data;
532         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
533         struct llog_handle *llh;
534         int rc;
535
536         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
537                 CERROR("invalid record in catalog\n");
538                 return -EINVAL;
539         }
540         CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
541                DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
542                rec->lrh_index, POSTID(&cat_llh->lgh_id.lgl_oi));
543
544         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
545         if (rc) {
546                 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
547                        cat_llh->lgh_ctxt->loc_obd->obd_name,
548                        POSTID(&lir->lid_id.lgl_oi), rc);
549                 return rc;
550         }
551
552         if (rec->lrh_index < d->lpd_startcat)
553                 /* Skip processing of the logs until startcat */
554                 return 0;
555
556         if (d->lpd_startidx > 0) {
557                 struct llog_process_cat_data cd;
558
559                 cd.lpcd_first_idx = d->lpd_startidx;
560                 cd.lpcd_last_idx = 0;
561                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
562                                           &cd, false);
563                 /* Continue processing the next log from idx 0 */
564                 d->lpd_startidx = 0;
565         } else {
566                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
567                                           NULL, false);
568         }
569         llog_handle_put(llh);
570
571         return rc;
572 }
573
574 int llog_cat_process_or_fork(const struct lu_env *env,
575                              struct llog_handle *cat_llh,
576                              llog_cb_t cb, void *data, int startcat,
577                              int startidx, bool fork)
578 {
579         struct llog_process_data d;
580         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
581         int rc;
582
583         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
584         d.lpd_data = data;
585         d.lpd_cb = cb;
586         d.lpd_startcat = startcat;
587         d.lpd_startidx = startidx;
588
589         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
590                 struct llog_process_cat_data cd;
591
592                 CWARN("catlog "DOSTID" crosses index zero\n",
593                       POSTID(&cat_llh->lgh_id.lgl_oi));
594
595                 cd.lpcd_first_idx = llh->llh_cat_idx;
596                 cd.lpcd_last_idx = 0;
597                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
598                                           &d, &cd, fork);
599                 if (rc != 0)
600                         return rc;
601
602                 cd.lpcd_first_idx = 0;
603                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
604                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
605                                           &d, &cd, fork);
606         } else {
607                 rc = llog_process_or_fork(env, cat_llh, llog_cat_process_cb,
608                                           &d, NULL, fork);
609         }
610
611         return rc;
612 }
613 EXPORT_SYMBOL(llog_cat_process_or_fork);
614
615 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
616                      llog_cb_t cb, void *data, int startcat, int startidx)
617 {
618         return llog_cat_process_or_fork(env, cat_llh, cb, data, startcat,
619                                         startidx, false);
620 }
621 EXPORT_SYMBOL(llog_cat_process);
622
623 static int llog_cat_reverse_process_cb(const struct lu_env *env,
624                                        struct llog_handle *cat_llh,
625                                        struct llog_rec_hdr *rec, void *data)
626 {
627         struct llog_process_data *d = data;
628         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
629         struct llog_handle *llh;
630         int rc;
631
632         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
633                 CERROR("invalid record in catalog\n");
634                 return -EINVAL;
635         }
636         CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
637                DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
638                le32_to_cpu(rec->lrh_index), POSTID(&cat_llh->lgh_id.lgl_oi));
639
640         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
641         if (rc) {
642                 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
643                        cat_llh->lgh_ctxt->loc_obd->obd_name,
644                        POSTID(&lir->lid_id.lgl_oi), rc);
645                 return rc;
646         }
647
648         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
649         llog_handle_put(llh);
650         return rc;
651 }
652
653 int llog_cat_reverse_process(const struct lu_env *env,
654                              struct llog_handle *cat_llh,
655                              llog_cb_t cb, void *data)
656 {
657         struct llog_process_data d;
658         struct llog_process_cat_data cd;
659         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
660         int rc;
661
662         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
663         d.lpd_data = data;
664         d.lpd_cb = cb;
665
666         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
667                 CWARN("catalog "DOSTID" crosses index zero\n",
668                       POSTID(&cat_llh->lgh_id.lgl_oi));
669
670                 cd.lpcd_first_idx = 0;
671                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
672                 rc = llog_reverse_process(env, cat_llh,
673                                           llog_cat_reverse_process_cb,
674                                           &d, &cd);
675                 if (rc != 0)
676                         return rc;
677
678                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
679                 cd.lpcd_last_idx = 0;
680                 rc = llog_reverse_process(env, cat_llh,
681                                           llog_cat_reverse_process_cb,
682                                           &d, &cd);
683         } else {
684                 rc = llog_reverse_process(env, cat_llh,
685                                           llog_cat_reverse_process_cb,
686                                           &d, NULL);
687         }
688
689         return rc;
690 }
691 EXPORT_SYMBOL(llog_cat_reverse_process);
692
693 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
694 {
695         struct llog_log_hdr *llh = cathandle->lgh_hdr;
696         int i, bitmap_size, idx;
697
698         bitmap_size = LLOG_BITMAP_SIZE(llh);
699         if (llh->llh_cat_idx == (index - 1)) {
700                 idx = llh->llh_cat_idx + 1;
701                 llh->llh_cat_idx = idx;
702                 if (idx == cathandle->lgh_last_idx)
703                         goto out;
704                 for (i = (index + 1) % bitmap_size;
705                      i != cathandle->lgh_last_idx;
706                      i = (i + 1) % bitmap_size) {
707                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
708                                 idx = llh->llh_cat_idx + 1;
709                                 llh->llh_cat_idx = idx;
710                         } else if (i == 0) {
711                                 llh->llh_cat_idx = 0;
712                         } else {
713                                 break;
714                         }
715                 }
716 out:
717                 CDEBUG(D_RPCTRACE, "set catlog "DOSTID" first idx %u\n",
718                        POSTID(&cathandle->lgh_id.lgl_oi), llh->llh_cat_idx);
719         }
720
721         return 0;
722 }
723
724 /* Cleanup deleted plain llog traces from catalog */
725 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
726                      struct llog_handle *loghandle, int index)
727 {
728         int rc;
729
730         LASSERT(index);
731         if (loghandle != NULL) {
732                 /* remove destroyed llog from catalog list and
733                  * chd_current_log variable */
734                 down_write(&cathandle->lgh_lock);
735                 if (cathandle->u.chd.chd_current_log == loghandle)
736                         cathandle->u.chd.chd_current_log = NULL;
737                 list_del_init(&loghandle->u.phd.phd_entry);
738                 up_write(&cathandle->lgh_lock);
739                 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
740                 /* llog was opened and keep in a list, close it now */
741                 llog_close(env, loghandle);
742         }
743         /* remove plain llog entry from catalog by index */
744         llog_cat_set_first_idx(cathandle, index);
745         rc = llog_cancel_rec(env, cathandle, index);
746         if (rc == 0)
747                 CDEBUG(D_HA, "cancel plain log at index"
748                        " %u of catalog "DOSTID"\n",
749                        index, POSTID(&cathandle->lgh_id.lgl_oi));
750         return rc;
751 }
752
753 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
754                   struct llog_rec_hdr *rec, void *data)
755 {
756         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
757         struct llog_handle      *loghandle;
758         struct llog_log_hdr     *llh;
759         int                      rc;
760
761         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
762                 CERROR("invalid record in catalog\n");
763                 return -EINVAL;
764         }
765
766         CDEBUG(D_HA, "processing log "DOSTID":%x at index %u of catalog "
767                DOSTID"\n", POSTID(&lir->lid_id.lgl_oi), lir->lid_id.lgl_ogen,
768                rec->lrh_index, POSTID(&cathandle->lgh_id.lgl_oi));
769
770         rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
771         if (rc) {
772                 CERROR("%s: cannot find handle for llog "DOSTID": %d\n",
773                        cathandle->lgh_ctxt->loc_obd->obd_name,
774                        POSTID(&lir->lid_id.lgl_oi), rc);
775                 if (rc == -ENOENT || rc == -ESTALE) {
776                         /* remove index from catalog */
777                         llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
778                 }
779                 return rc;
780         }
781
782         llh = loghandle->lgh_hdr;
783         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
784             (llh->llh_count == 1)) {
785                 rc = llog_destroy(env, loghandle);
786                 if (rc)
787                         CERROR("%s: fail to destroy empty log: rc = %d\n",
788                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
789
790                 llog_cat_cleanup(env, cathandle, loghandle,
791                                  loghandle->u.phd.phd_cookie.lgc_index);
792         }
793         llog_handle_put(loghandle);
794
795         return rc;
796 }
797 EXPORT_SYMBOL(cat_cancel_cb);
798
799 /* helper to initialize catalog llog and process it to cancel */
800 int llog_cat_init_and_process(const struct lu_env *env,
801                               struct llog_handle *llh)
802 {
803         int rc;
804
805         rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
806         if (rc)
807                 return rc;
808
809         rc = llog_process_or_fork(env, llh, cat_cancel_cb, NULL, NULL, false);
810         if (rc)
811                 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
812                        "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
813         return 0;
814 }
815 EXPORT_SYMBOL(llog_cat_init_and_process);