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