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