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