Whamcloud - gitweb
LU-15481 llog: Add LLOG_SKIP_PLAIN to skip llog plain
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdclass/llog_cat.c
32  *
33  * OST<->MDS recovery logging infrastructure.
34  *
35  * Invariants in implementation:
36  * - we do not share logs among different OST<->MDS connections, so that
37  *   if an OST or MDS fails it need only look at log(s) relevant to itself
38  *
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
41  * Author: Mikhail Pershin <mike.pershin@intel.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LOG
45
46
47 #include <obd_class.h>
48
49 #include "llog_internal.h"
50
51
52 /**
53  * lockdep markers for nested struct llog_handle::lgh_lock locking.
54  */
55 enum {
56         LLOGH_CAT,
57         LLOGH_LOG,
58 };
59
60 /* Create a new log handle and add it to the open list.
61  * This log handle will be closed when all of the records in it are removed.
62  *
63  * Assumes caller has already pushed us into the kernel context and is locking.
64  */
65 static int llog_cat_new_log(const struct lu_env *env,
66                             struct llog_handle *cathandle,
67                             struct llog_handle *loghandle,
68                             struct thandle *th)
69 {
70         struct llog_thread_info *lgi = llog_info(env);
71         struct llog_logid_rec   *rec = &lgi->lgi_logid;
72         struct thandle *handle = NULL;
73         struct dt_device *dt = NULL;
74         struct llog_log_hdr     *llh = cathandle->lgh_hdr;
75         int                      rc, index;
76
77         ENTRY;
78
79         index = (cathandle->lgh_last_idx + 1) %
80                 (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) ? (cfs_fail_val + 1) :
81                                                 LLOG_HDR_BITMAP_SIZE(llh));
82
83         /* check that new llog index will not overlap with the first one.
84          * - llh_cat_idx is the index just before the first/oldest still in-use
85          *      index in catalog
86          * - lgh_last_idx is the last/newest used index in catalog
87          *
88          * When catalog is not wrapped yet then lgh_last_idx is always larger
89          * than llh_cat_idx. After the wrap around lgh_last_idx re-starts
90          * from 0 and llh_cat_idx becomes the upper limit for it
91          *
92          * Check if catalog has already wrapped around or not by comparing
93          * last_idx and cat_idx */
94         if ((index == llh->llh_cat_idx + 1 && llh->llh_count > 1) ||
95             (index == 0 && llh->llh_cat_idx == 0)) {
96                 if (cathandle->lgh_name == NULL) {
97                         CWARN("%s: there are no more free slots in catalog "
98                               DFID":%x\n",
99                               loghandle2name(loghandle),
100                               PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
101                               cathandle->lgh_id.lgl_ogen);
102                 } else {
103                         CWARN("%s: there are no more free slots in "
104                               "catalog %s\n", loghandle2name(loghandle),
105                               cathandle->lgh_name);
106                 }
107                 RETURN(-ENOSPC);
108         }
109
110         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
111                 RETURN(-ENOSPC);
112
113         if (loghandle->lgh_hdr != NULL) {
114                 /* If llog object is remote and creation is failed, lgh_hdr
115                  * might be left over here, free it first */
116                 LASSERT(!llog_exist(loghandle));
117                 OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
118                 loghandle->lgh_hdr = NULL;
119         }
120
121         if (th == NULL) {
122                 dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
123
124                 handle = dt_trans_create(env, dt);
125                 if (IS_ERR(handle))
126                         RETURN(PTR_ERR(handle));
127
128                 /* Create update llog object synchronously, which
129                  * happens during inialization process see
130                  * lod_sub_prep_llog(), to make sure the update
131                  * llog object is created before corss-MDT writing
132                  * updates into the llog object */
133                 if (cathandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
134                         handle->th_sync = 1;
135
136                 handle->th_wait_submit = 1;
137
138                 rc = llog_declare_create(env, loghandle, handle);
139                 if (rc != 0)
140                         GOTO(out, rc);
141
142                 rec->lid_hdr.lrh_len = sizeof(*rec);
143                 rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
144                 rec->lid_id = loghandle->lgh_id;
145                 rc = llog_declare_write_rec(env, cathandle, &rec->lid_hdr, -1,
146                                             handle);
147                 if (rc != 0)
148                         GOTO(out, rc);
149
150                 rc = dt_trans_start_local(env, dt, handle);
151                 if (rc != 0)
152                         GOTO(out, rc);
153
154                 th = handle;
155         }
156
157         rc = llog_create(env, loghandle, th);
158         /* if llog is already created, no need to initialize it */
159         if (rc == -EEXIST) {
160                 GOTO(out, rc = 0);
161         } else if (rc != 0) {
162                 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
163                        loghandle2name(loghandle), rc);
164                 GOTO(out, rc);
165         }
166
167         rc = llog_init_handle(env, loghandle,
168                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
169                               &cathandle->lgh_hdr->llh_tgtuuid);
170         if (rc < 0)
171                 GOTO(out, rc);
172
173         /* build the record for this log in the catalog */
174         rec->lid_hdr.lrh_len = sizeof(*rec);
175         rec->lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
176         rec->lid_id = loghandle->lgh_id;
177
178         /* append the new record into catalog. The new index will be
179          * assigned to the record and updated in rec header */
180         rc = llog_write_rec(env, cathandle, &rec->lid_hdr,
181                             &loghandle->u.phd.phd_cookie, LLOG_NEXT_IDX, th);
182         if (rc < 0)
183                 GOTO(out_destroy, rc);
184
185         CDEBUG(D_OTHER, "new plain log "DFID".%u of catalog "DFID"\n",
186                PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rec->lid_hdr.lrh_index,
187                PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
188
189         loghandle->lgh_hdr->llh_cat_idx = rec->lid_hdr.lrh_index;
190
191         /* limit max size of plain llog so that space can be
192          * released sooner, especially on small filesystems */
193         /* 2MB for the cases when free space hasn't been learned yet */
194         loghandle->lgh_max_size = 2 << 20;
195         dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
196         rc = dt_statfs(env, dt, &lgi->lgi_statfs);
197         if (rc == 0 && lgi->lgi_statfs.os_bfree > 0) {
198                 __u64 freespace = (lgi->lgi_statfs.os_bfree *
199                                   lgi->lgi_statfs.os_bsize) >> 6;
200                 if (freespace < loghandle->lgh_max_size)
201                         loghandle->lgh_max_size = freespace;
202                 /* shouldn't be > 128MB in any case?
203                  * it's 256K records of 512 bytes each */
204                 if (freespace > (128 << 20))
205                         loghandle->lgh_max_size = 128 << 20;
206         }
207         if (unlikely(OBD_FAIL_PRECHECK(OBD_FAIL_PLAIN_RECORDS) ||
208                      OBD_FAIL_PRECHECK(OBD_FAIL_CATALOG_FULL_CHECK))) {
209                 // limit the numer of plain records for test
210                 loghandle->lgh_max_size = loghandle->lgh_hdr_size +
211                        cfs_fail_val * 64;
212         }
213
214         rc = 0;
215
216 out:
217         if (handle != NULL) {
218                 handle->th_result = rc >= 0 ? 0 : rc;
219                 dt_trans_stop(env, dt, handle);
220         }
221         RETURN(rc);
222
223 out_destroy:
224         /* to signal llog_cat_close() it shouldn't try to destroy the llog,
225          * we want to destroy it in this transaction, otherwise the object
226          * becomes an orphan */
227         loghandle->lgh_hdr->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
228         /* this is to mimic full log, so another llog_cat_current_log()
229          * can skip it and ask for another onet */
230         loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(loghandle->lgh_hdr) + 1;
231         llog_trans_destroy(env, loghandle, th);
232         if (handle != NULL)
233                 dt_trans_stop(env, dt, handle);
234         RETURN(rc);
235 }
236
237 static int llog_cat_refresh(const struct lu_env *env,
238                             struct llog_handle *cathandle)
239 {
240         struct llog_handle *loghandle;
241         int rc;
242
243         down_write(&cathandle->lgh_lock);
244         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
245                             u.phd.phd_entry) {
246                 if (!llog_exist(loghandle))
247                         continue;
248
249                 down_write(&loghandle->lgh_lock);
250                 rc = llog_read_header(env, loghandle, NULL);
251                 up_write(&loghandle->lgh_lock);
252                 if (rc)
253                         goto unlock;
254         }
255
256         rc = llog_read_header(env, cathandle, NULL);
257 unlock:
258         up_write(&cathandle->lgh_lock);
259
260         return rc;
261 }
262
263 /*
264  * prepare current/next log for catalog.
265  *
266  * if \a *ploghandle is NULL, open it, and declare create, NB, if \a
267  * *ploghandle is remote, create it synchronously here, see comments
268  * below.
269  *
270  * \a cathandle->lgh_lock is down_read-ed, it gets down_write-ed if \a
271  * *ploghandle has to be opened.
272  */
273 static int llog_cat_prep_log(const struct lu_env *env,
274                              struct llog_handle *cathandle,
275                              struct llog_handle **ploghandle,
276                              struct thandle *th)
277 {
278         int rc;
279         int sem_upgraded;
280
281 start:
282         rc = 0;
283         sem_upgraded = 0;
284         if (IS_ERR_OR_NULL(*ploghandle)) {
285                 up_read(&cathandle->lgh_lock);
286                 down_write(&cathandle->lgh_lock);
287                 sem_upgraded = 1;
288                 if (IS_ERR_OR_NULL(*ploghandle)) {
289                         struct llog_handle *loghandle;
290
291                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
292                                        NULL, NULL, LLOG_OPEN_NEW);
293                         if (!rc) {
294                                 *ploghandle = loghandle;
295                                 list_add_tail(&loghandle->u.phd.phd_entry,
296                                               &cathandle->u.chd.chd_head);
297                         }
298                 }
299                 if (rc)
300                         GOTO(out, rc);
301         }
302
303         rc = llog_exist(*ploghandle);
304         if (rc < 0)
305                 GOTO(out, rc);
306         if (rc)
307                 GOTO(out, rc = 0);
308
309         if (dt_object_remote(cathandle->lgh_obj)) {
310                 down_write_nested(&(*ploghandle)->lgh_lock, LLOGH_LOG);
311                 if (!llog_exist(*ploghandle)) {
312                         /* For remote operation, if we put the llog object
313                          * creation in the current transaction, then the
314                          * llog object will not be created on the remote
315                          * target until the transaction stop, if other
316                          * operations start before the transaction stop,
317                          * and use the same llog object, will be dependent
318                          * on the success of this transaction. So let's
319                          * create the llog object synchronously here to
320                          * remove the dependency. */
321                         rc = llog_cat_new_log(env, cathandle, *ploghandle,
322                                               NULL);
323                         if (rc == -ESTALE) {
324                                 up_write(&(*ploghandle)->lgh_lock);
325                                 if (sem_upgraded)
326                                         up_write(&cathandle->lgh_lock);
327                                 else
328                                         up_read(&cathandle->lgh_lock);
329
330                                 rc = llog_cat_refresh(env, cathandle);
331                                 down_read_nested(&cathandle->lgh_lock,
332                                                  LLOGH_CAT);
333                                 if (rc)
334                                         return rc;
335                                 /* *ploghandle might become NULL, restart */
336                                 goto start;
337                         }
338                 }
339                 up_write(&(*ploghandle)->lgh_lock);
340         } else {
341                 struct llog_thread_info *lgi = llog_info(env);
342                 struct llog_logid_rec *lirec = &lgi->lgi_logid;
343
344                 rc = llog_declare_create(env, *ploghandle, th);
345                 if (rc)
346                         GOTO(out, rc);
347
348                 lirec->lid_hdr.lrh_len = sizeof(*lirec);
349                 rc = llog_declare_write_rec(env, cathandle, &lirec->lid_hdr, -1,
350                                             th);
351         }
352
353 out:
354         if (sem_upgraded) {
355                 up_write(&cathandle->lgh_lock);
356                 down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
357                 if (rc == 0)
358                         goto start;
359         }
360         return rc;
361 }
362
363 /* Open an existent log handle and add it to the open list.
364  * This log handle will be closed when all of the records in it are removed.
365  *
366  * Assumes caller has already pushed us into the kernel context and is locking.
367  * We return a lock on the handle to ensure nobody yanks it from us.
368  *
369  * This takes extra reference on llog_handle via llog_handle_get() and require
370  * this reference to be put by caller using llog_handle_put()
371  */
372 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
373                        struct llog_handle **res, struct llog_logid *logid)
374 {
375         struct llog_handle      *loghandle;
376         enum llog_flag           fmt;
377         int                      rc = 0;
378
379         ENTRY;
380
381         if (cathandle == NULL)
382                 RETURN(-EBADF);
383
384         fmt = cathandle->lgh_hdr->llh_flags & LLOG_F_EXT_MASK;
385         down_write(&cathandle->lgh_lock);
386         list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
387                             u.phd.phd_entry) {
388                 struct llog_logid *cgl = &loghandle->lgh_id;
389
390                 if (ostid_id(&cgl->lgl_oi) == ostid_id(&logid->lgl_oi) &&
391                     ostid_seq(&cgl->lgl_oi) == ostid_seq(&logid->lgl_oi)) {
392                         if (cgl->lgl_ogen != logid->lgl_ogen) {
393                                 CWARN("%s: log "DFID" generation %x != %x\n",
394                                       loghandle2name(loghandle),
395                                       PFID(&logid->lgl_oi.oi_fid),
396                                       cgl->lgl_ogen, logid->lgl_ogen);
397                                 continue;
398                         }
399                         *res = llog_handle_get(loghandle);
400                         if (!*res) {
401                                 CERROR("%s: log "DFID" refcount is zero!\n",
402                                        loghandle2name(loghandle),
403                                        PFID(&logid->lgl_oi.oi_fid));
404                                 continue;
405                         }
406                         loghandle->u.phd.phd_cat_handle = cathandle;
407                         up_write(&cathandle->lgh_lock);
408                         RETURN(rc);
409                 }
410         }
411         up_write(&cathandle->lgh_lock);
412
413         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
414                        LLOG_OPEN_EXISTS);
415         if (rc < 0) {
416                 CERROR("%s: error opening log id "DFID":%x: rc = %d\n",
417                        loghandle2name(cathandle), PFID(&logid->lgl_oi.oi_fid),
418                        logid->lgl_ogen, rc);
419                 RETURN(rc);
420         }
421
422         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN |
423                               LLOG_F_ZAP_WHEN_EMPTY | fmt, NULL);
424         if (rc < 0) {
425                 llog_close(env, loghandle);
426                 *res = NULL;
427                 RETURN(rc);
428         }
429
430         *res = llog_handle_get(loghandle);
431         LASSERT(*res);
432         down_write(&cathandle->lgh_lock);
433         list_add_tail(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
434         up_write(&cathandle->lgh_lock);
435
436         loghandle->u.phd.phd_cat_handle = cathandle;
437         loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
438         loghandle->u.phd.phd_cookie.lgc_index =
439                                 loghandle->lgh_hdr->llh_cat_idx;
440         RETURN(0);
441 }
442
443 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
444 {
445         struct llog_handle      *loghandle, *n;
446         int                      rc;
447
448         ENTRY;
449
450         list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
451                                  u.phd.phd_entry) {
452                 struct llog_log_hdr     *llh = loghandle->lgh_hdr;
453                 int                      index;
454
455                 /* unlink open-not-created llogs */
456                 list_del_init(&loghandle->u.phd.phd_entry);
457                 llh = loghandle->lgh_hdr;
458                 if (loghandle->lgh_obj != NULL && llh != NULL &&
459                     (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
460                     (llh->llh_count == 1)) {
461                         rc = llog_destroy(env, loghandle);
462                         if (rc)
463                                 CERROR("%s: failure destroying log during "
464                                        "cleanup: rc = %d\n",
465                                        loghandle2name(loghandle), rc);
466
467                         index = loghandle->u.phd.phd_cookie.lgc_index;
468                         llog_cat_cleanup(env, cathandle, NULL, index);
469                 }
470                 llog_close(env, loghandle);
471         }
472         /* if handle was stored in ctxt, remove it too */
473         if (cathandle->lgh_ctxt->loc_handle == cathandle)
474                 cathandle->lgh_ctxt->loc_handle = NULL;
475         rc = llog_close(env, cathandle);
476         RETURN(rc);
477 }
478 EXPORT_SYMBOL(llog_cat_close);
479
480 /** Return the currently active log handle.  If the current log handle doesn't
481  * have enough space left for the current record, start a new one.
482  *
483  * If reclen is 0, we only want to know what the currently active log is,
484  * otherwise we get a lock on this log so nobody can steal our space.
485  *
486  * Assumes caller has already pushed us into the kernel context and is locking.
487  *
488  * NOTE: loghandle is write-locked upon successful return
489  */
490 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
491                                                 struct thandle *th)
492 {
493         struct llog_handle *loghandle = NULL;
494         ENTRY;
495
496
497         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2)) {
498                 down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
499                 GOTO(next, loghandle);
500         }
501
502         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
503         loghandle = cathandle->u.chd.chd_current_log;
504         if (loghandle) {
505                 struct llog_log_hdr *llh;
506
507                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
508                 llh = loghandle->lgh_hdr;
509                 if (llh == NULL || !llog_is_full(loghandle)) {
510                         up_read(&cathandle->lgh_lock);
511                         RETURN(loghandle);
512                 } else {
513                         up_write(&loghandle->lgh_lock);
514                 }
515         }
516         up_read(&cathandle->lgh_lock);
517
518         /* time to use next log */
519
520         /* first, we have to make sure the state hasn't changed */
521         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
522         loghandle = cathandle->u.chd.chd_current_log;
523         if (loghandle) {
524                 struct llog_log_hdr *llh;
525
526                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
527                 llh = loghandle->lgh_hdr;
528                 if (llh == NULL || !llog_is_full(loghandle))
529                         GOTO(out_unlock, loghandle);
530                 else
531                         up_write(&loghandle->lgh_lock);
532         }
533
534 next:
535         /* Sigh, the chd_next_log and chd_current_log is initialized
536          * in declare phase, and we do not serialize the catlog
537          * accessing, so it might be possible the llog creation
538          * thread (see llog_cat_declare_add_rec()) did not create
539          * llog successfully, then the following thread might
540          * meet this situation. */
541         if (IS_ERR_OR_NULL(cathandle->u.chd.chd_next_log)) {
542                 CERROR("%s: next log does not exist!\n",
543                        loghandle2name(cathandle));
544                 loghandle = ERR_PTR(-EIO);
545                 if (cathandle->u.chd.chd_next_log == NULL) {
546                         /* Store the error in chd_next_log, so
547                          * the following process can get correct
548                          * failure value */
549                         cathandle->u.chd.chd_next_log = loghandle;
550                 }
551                 GOTO(out_unlock, loghandle);
552         }
553
554         CDEBUG(D_INODE, "use next log\n");
555
556         loghandle = cathandle->u.chd.chd_next_log;
557         cathandle->u.chd.chd_current_log = loghandle;
558         cathandle->u.chd.chd_next_log = NULL;
559         down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
560
561 out_unlock:
562         up_write(&cathandle->lgh_lock);
563         LASSERT(loghandle);
564         RETURN(loghandle);
565 }
566
567 /* Add a single record to the recovery log(s) using a catalog
568  * Returns as llog_write_record
569  *
570  * Assumes caller has already pushed us into the kernel context.
571  */
572 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
573                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
574                      struct thandle *th)
575 {
576         struct llog_handle *loghandle;
577         int rc, retried = 0;
578         ENTRY;
579
580         LASSERT(rec->lrh_len <= cathandle->lgh_ctxt->loc_chunk_size);
581
582 retry:
583         loghandle = llog_cat_current_log(cathandle, th);
584         if (IS_ERR(loghandle))
585                 RETURN(PTR_ERR(loghandle));
586
587         /* loghandle is already locked by llog_cat_current_log() for us */
588         if (!llog_exist(loghandle)) {
589                 rc = llog_cat_new_log(env, cathandle, loghandle, th);
590                 if (rc < 0) {
591                         up_write(&loghandle->lgh_lock);
592                         /* nobody should be trying to use this llog */
593                         down_write(&cathandle->lgh_lock);
594                         if (cathandle->u.chd.chd_current_log == loghandle)
595                                 cathandle->u.chd.chd_current_log = NULL;
596                         up_write(&cathandle->lgh_lock);
597                         RETURN(rc);
598                 }
599         }
600         /* now let's try to add the record */
601         rc = llog_write_rec(env, loghandle, rec, reccookie, LLOG_NEXT_IDX, th);
602         if (rc < 0) {
603                 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
604                              "llog_write_rec %d: lh=%p\n", rc, loghandle);
605                 /* -ENOSPC is returned if no empty records left
606                  * and when it's lack of space on the stogage.
607                  * there is no point to try again if it's the second
608                  * case. many callers (like llog test) expect ENOSPC,
609                  * so we preserve this error code, but look for the
610                  * actual cause here */
611                 if (rc == -ENOSPC && llog_is_full(loghandle))
612                         rc = -ENOBUFS;
613         }
614         up_write(&loghandle->lgh_lock);
615
616         if (rc == -ENOBUFS) {
617                 if (retried++ == 0)
618                         GOTO(retry, rc);
619                 CERROR("%s: error on 2nd llog: rc = %d\n",
620                        loghandle2name(cathandle), rc);
621         }
622
623         RETURN(rc);
624 }
625 EXPORT_SYMBOL(llog_cat_add_rec);
626
627 int llog_cat_declare_add_rec(const struct lu_env *env,
628                              struct llog_handle *cathandle,
629                              struct llog_rec_hdr *rec, struct thandle *th)
630 {
631         int rc;
632
633         ENTRY;
634
635 start:
636         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
637         rc = llog_cat_prep_log(env, cathandle,
638                                &cathandle->u.chd.chd_current_log, th);
639         if (rc)
640                 GOTO(unlock, rc);
641
642         rc = llog_cat_prep_log(env, cathandle, &cathandle->u.chd.chd_next_log,
643                                th);
644         if (rc)
645                 GOTO(unlock, rc);
646
647         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
648                                     rec, -1, th);
649         if (rc == -ESTALE && dt_object_remote(cathandle->lgh_obj)) {
650                 up_read(&cathandle->lgh_lock);
651                 rc = llog_cat_refresh(env, cathandle);
652                 if (rc)
653                         RETURN(rc);
654                 goto start;
655         }
656
657 #if 0
658         /*
659          * XXX: we hope for declarations made for existing llog this might be
660          * not correct with some backends where declarations are expected
661          * against specific object like ZFS with full debugging enabled.
662          */
663         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_next_log, rec, -1,
664                                     th);
665 #endif
666 unlock:
667         up_read(&cathandle->lgh_lock);
668         RETURN(rc);
669 }
670 EXPORT_SYMBOL(llog_cat_declare_add_rec);
671
672 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
673                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie)
674 {
675         struct llog_ctxt        *ctxt;
676         struct dt_device        *dt;
677         struct thandle          *th = NULL;
678         int                      rc;
679
680         ctxt = cathandle->lgh_ctxt;
681         LASSERT(ctxt);
682         LASSERT(ctxt->loc_exp);
683
684         LASSERT(cathandle->lgh_obj != NULL);
685         dt = lu2dt_dev(cathandle->lgh_obj->do_lu.lo_dev);
686
687         th = dt_trans_create(env, dt);
688         if (IS_ERR(th))
689                 RETURN(PTR_ERR(th));
690
691         rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
692         if (rc)
693                 GOTO(out_trans, rc);
694
695         rc = dt_trans_start_local(env, dt, th);
696         if (rc)
697                 GOTO(out_trans, rc);
698         rc = llog_cat_add_rec(env, cathandle, rec, reccookie, th);
699 out_trans:
700         dt_trans_stop(env, dt, th);
701         RETURN(rc);
702 }
703 EXPORT_SYMBOL(llog_cat_add);
704
705 int llog_cat_cancel_arr_rec(const struct lu_env *env,
706                             struct llog_handle *cathandle,
707                             struct llog_logid *lgl, int count, int *index)
708 {
709         struct llog_handle *loghandle;
710         int  rc;
711
712         ENTRY;
713         rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
714         if (rc) {
715                 CDEBUG(D_HA, "%s: cannot find llog for handle "DFID":%x"
716                        ": rc = %d\n", loghandle2name(cathandle),
717                        PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
718                 RETURN(rc);
719         }
720
721         if ((cathandle->lgh_ctxt->loc_flags &
722              LLOG_CTXT_FLAG_NORMAL_FID) && !llog_exist(loghandle)) {
723                 /* For update log, some of loghandles of cathandle
724                  * might not exist because remote llog creation might
725                  * be failed, so let's skip the record cancellation
726                  * for these non-exist llogs.
727                  */
728                 rc = -ENOENT;
729                 CDEBUG(D_HA, "%s: llog "DFID":%x does not exist"
730                        ": rc = %d\n", loghandle2name(cathandle),
731                        PFID(&lgl->lgl_oi.oi_fid), lgl->lgl_ogen, rc);
732
733                 llog_handle_put(env, loghandle);
734                 RETURN(rc);
735         }
736
737         rc = llog_cancel_arr_rec(env, loghandle, count, index);
738         if (rc == LLOG_DEL_PLAIN) { /* log has been destroyed */
739                 int cat_index;
740
741                 cat_index = loghandle->u.phd.phd_cookie.lgc_index;
742                 rc = llog_cat_cleanup(env, cathandle, loghandle, cat_index);
743                 if (rc)
744                         CERROR("%s: fail to cancel catalog record: rc = %d\n",
745                                loghandle2name(cathandle), rc);
746                 rc = 0;
747
748         }
749         llog_handle_put(env, loghandle);
750
751         if (rc)
752                 CERROR("%s: fail to cancel %d llog-records: rc = %d\n",
753                        loghandle2name(cathandle), count, rc);
754
755         RETURN(rc);
756 }
757 EXPORT_SYMBOL(llog_cat_cancel_arr_rec);
758
759 /* For each cookie in the cookie array, we clear the log in-use bit and either:
760  * - the log is empty, so mark it free in the catalog header and delete it
761  * - the log is not empty, just write out the log header
762  *
763  * The cookies may be in different log files, so we need to get new logs
764  * each time.
765  *
766  * Assumes caller has already pushed us into the kernel context.
767  */
768 int llog_cat_cancel_records(const struct lu_env *env,
769                             struct llog_handle *cathandle, int count,
770                             struct llog_cookie *cookies)
771 {
772         int i, rc = 0, failed = 0;
773
774         ENTRY;
775
776         for (i = 0; i < count; i++, cookies++) {
777                 int lrc;
778
779                 lrc = llog_cat_cancel_arr_rec(env, cathandle, &cookies->lgc_lgl,
780                                              1, &cookies->lgc_index);
781                 if (lrc) {
782                         failed++;
783                         if (!rc)
784                                 rc = lrc;
785                 }
786         }
787         if (failed)
788                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
789                        loghandle2name(cathandle), failed, count, rc);
790         RETURN(rc);
791 }
792 EXPORT_SYMBOL(llog_cat_cancel_records);
793
794 static int llog_cat_process_common(const struct lu_env *env,
795                                    struct llog_handle *cat_llh,
796                                    struct llog_rec_hdr *rec,
797                                    struct llog_handle **llhp)
798 {
799         struct llog_logid_rec *lir = container_of(rec, typeof(*lir), lid_hdr);
800         struct llog_log_hdr *hdr;
801         int rc;
802
803         ENTRY;
804         if (rec->lrh_type != le32_to_cpu(LLOG_LOGID_MAGIC)) {
805                 rc = -EINVAL;
806                 CWARN("%s: invalid record in catalog "DFID":%x: rc = %d\n",
807                       loghandle2name(cat_llh),
808                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid),
809                       cat_llh->lgh_id.lgl_ogen, rc);
810                 RETURN(rc);
811         }
812         CDEBUG(D_HA, "processing log "DFID":%x at index %u of catalog "DFID"\n",
813                PFID(&lir->lid_id.lgl_oi.oi_fid), lir->lid_id.lgl_ogen,
814                le32_to_cpu(rec->lrh_index),
815                PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
816
817         rc = llog_cat_id2handle(env, cat_llh, llhp, &lir->lid_id);
818         if (rc) {
819                 /* After a server crash, a stub of index record in catlog could
820                  * be kept, because plain log destroy + catlog index record
821                  * deletion are not atomic. So we end up with an index but no
822                  * actual record. Destroy the index and move on. */
823                 if (rc == -ENOENT || rc == -ESTALE)
824                         rc = LLOG_DEL_RECORD;
825                 else if (rc)
826                         CWARN("%s: can't find llog handle "DFID":%x: rc = %d\n",
827                               loghandle2name(cat_llh),
828                               PFID(&lir->lid_id.lgl_oi.oi_fid),
829                               lir->lid_id.lgl_ogen, rc);
830
831                 RETURN(rc);
832         }
833
834         /* clean old empty llogs, do not consider current llog in use */
835         /* ignore remote (lgh_obj == NULL) llogs */
836         hdr = (*llhp)->lgh_hdr;
837         if ((hdr->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
838             hdr->llh_count == 1 && cat_llh->lgh_obj != NULL &&
839             *llhp != cat_llh->u.chd.chd_current_log &&
840             *llhp != cat_llh->u.chd.chd_next_log) {
841                 rc = llog_destroy(env, *llhp);
842                 if (rc)
843                         CWARN("%s: can't destroy empty log "DFID": rc = %d\n",
844                               loghandle2name((*llhp)),
845                               PFID(&lir->lid_id.lgl_oi.oi_fid), rc);
846                 rc = LLOG_DEL_PLAIN;
847         }
848
849         RETURN(rc);
850 }
851
852 static int llog_cat_process_cb(const struct lu_env *env,
853                                struct llog_handle *cat_llh,
854                                struct llog_rec_hdr *rec, void *data)
855 {
856         struct llog_process_data *d = data;
857         struct llog_handle *llh = NULL;
858         int rc;
859
860         ENTRY;
861         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
862         if (rc)
863                 GOTO(out, rc);
864
865         if (rec->lrh_index < d->lpd_startcat) {
866                 /* Skip processing of the logs until startcat */
867                 rc = 0;
868         } else if (d->lpd_startidx > 0) {
869                 struct llog_process_cat_data cd;
870
871                 cd.lpcd_read_mode = LLOG_READ_MODE_NORMAL;
872                 cd.lpcd_first_idx = d->lpd_startidx;
873                 cd.lpcd_last_idx = 0;
874                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
875                                           &cd, false);
876                 /* Continue processing the next log from idx 0 */
877                 d->lpd_startidx = 0;
878         } else {
879                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
880                                           NULL, false);
881         }
882         if (rc == -ENOENT && (cat_llh->lgh_hdr->llh_flags & LLOG_F_RM_ON_ERR)) {
883                 /*
884                  * plain llog is reported corrupted, so better to just remove
885                  * it if the caller is fine with that.
886                  */
887                 CERROR("%s: remove corrupted/missing llog "DFID"\n",
888                        loghandle2name(cat_llh),
889                        PFID(&llh->lgh_id.lgl_oi.oi_fid));
890                 rc = LLOG_DEL_PLAIN;
891         }
892
893 out:
894         /* The empty plain log was destroyed while processing */
895         if (rc == LLOG_DEL_PLAIN || rc == LLOG_DEL_RECORD)
896                 /* clear wrong catalog entry */
897                 rc = llog_cat_cleanup(env, cat_llh, llh, rec->lrh_index);
898         else if (rc == LLOG_SKIP_PLAIN)
899                 /* processing callback ask to skip the llog -> continue */
900                 rc = 0;
901
902         if (llh)
903                 llog_handle_put(env, llh);
904
905         RETURN(rc);
906 }
907
908 int llog_cat_process_or_fork(const struct lu_env *env,
909                              struct llog_handle *cat_llh, llog_cb_t cat_cb,
910                              llog_cb_t cb, void *data, int startcat,
911                              int startidx, bool fork)
912 {
913         struct llog_process_data d;
914         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
915         int rc;
916
917         ENTRY;
918
919         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
920         d.lpd_data = data;
921         d.lpd_cb = cb;
922         d.lpd_startcat = (startcat == LLOG_CAT_FIRST ? 0 : startcat);
923         d.lpd_startidx = startidx;
924
925         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
926             llh->llh_count > 1) {
927                 struct llog_process_cat_data cd = {
928                         .lpcd_read_mode = LLOG_READ_MODE_NORMAL
929                 };
930
931                 CWARN("%s: catlog "DFID" crosses index zero\n",
932                       loghandle2name(cat_llh),
933                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
934                 /*startcat = 0 is default value for general processing */
935                 if ((startcat != LLOG_CAT_FIRST &&
936                     startcat >= llh->llh_cat_idx) || !startcat) {
937                         /* processing the catalog part at the end */
938                         cd.lpcd_first_idx = (startcat ? startcat :
939                                              llh->llh_cat_idx);
940                         if (OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS))
941                                 cd.lpcd_last_idx = cfs_fail_val;
942                         else
943                                 cd.lpcd_last_idx = 0;
944                         rc = llog_process_or_fork(env, cat_llh, cat_cb,
945                                                   &d, &cd, fork);
946                         /* Reset the startcat becasue it has already reached
947                          * catalog bottom.
948                          */
949                         startcat = 0;
950                         d.lpd_startcat = 0;
951                         if (rc != 0)
952                                 RETURN(rc);
953                 }
954                 /* processing the catalog part at the begining */
955                 cd.lpcd_first_idx = (startcat == LLOG_CAT_FIRST) ? 0 : startcat;
956                 /* Note, the processing will stop at the lgh_last_idx value,
957                  * and it could be increased during processing. So records
958                  * between current lgh_last_idx and lgh_last_idx in future
959                  * would left unprocessed.
960                  */
961                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
962                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
963                                           &d, &cd, fork);
964         } else {
965                 rc = llog_process_or_fork(env, cat_llh, cat_cb,
966                                           &d, NULL, fork);
967         }
968
969         RETURN(rc);
970 }
971 EXPORT_SYMBOL(llog_cat_process_or_fork);
972
973 int llog_cat_process(const struct lu_env *env, struct llog_handle *cat_llh,
974                      llog_cb_t cb, void *data, int startcat, int startidx)
975 {
976         return llog_cat_process_or_fork(env, cat_llh, llog_cat_process_cb,
977                                         cb, data, startcat, startidx, false);
978 }
979 EXPORT_SYMBOL(llog_cat_process);
980
981 static int llog_cat_size_cb(const struct lu_env *env,
982                              struct llog_handle *cat_llh,
983                              struct llog_rec_hdr *rec, void *data)
984 {
985         struct llog_process_data *d = data;
986         struct llog_handle *llh = NULL;
987         __u64 *cum_size = d->lpd_data;
988         __u64 size;
989         int rc;
990
991         ENTRY;
992         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
993
994         if (rc == LLOG_DEL_PLAIN) {
995                 /* empty log was deleted, don't count it */
996                 rc = llog_cat_cleanup(env, cat_llh, llh,
997                                       llh->u.phd.phd_cookie.lgc_index);
998         } else if (rc == LLOG_DEL_RECORD) {
999                 /* clear wrong catalog entry */
1000                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1001         } else {
1002                 size = llog_size(env, llh);
1003                 *cum_size += size;
1004
1005                 CDEBUG(D_INFO, "Add llog entry "DFID" size=%llu, tot=%llu\n",
1006                        PFID(&llh->lgh_id.lgl_oi.oi_fid), size, *cum_size);
1007         }
1008
1009         if (llh != NULL)
1010                 llog_handle_put(env, llh);
1011
1012         RETURN(0);
1013 }
1014
1015 __u64 llog_cat_size(const struct lu_env *env, struct llog_handle *cat_llh)
1016 {
1017         __u64 size = llog_size(env, cat_llh);
1018
1019         llog_cat_process_or_fork(env, cat_llh, llog_cat_size_cb,
1020                                  NULL, &size, 0, 0, false);
1021
1022         return size;
1023 }
1024 EXPORT_SYMBOL(llog_cat_size);
1025
1026 /* currently returns the number of "free" entries in catalog,
1027  * ie the available entries for a new plain LLOG file creation,
1028  * even if catalog has wrapped
1029  */
1030 __u32 llog_cat_free_space(struct llog_handle *cat_llh)
1031 {
1032         /* simulate almost full Catalog */
1033         if (OBD_FAIL_CHECK(OBD_FAIL_CAT_FREE_RECORDS))
1034                 return cfs_fail_val;
1035
1036         if (cat_llh->lgh_hdr->llh_count == 1)
1037                 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1;
1038
1039         if (cat_llh->lgh_last_idx > cat_llh->lgh_hdr->llh_cat_idx)
1040                 return LLOG_HDR_BITMAP_SIZE(cat_llh->lgh_hdr) - 1 +
1041                        cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1042
1043         /* catalog is presently wrapped */
1044         return cat_llh->lgh_hdr->llh_cat_idx - cat_llh->lgh_last_idx;
1045 }
1046 EXPORT_SYMBOL(llog_cat_free_space);
1047
1048 static int llog_cat_reverse_process_cb(const struct lu_env *env,
1049                                        struct llog_handle *cat_llh,
1050                                        struct llog_rec_hdr *rec, void *data)
1051 {
1052         struct llog_process_data *d = data;
1053         struct llog_handle *llh;
1054         int rc;
1055
1056         ENTRY;
1057         rc = llog_cat_process_common(env, cat_llh, rec, &llh);
1058
1059         /* The empty plain log was destroyed while processing */
1060         if (rc == LLOG_DEL_PLAIN) {
1061                 rc = llog_cat_cleanup(env, cat_llh, llh,
1062                                       llh->u.phd.phd_cookie.lgc_index);
1063         } else if (rc == LLOG_DEL_RECORD) {
1064                 /* clear wrong catalog entry */
1065                 rc = llog_cat_cleanup(env, cat_llh, NULL, rec->lrh_index);
1066         } else if (rc == LLOG_SKIP_PLAIN) {
1067                 /* processing callback ask to skip the llog -> continue */
1068                 rc = 0;
1069         }
1070         if (rc)
1071                 RETURN(rc);
1072
1073         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
1074
1075         /* The empty plain was destroyed while processing */
1076         if (rc == LLOG_DEL_PLAIN)
1077                 rc = llog_cat_cleanup(env, cat_llh, llh,
1078                                       llh->u.phd.phd_cookie.lgc_index);
1079
1080         llog_handle_put(env, llh);
1081         RETURN(rc);
1082 }
1083
1084 int llog_cat_reverse_process(const struct lu_env *env,
1085                              struct llog_handle *cat_llh,
1086                              llog_cb_t cb, void *data)
1087 {
1088         struct llog_process_data d;
1089         struct llog_process_cat_data cd;
1090         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
1091         int rc;
1092         ENTRY;
1093
1094         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
1095         cd.lpcd_read_mode = LLOG_READ_MODE_NORMAL;
1096         d.lpd_data = data;
1097         d.lpd_cb = cb;
1098
1099         if (llh->llh_cat_idx >= cat_llh->lgh_last_idx &&
1100             llh->llh_count > 1) {
1101                 CWARN("%s: catalog "DFID" crosses index zero\n",
1102                       loghandle2name(cat_llh),
1103                       PFID(&cat_llh->lgh_id.lgl_oi.oi_fid));
1104
1105                 cd.lpcd_first_idx = 0;
1106                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
1107                 rc = llog_reverse_process(env, cat_llh,
1108                                           llog_cat_reverse_process_cb,
1109                                           &d, &cd);
1110                 if (rc != 0)
1111                         RETURN(rc);
1112
1113                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
1114                 cd.lpcd_last_idx = 0;
1115                 rc = llog_reverse_process(env, cat_llh,
1116                                           llog_cat_reverse_process_cb,
1117                                           &d, &cd);
1118         } else {
1119                 rc = llog_reverse_process(env, cat_llh,
1120                                           llog_cat_reverse_process_cb,
1121                                           &d, NULL);
1122         }
1123
1124         RETURN(rc);
1125 }
1126 EXPORT_SYMBOL(llog_cat_reverse_process);
1127
1128 static int llog_cat_set_first_idx(struct llog_handle *cathandle, int idx)
1129 {
1130         struct llog_log_hdr *llh = cathandle->lgh_hdr;
1131         int bitmap_size;
1132
1133         ENTRY;
1134
1135         bitmap_size = LLOG_HDR_BITMAP_SIZE(llh);
1136         /*
1137          * The llh_cat_idx equals to the first used index minus 1
1138          * so if we canceled the first index then llh_cat_idx
1139          * must be renewed.
1140          */
1141         if (llh->llh_cat_idx == (idx - 1)) {
1142                 llh->llh_cat_idx = idx;
1143
1144                 while (idx != cathandle->lgh_last_idx) {
1145                         idx = (idx + 1) % bitmap_size;
1146                         if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
1147                                 /* update llh_cat_idx for each unset bit,
1148                                  * expecting the next one is set */
1149                                 llh->llh_cat_idx = idx;
1150                         } else if (idx == 0) {
1151                                 /* skip header bit */
1152                                 llh->llh_cat_idx = 0;
1153                                 continue;
1154                         } else {
1155                                 /* the first index is found */
1156                                 break;
1157                         }
1158                 }
1159
1160                 CDEBUG(D_HA, "catlog "DFID" first idx %u, last_idx %u\n",
1161                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid),
1162                        llh->llh_cat_idx, cathandle->lgh_last_idx);
1163         }
1164
1165         RETURN(0);
1166 }
1167
1168 /* Cleanup deleted plain llog traces from catalog */
1169 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
1170                      struct llog_handle *loghandle, int index)
1171 {
1172         int rc;
1173         struct lu_fid fid = {.f_seq = 0, .f_oid = 0, .f_ver = 0};
1174
1175         LASSERT(index);
1176         if (loghandle != NULL) {
1177                 /* remove destroyed llog from catalog list and
1178                  * chd_current_log variable */
1179                 fid = loghandle->lgh_id.lgl_oi.oi_fid;
1180                 down_write(&cathandle->lgh_lock);
1181                 if (cathandle->u.chd.chd_current_log == loghandle)
1182                         cathandle->u.chd.chd_current_log = NULL;
1183                 list_del_init(&loghandle->u.phd.phd_entry);
1184                 up_write(&cathandle->lgh_lock);
1185                 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index ||
1186                         loghandle->u.phd.phd_cookie.lgc_index == 0);
1187                 /* llog was opened and keep in a list, close it now */
1188                 llog_close(env, loghandle);
1189         }
1190
1191         /* do not attempt to cleanup on-disk llog if on client side */
1192         if (cathandle->lgh_obj == NULL)
1193                 return 0;
1194
1195         /* remove plain llog entry from catalog by index */
1196         llog_cat_set_first_idx(cathandle, index);
1197         rc = llog_cancel_rec(env, cathandle, index);
1198         if (rc == 0)
1199                 CDEBUG(D_HA,
1200                        "cancel plain log "DFID" at index %u of catalog "DFID"\n",
1201                        PFID(&fid), index,
1202                        PFID(&cathandle->lgh_id.lgl_oi.oi_fid));
1203         return rc;
1204 }