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