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