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