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