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