Whamcloud - gitweb
fbf2766df0969ad0371bcbaa243679ce475a95d1
[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, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog_cat.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  *
40  * Invariants in implementation:
41  * - we do not share logs among different OST<->MDS connections, so that
42  *   if an OST or MDS fails it need only look at log(s) relevant to itself
43  *
44  * Author: Andreas Dilger <adilger@clusterfs.com>
45  * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
46  * Author: Mikhail Pershin <mike.pershin@intel.com>
47  */
48
49 #define DEBUG_SUBSYSTEM S_LOG
50
51 #ifndef __KERNEL__
52 #include <liblustre.h>
53 #endif
54
55 #include <obd_class.h>
56
57 #include "llog_internal.h"
58
59 /* Create a new log handle and add it to the open list.
60  * This log handle will be closed when all of the records in it are removed.
61  *
62  * Assumes caller has already pushed us into the kernel context and is locking.
63  */
64 static int llog_cat_new_log(const struct lu_env *env,
65                             struct llog_handle *cathandle,
66                             struct llog_handle *loghandle,
67                             struct thandle *th)
68 {
69
70         struct llog_log_hdr *llh;
71         struct llog_logid_rec rec = { { 0 }, };
72         int rc, index, bitmap_size;
73         ENTRY;
74
75         llh = cathandle->lgh_hdr;
76         bitmap_size = LLOG_BITMAP_SIZE(llh);
77
78         index = (cathandle->lgh_last_idx + 1) % bitmap_size;
79
80         /* maximum number of available slots in catlog is bitmap_size - 2 */
81         if (llh->llh_cat_idx == index) {
82                 CERROR("no free catalog slots for log...\n");
83                 RETURN(-ENOSPC);
84         }
85
86         if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED))
87                 RETURN(-ENOSPC);
88
89         rc = llog_create(env, loghandle, th);
90         /* if llog is already created, no need to initialize it */
91         if (rc == -EEXIST) {
92                 RETURN(0);
93         } else if (rc != 0) {
94                 CERROR("%s: can't create new plain llog in catalog: rc = %d\n",
95                        loghandle->lgh_ctxt->loc_obd->obd_name, rc);
96                 RETURN(rc);
97         }
98
99         rc = llog_init_handle(env, loghandle,
100                               LLOG_F_IS_PLAIN | LLOG_F_ZAP_WHEN_EMPTY,
101                               &cathandle->lgh_hdr->llh_tgtuuid);
102         if (rc)
103                 GOTO(out_destroy, rc);
104
105         if (index == 0)
106                 index = 1;
107
108         spin_lock(&loghandle->lgh_hdr_lock);
109         llh->llh_count++;
110         if (ext2_set_bit(index, llh->llh_bitmap)) {
111                 CERROR("argh, index %u already set in log bitmap?\n",
112                        index);
113                 spin_unlock(&loghandle->lgh_hdr_lock);
114                 LBUG(); /* should never happen */
115         }
116         spin_unlock(&loghandle->lgh_hdr_lock);
117
118         cathandle->lgh_last_idx = index;
119         llh->llh_tail.lrt_index = index;
120
121         CDEBUG(D_RPCTRACE,"new recovery log "LPX64":%x for index %u of catalog "
122                LPX64"\n", loghandle->lgh_id.lgl_oid, loghandle->lgh_id.lgl_ogen,
123                index, cathandle->lgh_id.lgl_oid);
124         /* build the record for this log in the catalog */
125         rec.lid_hdr.lrh_len = sizeof(rec);
126         rec.lid_hdr.lrh_index = index;
127         rec.lid_hdr.lrh_type = LLOG_LOGID_MAGIC;
128         rec.lid_id = loghandle->lgh_id;
129         rec.lid_tail.lrt_len = sizeof(rec);
130         rec.lid_tail.lrt_index = index;
131
132         /* update the catalog: header and record */
133         rc = llog_write_rec(env, cathandle, &rec.lid_hdr,
134                             &loghandle->u.phd.phd_cookie, 1, NULL, index, th);
135         if (rc < 0)
136                 GOTO(out_destroy, rc);
137
138         loghandle->lgh_hdr->llh_cat_idx = index;
139         RETURN(0);
140 out_destroy:
141         llog_destroy(env, loghandle);
142         RETURN(rc);
143 }
144
145 /* Open an existent log handle and add it to the open list.
146  * This log handle will be closed when all of the records in it are removed.
147  *
148  * Assumes caller has already pushed us into the kernel context and is locking.
149  * We return a lock on the handle to ensure nobody yanks it from us.
150  *
151  * This takes extra reference on llog_handle via llog_handle_get() and require
152  * this reference to be put by caller using llog_handle_put()
153  */
154 int llog_cat_id2handle(const struct lu_env *env, struct llog_handle *cathandle,
155                        struct llog_handle **res, struct llog_logid *logid)
156 {
157         struct llog_handle      *loghandle;
158         int                      rc = 0;
159
160         ENTRY;
161
162         if (cathandle == NULL)
163                 RETURN(-EBADF);
164
165         down_write(&cathandle->lgh_lock);
166         cfs_list_for_each_entry(loghandle, &cathandle->u.chd.chd_head,
167                                 u.phd.phd_entry) {
168                 struct llog_logid *cgl = &loghandle->lgh_id;
169
170                 if (cgl->lgl_oid == logid->lgl_oid) {
171                         if (cgl->lgl_ogen != logid->lgl_ogen) {
172                                 CERROR("%s: log "LPX64" generation %x != %x\n",
173                                        loghandle->lgh_ctxt->loc_obd->obd_name,
174                                        logid->lgl_oid, cgl->lgl_ogen,
175                                        logid->lgl_ogen);
176                                 continue;
177                         }
178                         loghandle->u.phd.phd_cat_handle = cathandle;
179                         up_write(&cathandle->lgh_lock);
180                         GOTO(out, rc = 0);
181                 }
182         }
183         up_write(&cathandle->lgh_lock);
184
185         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle, logid, NULL,
186                        LLOG_OPEN_EXISTS);
187         if (rc < 0) {
188                 CERROR("%s: error opening log id "LPX64":%x: rc = %d\n",
189                        cathandle->lgh_ctxt->loc_obd->obd_name,
190                        logid->lgl_oid, logid->lgl_ogen, rc);
191                 RETURN(rc);
192         }
193
194         rc = llog_init_handle(env, loghandle, LLOG_F_IS_PLAIN, NULL);
195         if (rc < 0) {
196                 llog_close(env, loghandle);
197                 loghandle = NULL;
198                 RETURN(rc);
199         }
200
201         down_write(&cathandle->lgh_lock);
202         cfs_list_add(&loghandle->u.phd.phd_entry, &cathandle->u.chd.chd_head);
203         up_write(&cathandle->lgh_lock);
204
205         loghandle->u.phd.phd_cat_handle = cathandle;
206         loghandle->u.phd.phd_cookie.lgc_lgl = cathandle->lgh_id;
207         loghandle->u.phd.phd_cookie.lgc_index =
208                                 loghandle->lgh_hdr->llh_cat_idx;
209         EXIT;
210 out:
211         llog_handle_get(loghandle);
212         *res = loghandle;
213         return 0;
214 }
215
216 int llog_cat_close(const struct lu_env *env, struct llog_handle *cathandle)
217 {
218         struct llog_handle      *loghandle, *n;
219         int                      rc;
220
221         ENTRY;
222
223         cfs_list_for_each_entry_safe(loghandle, n, &cathandle->u.chd.chd_head,
224                                      u.phd.phd_entry) {
225                 struct llog_log_hdr     *llh = loghandle->lgh_hdr;
226                 int                      index;
227
228                 /* unlink open-not-created llogs */
229                 cfs_list_del_init(&loghandle->u.phd.phd_entry);
230                 llh = loghandle->lgh_hdr;
231                 if (loghandle->lgh_obj != NULL && llh != NULL &&
232                     (llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
233                     (llh->llh_count == 1)) {
234                         rc = llog_destroy(env, loghandle);
235                         if (rc)
236                                 CERROR("%s: failure destroying log during "
237                                        "cleanup: rc = %d\n",
238                                        loghandle->lgh_ctxt->loc_obd->obd_name,
239                                        rc);
240
241                         index = loghandle->u.phd.phd_cookie.lgc_index;
242                         llog_cat_cleanup(env, cathandle, NULL, index);
243                 }
244                 llog_close(env, loghandle);
245         }
246         /* if handle was stored in ctxt, remove it too */
247         if (cathandle->lgh_ctxt->loc_handle == cathandle)
248                 cathandle->lgh_ctxt->loc_handle = NULL;
249         rc = llog_close(env, cathandle);
250         RETURN(rc);
251 }
252 EXPORT_SYMBOL(llog_cat_close);
253
254 /**
255  * lockdep markers for nested struct llog_handle::lgh_lock locking.
256  */
257 enum {
258         LLOGH_CAT,
259         LLOGH_LOG
260 };
261
262 /** Return the currently active log handle.  If the current log handle doesn't
263  * have enough space left for the current record, start a new one.
264  *
265  * If reclen is 0, we only want to know what the currently active log is,
266  * otherwise we get a lock on this log so nobody can steal our space.
267  *
268  * Assumes caller has already pushed us into the kernel context and is locking.
269  *
270  * NOTE: loghandle is write-locked upon successful return
271  */
272 static struct llog_handle *llog_cat_current_log(struct llog_handle *cathandle,
273                                                 struct thandle *th)
274 {
275         struct llog_handle *loghandle = NULL;
276         ENTRY;
277
278         down_read_nested(&cathandle->lgh_lock, LLOGH_CAT);
279         loghandle = cathandle->u.chd.chd_current_log;
280         if (loghandle) {
281                 struct llog_log_hdr *llh;
282
283                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
284                 llh = loghandle->lgh_hdr;
285                 if (llh == NULL ||
286                     loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
287                         up_read(&cathandle->lgh_lock);
288                         RETURN(loghandle);
289                 } else {
290                         up_write(&loghandle->lgh_lock);
291                 }
292         }
293         up_read(&cathandle->lgh_lock);
294
295         /* time to use next log */
296
297         /* first, we have to make sure the state hasn't changed */
298         down_write_nested(&cathandle->lgh_lock, LLOGH_CAT);
299         loghandle = cathandle->u.chd.chd_current_log;
300         if (loghandle) {
301                 struct llog_log_hdr *llh;
302
303                 down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
304                 llh = loghandle->lgh_hdr;
305                 LASSERT(llh);
306                 if (loghandle->lgh_last_idx < LLOG_BITMAP_SIZE(llh) - 1) {
307                         up_write(&cathandle->lgh_lock);
308                         RETURN(loghandle);
309                 } else {
310                         up_write(&loghandle->lgh_lock);
311                 }
312         }
313
314         CDEBUG(D_INODE, "use next log\n");
315
316         loghandle = cathandle->u.chd.chd_next_log;
317         cathandle->u.chd.chd_current_log = loghandle;
318         cathandle->u.chd.chd_next_log = NULL;
319         down_write_nested(&loghandle->lgh_lock, LLOGH_LOG);
320         up_write(&cathandle->lgh_lock);
321         LASSERT(loghandle);
322         RETURN(loghandle);
323 }
324
325 /* Add a single record to the recovery log(s) using a catalog
326  * Returns as llog_write_record
327  *
328  * Assumes caller has already pushed us into the kernel context.
329  */
330 int llog_cat_add_rec(const struct lu_env *env, struct llog_handle *cathandle,
331                      struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
332                      void *buf, struct thandle *th)
333 {
334         struct llog_handle *loghandle;
335         int rc;
336         ENTRY;
337
338         LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
339         loghandle = llog_cat_current_log(cathandle, th);
340         LASSERT(!IS_ERR(loghandle));
341
342         /* loghandle is already locked by llog_cat_current_log() for us */
343         if (!llog_exist(loghandle)) {
344                 rc = llog_cat_new_log(env, cathandle, loghandle, th);
345                 if (rc < 0) {
346                         up_write(&loghandle->lgh_lock);
347                         RETURN(rc);
348                 }
349         }
350         /* now let's try to add the record */
351         rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf, -1, th);
352         if (rc < 0)
353                 CDEBUG_LIMIT(rc == -ENOSPC ? D_HA : D_ERROR,
354                              "llog_write_rec %d: lh=%p\n", rc, loghandle);
355         up_write(&loghandle->lgh_lock);
356         if (rc == -ENOSPC) {
357                 /* try to use next log */
358                 loghandle = llog_cat_current_log(cathandle, th);
359                 LASSERT(!IS_ERR(loghandle));
360                 /* new llog can be created concurrently */
361                 if (!llog_exist(loghandle)) {
362                         rc = llog_cat_new_log(env, cathandle, loghandle, th);
363                         if (rc < 0) {
364                                 up_write(&loghandle->lgh_lock);
365                                 RETURN(rc);
366                         }
367                 }
368                 /* now let's try to add the record */
369                 rc = llog_write_rec(env, loghandle, rec, reccookie, 1, buf,
370                                     -1, th);
371                 if (rc < 0)
372                         CERROR("llog_write_rec %d: lh=%p\n", rc, loghandle);
373                 up_write(&loghandle->lgh_lock);
374         }
375
376         RETURN(rc);
377 }
378 EXPORT_SYMBOL(llog_cat_add_rec);
379
380 int llog_cat_declare_add_rec(const struct lu_env *env,
381                              struct llog_handle *cathandle,
382                              struct llog_rec_hdr *rec, struct thandle *th)
383 {
384         struct llog_handle      *loghandle, *next;
385         int                      rc = 0;
386
387         ENTRY;
388
389         if (cathandle->u.chd.chd_current_log == NULL) {
390                 /* declare new plain llog */
391                 down_write(&cathandle->lgh_lock);
392                 if (cathandle->u.chd.chd_current_log == NULL) {
393                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
394                                        NULL, NULL, LLOG_OPEN_NEW);
395                         if (rc == 0) {
396                                 cathandle->u.chd.chd_current_log = loghandle;
397                                 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
398                                                   &cathandle->u.chd.chd_head);
399                         }
400                 }
401                 up_write(&cathandle->lgh_lock);
402         } else if (cathandle->u.chd.chd_next_log == NULL) {
403                 /* declare next plain llog */
404                 down_write(&cathandle->lgh_lock);
405                 if (cathandle->u.chd.chd_next_log == NULL) {
406                         rc = llog_open(env, cathandle->lgh_ctxt, &loghandle,
407                                        NULL, NULL, LLOG_OPEN_NEW);
408                         if (rc == 0) {
409                                 cathandle->u.chd.chd_next_log = loghandle;
410                                 cfs_list_add_tail(&loghandle->u.phd.phd_entry,
411                                                   &cathandle->u.chd.chd_head);
412                         }
413                 }
414                 up_write(&cathandle->lgh_lock);
415         }
416         if (rc)
417                 GOTO(out, rc);
418
419         if (!llog_exist(cathandle->u.chd.chd_current_log)) {
420                 rc = llog_declare_create(env, cathandle->u.chd.chd_current_log,
421                                          th);
422                 if (rc)
423                         GOTO(out, rc);
424                 llog_declare_write_rec(env, cathandle, NULL, -1, th);
425         }
426         /* declare records in the llogs */
427         rc = llog_declare_write_rec(env, cathandle->u.chd.chd_current_log,
428                                     rec, -1, th);
429         if (rc)
430                 GOTO(out, rc);
431
432         next = cathandle->u.chd.chd_next_log;
433         if (next) {
434                 if (!llog_exist(next)) {
435                         rc = llog_declare_create(env, next, th);
436                         llog_declare_write_rec(env, cathandle, NULL, -1, th);
437                 }
438                 llog_declare_write_rec(env, next, rec, -1, th);
439         }
440 out:
441         RETURN(rc);
442 }
443 EXPORT_SYMBOL(llog_cat_declare_add_rec);
444
445 int llog_cat_add(const struct lu_env *env, struct llog_handle *cathandle,
446                  struct llog_rec_hdr *rec, struct llog_cookie *reccookie,
447                  void *buf)
448 {
449         struct llog_ctxt        *ctxt;
450         struct dt_device        *dt;
451         struct thandle          *th = NULL;
452         int                      rc;
453
454         ctxt = cathandle->lgh_ctxt;
455         LASSERT(ctxt);
456         LASSERT(ctxt->loc_exp);
457
458         if (cathandle->lgh_obj != NULL) {
459                 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
460                 LASSERT(dt);
461
462                 th = dt_trans_create(env, dt);
463                 if (IS_ERR(th))
464                         RETURN(PTR_ERR(th));
465
466                 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
467                 if (rc)
468                         GOTO(out_trans, rc);
469
470                 rc = dt_trans_start_local(env, dt, th);
471                 if (rc)
472                         GOTO(out_trans, rc);
473                 rc = llog_cat_add_rec(env, cathandle, rec, reccookie, buf, th);
474 out_trans:
475                 dt_trans_stop(env, dt, th);
476         } else { /* lvfs compat code */
477                 LASSERT(cathandle->lgh_file != NULL);
478                 rc = llog_cat_declare_add_rec(env, cathandle, rec, th);
479                 if (rc == 0)
480                         rc = llog_cat_add_rec(env, cathandle, rec, reccookie,
481                                               buf, th);
482         }
483         RETURN(rc);
484 }
485 EXPORT_SYMBOL(llog_cat_add);
486
487 /* For each cookie in the cookie array, we clear the log in-use bit and either:
488  * - the log is empty, so mark it free in the catalog header and delete it
489  * - the log is not empty, just write out the log header
490  *
491  * The cookies may be in different log files, so we need to get new logs
492  * each time.
493  *
494  * Assumes caller has already pushed us into the kernel context.
495  */
496 int llog_cat_cancel_records(const struct lu_env *env,
497                             struct llog_handle *cathandle, int count,
498                             struct llog_cookie *cookies)
499 {
500         int i, index, rc = 0, failed = 0;
501
502         ENTRY;
503
504         for (i = 0; i < count; i++, cookies++) {
505                 struct llog_handle      *loghandle;
506                 struct llog_logid       *lgl = &cookies->lgc_lgl;
507                 int                      lrc;
508
509                 rc = llog_cat_id2handle(env, cathandle, &loghandle, lgl);
510                 if (rc) {
511                         CERROR("%s: cannot find handle for llog "LPX64": %d\n",
512                                cathandle->lgh_ctxt->loc_obd->obd_name,
513                                lgl->lgl_oid, rc);
514                         failed++;
515                         continue;
516                 }
517
518                 lrc = llog_cancel_rec(env, loghandle, cookies->lgc_index);
519                 if (lrc == 1) {          /* log has been destroyed */
520                         index = loghandle->u.phd.phd_cookie.lgc_index;
521                         rc = llog_cat_cleanup(env, cathandle, loghandle,
522                                               index);
523                 } else if (lrc == -ENOENT) {
524                         if (rc == 0) /* ENOENT shouldn't rewrite any error */
525                                 rc = lrc;
526                 } else if (lrc < 0) {
527                         failed++;
528                         rc = lrc;
529                 }
530                 llog_handle_put(loghandle);
531         }
532         if (rc)
533                 CERROR("%s: fail to cancel %d of %d llog-records: rc = %d\n",
534                        cathandle->lgh_ctxt->loc_obd->obd_name, failed, count,
535                        rc);
536
537         RETURN(rc);
538 }
539 EXPORT_SYMBOL(llog_cat_cancel_records);
540
541 int llog_cat_process_cb(const struct lu_env *env, struct llog_handle *cat_llh,
542                         struct llog_rec_hdr *rec, void *data)
543 {
544         struct llog_process_data *d = data;
545         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
546         struct llog_handle *llh;
547         int rc;
548
549         ENTRY;
550         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
551                 CERROR("invalid record in catalog\n");
552                 RETURN(-EINVAL);
553         }
554         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
555                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
556                rec->lrh_index, cat_llh->lgh_id.lgl_oid);
557
558         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
559         if (rc) {
560                 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
561                        cat_llh->lgh_ctxt->loc_obd->obd_name,
562                        lir->lid_id.lgl_oid, rc);
563                 RETURN(rc);
564         }
565
566         if (rec->lrh_index < d->lpd_startcat)
567                 /* Skip processing of the logs until startcat */
568                 RETURN(0);
569
570         if (d->lpd_startidx > 0) {
571                 struct llog_process_cat_data cd;
572
573                 cd.lpcd_first_idx = d->lpd_startidx;
574                 cd.lpcd_last_idx = 0;
575                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
576                                           &cd, false);
577                 /* Continue processing the next log from idx 0 */
578                 d->lpd_startidx = 0;
579         } else {
580                 rc = llog_process_or_fork(env, llh, d->lpd_cb, d->lpd_data,
581                                           NULL, false);
582         }
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 "LPX64" crosses index zero\n",
608                       cat_llh->lgh_id.lgl_oid);
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 #ifdef __KERNEL__
639 int llog_cat_process_thread(void *data)
640 {
641         struct llog_process_cat_args *args = data;
642         struct llog_ctxt *ctxt = args->lpca_ctxt;
643         struct llog_handle *llh = NULL;
644         llog_cb_t cb = args->lpca_cb;
645         struct llog_thread_info *lgi;
646         struct lu_env            env;
647         int rc;
648         ENTRY;
649
650         cfs_daemonize_ctxt("ll_log_process");
651
652         rc = lu_env_init(&env, LCT_LOCAL);
653         if (rc)
654                 GOTO(out, rc);
655         lgi = llog_info(&env);
656         LASSERT(lgi);
657
658         lgi->lgi_logid = *(struct llog_logid *)(args->lpca_arg);
659         rc = llog_open(&env, ctxt, &llh, &lgi->lgi_logid, NULL,
660                        LLOG_OPEN_EXISTS);
661         if (rc) {
662                 CERROR("%s: cannot open llog "LPX64":%x: rc = %d\n",
663                        ctxt->loc_obd->obd_name, lgi->lgi_logid.lgl_oid,
664                        lgi->lgi_logid.lgl_ogen, rc);
665                 GOTO(out_env, rc);
666         }
667         rc = llog_init_handle(&env, llh, LLOG_F_IS_CAT, NULL);
668         if (rc) {
669                 CERROR("%s: llog_init_handle failed: rc = %d\n",
670                        llh->lgh_ctxt->loc_obd->obd_name, rc);
671                 GOTO(release_llh, rc);
672         }
673
674         if (cb) {
675                 rc = llog_cat_process(&env, llh, cb, NULL, 0, 0);
676                 if (rc != LLOG_PROC_BREAK && rc != 0)
677                         CERROR("%s: llog_cat_process() failed: rc = %d\n",
678                                llh->lgh_ctxt->loc_obd->obd_name, rc);
679                 cb(&env, llh, NULL, NULL);
680         } else {
681                 CWARN("No callback function for recovery\n");
682         }
683
684         /*
685          * Make sure that all cached data is sent.
686          */
687         llog_sync(ctxt, NULL, 0);
688         GOTO(release_llh, rc);
689 release_llh:
690         rc = llog_cat_close(&env, llh);
691         if (rc)
692                 CERROR("%s: llog_cat_close() failed: rc = %d\n",
693                        llh->lgh_ctxt->loc_obd->obd_name, rc);
694 out_env:
695         lu_env_fini(&env);
696 out:
697         llog_ctxt_put(ctxt);
698         OBD_FREE_PTR(args);
699         return rc;
700 }
701 EXPORT_SYMBOL(llog_cat_process_thread);
702 #endif
703
704 static int llog_cat_reverse_process_cb(const struct lu_env *env,
705                                        struct llog_handle *cat_llh,
706                                        struct llog_rec_hdr *rec, void *data)
707 {
708         struct llog_process_data *d = data;
709         struct llog_logid_rec *lir = (struct llog_logid_rec *)rec;
710         struct llog_handle *llh;
711         int rc;
712
713         if (le32_to_cpu(rec->lrh_type) != LLOG_LOGID_MAGIC) {
714                 CERROR("invalid record in catalog\n");
715                 RETURN(-EINVAL);
716         }
717         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
718                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
719                le32_to_cpu(rec->lrh_index), cat_llh->lgh_id.lgl_oid);
720
721         rc = llog_cat_id2handle(env, cat_llh, &llh, &lir->lid_id);
722         if (rc) {
723                 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
724                        cat_llh->lgh_ctxt->loc_obd->obd_name,
725                        lir->lid_id.lgl_oid, rc);
726                 RETURN(rc);
727         }
728
729         rc = llog_reverse_process(env, llh, d->lpd_cb, d->lpd_data, NULL);
730         llog_handle_put(llh);
731         RETURN(rc);
732 }
733
734 int llog_cat_reverse_process(const struct lu_env *env,
735                              struct llog_handle *cat_llh,
736                              llog_cb_t cb, void *data)
737 {
738         struct llog_process_data d;
739         struct llog_process_cat_data cd;
740         struct llog_log_hdr *llh = cat_llh->lgh_hdr;
741         int rc;
742         ENTRY;
743
744         LASSERT(llh->llh_flags & LLOG_F_IS_CAT);
745         d.lpd_data = data;
746         d.lpd_cb = cb;
747
748         if (llh->llh_cat_idx > cat_llh->lgh_last_idx) {
749                 CWARN("catalog "LPX64" crosses index zero\n",
750                       cat_llh->lgh_id.lgl_oid);
751
752                 cd.lpcd_first_idx = 0;
753                 cd.lpcd_last_idx = cat_llh->lgh_last_idx;
754                 rc = llog_reverse_process(env, cat_llh,
755                                           llog_cat_reverse_process_cb,
756                                           &d, &cd);
757                 if (rc != 0)
758                         RETURN(rc);
759
760                 cd.lpcd_first_idx = le32_to_cpu(llh->llh_cat_idx);
761                 cd.lpcd_last_idx = 0;
762                 rc = llog_reverse_process(env, cat_llh,
763                                           llog_cat_reverse_process_cb,
764                                           &d, &cd);
765         } else {
766                 rc = llog_reverse_process(env, cat_llh,
767                                           llog_cat_reverse_process_cb,
768                                           &d, NULL);
769         }
770
771         RETURN(rc);
772 }
773 EXPORT_SYMBOL(llog_cat_reverse_process);
774
775 int llog_cat_set_first_idx(struct llog_handle *cathandle, int index)
776 {
777         struct llog_log_hdr *llh = cathandle->lgh_hdr;
778         int i, bitmap_size, idx;
779         ENTRY;
780
781         bitmap_size = LLOG_BITMAP_SIZE(llh);
782         if (llh->llh_cat_idx == (index - 1)) {
783                 idx = llh->llh_cat_idx + 1;
784                 llh->llh_cat_idx = idx;
785                 if (idx == cathandle->lgh_last_idx)
786                         goto out;
787                 for (i = (index + 1) % bitmap_size;
788                      i != cathandle->lgh_last_idx;
789                      i = (i + 1) % bitmap_size) {
790                         if (!ext2_test_bit(i, llh->llh_bitmap)) {
791                                 idx = llh->llh_cat_idx + 1;
792                                 llh->llh_cat_idx = idx;
793                         } else if (i == 0) {
794                                 llh->llh_cat_idx = 0;
795                         } else {
796                                 break;
797                         }
798                 }
799 out:
800                 CDEBUG(D_RPCTRACE, "set catlog "LPX64" first idx %u\n",
801                        cathandle->lgh_id.lgl_oid, llh->llh_cat_idx);
802         }
803
804         RETURN(0);
805 }
806
807 /* Cleanup deleted plain llog traces from catalog */
808 int llog_cat_cleanup(const struct lu_env *env, struct llog_handle *cathandle,
809                      struct llog_handle *loghandle, int index)
810 {
811         int rc;
812
813         LASSERT(index);
814         if (loghandle != NULL) {
815                 /* remove destroyed llog from catalog list and
816                  * chd_current_log variable */
817                 down_write(&cathandle->lgh_lock);
818                 if (cathandle->u.chd.chd_current_log == loghandle)
819                         cathandle->u.chd.chd_current_log = NULL;
820                 cfs_list_del_init(&loghandle->u.phd.phd_entry);
821                 up_write(&cathandle->lgh_lock);
822                 LASSERT(index == loghandle->u.phd.phd_cookie.lgc_index);
823                 /* llog was opened and keep in a list, close it now */
824                 llog_close(env, loghandle);
825         }
826         /* remove plain llog entry from catalog by index */
827         llog_cat_set_first_idx(cathandle, index);
828         rc = llog_cancel_rec(env, cathandle, index);
829         if (rc == 0)
830                 CDEBUG(D_HA, "cancel plain log at index"
831                        " %u of catalog "LPX64"\n",
832                        index, cathandle->lgh_id.lgl_oid);
833         return rc;
834 }
835
836 int cat_cancel_cb(const struct lu_env *env, struct llog_handle *cathandle,
837                   struct llog_rec_hdr *rec, void *data)
838 {
839         struct llog_logid_rec   *lir = (struct llog_logid_rec *)rec;
840         struct llog_handle      *loghandle;
841         struct llog_log_hdr     *llh;
842         int                      rc;
843
844         ENTRY;
845
846         if (rec->lrh_type != LLOG_LOGID_MAGIC) {
847                 CERROR("invalid record in catalog\n");
848                 RETURN(-EINVAL);
849         }
850         CDEBUG(D_HA, "processing log "LPX64":%x at index %u of catalog "
851                LPX64"\n", lir->lid_id.lgl_oid, lir->lid_id.lgl_ogen,
852                rec->lrh_index, cathandle->lgh_id.lgl_oid);
853
854         rc = llog_cat_id2handle(env, cathandle, &loghandle, &lir->lid_id);
855         if (rc) {
856                 CERROR("%s: cannot find handle for llog "LPX64": %d\n",
857                        cathandle->lgh_ctxt->loc_obd->obd_name,
858                        lir->lid_id.lgl_oid, rc);
859                 if (rc == -ENOENT || rc == -ESTALE) {
860                         /* remove index from catalog */
861                         llog_cat_cleanup(env, cathandle, NULL, rec->lrh_index);
862                 }
863                 RETURN(rc);
864         }
865
866         llh = loghandle->lgh_hdr;
867         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
868             (llh->llh_count == 1)) {
869                 rc = llog_destroy(env, loghandle);
870                 if (rc)
871                         CERROR("%s: fail to destroy empty log: rc = %d\n",
872                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
873
874                 llog_cat_cleanup(env, cathandle, loghandle,
875                                  loghandle->u.phd.phd_cookie.lgc_index);
876         }
877         llog_handle_put(loghandle);
878
879         RETURN(rc);
880 }
881 EXPORT_SYMBOL(cat_cancel_cb);
882
883 /* helper to initialize catalog llog and process it to cancel */
884 int llog_cat_init_and_process(const struct lu_env *env,
885                               struct llog_handle *llh)
886 {
887         int rc;
888
889         rc = llog_init_handle(env, llh, LLOG_F_IS_CAT, NULL);
890         if (rc)
891                 RETURN(rc);
892
893         rc = llog_process_or_fork(env, llh, cat_cancel_cb, NULL, NULL, false);
894         if (rc)
895                 CERROR("%s: llog_process() with cat_cancel_cb failed: rc = "
896                        "%d\n", llh->lgh_ctxt->loc_obd->obd_name, rc);
897         RETURN(0);
898 }
899 EXPORT_SYMBOL(llog_cat_init_and_process);
900