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