Whamcloud - gitweb
334f752f12219135652c6000a8c4caf12fba2ca2
[fs/lustre-release.git] / lustre / obdclass / llog.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.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  * Invariants in implementation:
40  * - we do not share logs among different OST<->MDS connections, so that
41  *   if an OST or MDS fails it need only look at log(s) relevant to itself
42  *
43  * Author: Andreas Dilger <adilger@clusterfs.com>
44  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45  * Author: Mikhail Pershin <tappro@whamcloud.com>
46  */
47
48 #define DEBUG_SUBSYSTEM S_LOG
49
50 #include <linux/kthread.h>
51 #include <obd_class.h>
52 #include <lustre_log.h>
53 #include "llog_internal.h"
54
55 /*
56  * Allocate a new log or catalog handle
57  * Used inside llog_open().
58  */
59 static struct llog_handle *llog_alloc_handle(void)
60 {
61         struct llog_handle *loghandle;
62
63         OBD_ALLOC_PTR(loghandle);
64         if (loghandle == NULL)
65                 return NULL;
66
67         init_rwsem(&loghandle->lgh_lock);
68         mutex_init(&loghandle->lgh_hdr_mutex);
69         INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
70         atomic_set(&loghandle->lgh_refcount, 1);
71
72         return loghandle;
73 }
74
75 /*
76  * Free llog handle and header data if exists. Used in llog_close() only
77  */
78 static void llog_free_handle(struct llog_handle *loghandle)
79 {
80         LASSERT(loghandle != NULL);
81
82         /* failed llog_init_handle */
83         if (loghandle->lgh_hdr == NULL)
84                 goto out;
85
86         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
87                 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
88         else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
89                 LASSERT(list_empty(&loghandle->u.chd.chd_head));
90         OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
91 out:
92         OBD_FREE_PTR(loghandle);
93 }
94
95 void llog_handle_get(struct llog_handle *loghandle)
96 {
97         atomic_inc(&loghandle->lgh_refcount);
98 }
99
100 void llog_handle_put(struct llog_handle *loghandle)
101 {
102         LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
103         if (atomic_dec_and_test(&loghandle->lgh_refcount))
104                 llog_free_handle(loghandle);
105 }
106
107 static int llog_cancel_rec_internal(const struct lu_env *env,
108                                     struct llog_handle *loghandle, int index)
109 {
110         struct dt_device        *dt;
111         struct llog_log_hdr     *llh = loghandle->lgh_hdr;
112         struct thandle          *th;
113         int                      rc;
114
115         ENTRY;
116
117         LASSERT(loghandle);
118         LASSERT(loghandle->lgh_ctxt);
119         LASSERT(loghandle->lgh_obj != NULL);
120
121         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
122
123         th = dt_trans_create(env, dt);
124         if (IS_ERR(th))
125                 RETURN(PTR_ERR(th));
126
127         rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, index, th);
128         if (rc < 0)
129                 GOTO(out_trans, rc);
130
131         th->th_wait_submit = 1;
132         rc = dt_trans_start_local(env, dt, th);
133         if (rc < 0)
134                 GOTO(out_trans, rc);
135
136         down_write(&loghandle->lgh_lock);
137         /* clear bitmap */
138         mutex_lock(&loghandle->lgh_hdr_mutex);
139         if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
140                 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
141                 GOTO(out_unlock, rc);
142         }
143         /* update header */
144         rc = llog_write_rec(env, loghandle, &llh->llh_hdr, NULL,
145                             LLOG_HEADER_IDX, th);
146         if (rc == 0)
147                 loghandle->lgh_hdr->llh_count--;
148         else
149                 ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
150 out_unlock:
151         mutex_unlock(&loghandle->lgh_hdr_mutex);
152         up_write(&loghandle->lgh_lock);
153 out_trans:
154         dt_trans_stop(env, dt, th);
155         RETURN(rc);
156 }
157
158 /* returns negative on error; 0 if success; 1 if success & log destroyed */
159 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
160                     int index)
161 {
162         struct llog_log_hdr *llh = loghandle->lgh_hdr;
163         int rc = 0;
164         ENTRY;
165
166         CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n",
167                index, POSTID(&loghandle->lgh_id.lgl_oi));
168
169         if (index == 0) {
170                 CERROR("Can't cancel index 0 which is header\n");
171                 RETURN(-EINVAL);
172         }
173
174         rc = llog_cancel_rec_internal(env, loghandle, index);
175         if (rc < 0) {
176                 CERROR("%s: fail to write header for llog #"DOSTID
177                        "#%08x: rc = %d\n",
178                        loghandle->lgh_ctxt->loc_obd->obd_name,
179                        POSTID(&loghandle->lgh_id.lgl_oi),
180                        loghandle->lgh_id.lgl_ogen, rc);
181                 RETURN(rc);
182         }
183
184         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
185             (llh->llh_count == 1) &&
186             (loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1)) {
187                 rc = llog_destroy(env, loghandle);
188                 if (rc < 0) {
189                         /* Sigh, can not destroy the final plain llog, but
190                          * the bitmap has been clearly, so the record can not
191                          * be accessed anymore, let's return 0 for now, and
192                          * the orphan will be handled by LFSCK. */
193                         CERROR("%s: can't destroy empty llog #"DOSTID
194                                "#%08x: rc = %d\n",
195                                loghandle->lgh_ctxt->loc_obd->obd_name,
196                                POSTID(&loghandle->lgh_id.lgl_oi),
197                                loghandle->lgh_id.lgl_ogen, rc);
198                         RETURN(0);
199                 }
200                 RETURN(LLOG_DEL_PLAIN);
201         }
202
203         RETURN(0);
204 }
205
206 static int llog_read_header(const struct lu_env *env,
207                             struct llog_handle *handle,
208                             struct obd_uuid *uuid)
209 {
210         struct llog_operations *lop;
211         int rc;
212
213         rc = llog_handle2ops(handle, &lop);
214         if (rc)
215                 RETURN(rc);
216
217         if (lop->lop_read_header == NULL)
218                 RETURN(-EOPNOTSUPP);
219
220         rc = lop->lop_read_header(env, handle);
221         if (rc == LLOG_EEMPTY) {
222                 struct llog_log_hdr *llh = handle->lgh_hdr;
223
224                 /* lrh_len should be initialized in llog_init_handle */
225                 handle->lgh_last_idx = 0; /* header is record with index 0 */
226                 llh->llh_count = 1;         /* for the header record */
227                 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
228                 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
229                                                 LLOG_MIN_CHUNK_SIZE);
230                 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
231                 llh->llh_hdr.lrh_index = 0;
232                 llh->llh_timestamp = cfs_time_current_sec();
233                 if (uuid)
234                         memcpy(&llh->llh_tgtuuid, uuid,
235                                sizeof(llh->llh_tgtuuid));
236                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
237                 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
238                 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
239                 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
240                 rc = 0;
241         }
242         return rc;
243 }
244
245 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
246                      int flags, struct obd_uuid *uuid)
247 {
248         struct llog_log_hdr     *llh;
249         enum llog_flag           fmt = flags & LLOG_F_EXT_MASK;
250         int                      rc;
251         int                     chunk_size = handle->lgh_ctxt->loc_chunk_size;
252         ENTRY;
253
254         LASSERT(handle->lgh_hdr == NULL);
255
256         LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
257         OBD_ALLOC_LARGE(llh, chunk_size);
258         if (llh == NULL)
259                 RETURN(-ENOMEM);
260
261         handle->lgh_hdr = llh;
262         handle->lgh_hdr_size = chunk_size;
263         /* first assign flags to use llog_client_ops */
264         llh->llh_flags = flags;
265         rc = llog_read_header(env, handle, uuid);
266         if (rc == 0) {
267                 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
268                               flags & LLOG_F_IS_CAT) ||
269                              (llh->llh_flags & LLOG_F_IS_CAT &&
270                               flags & LLOG_F_IS_PLAIN))) {
271                         CERROR("%s: llog type is %s but initializing %s\n",
272                                handle->lgh_ctxt->loc_obd->obd_name,
273                                llh->llh_flags & LLOG_F_IS_CAT ?
274                                "catalog" : "plain",
275                                flags & LLOG_F_IS_CAT ? "catalog" : "plain");
276                         GOTO(out, rc = -EINVAL);
277                 } else if (llh->llh_flags &
278                            (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
279                         /*
280                          * it is possible to open llog without specifying llog
281                          * type so it is taken from llh_flags
282                          */
283                         flags = llh->llh_flags;
284                 } else {
285                         /* for some reason the llh_flags has no type set */
286                         CERROR("llog type is not specified!\n");
287                         GOTO(out, rc = -EINVAL);
288                 }
289                 if (unlikely(uuid &&
290                              !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
291                         CERROR("%s: llog uuid mismatch: %s/%s\n",
292                                handle->lgh_ctxt->loc_obd->obd_name,
293                                (char *)uuid->uuid,
294                                (char *)llh->llh_tgtuuid.uuid);
295                         GOTO(out, rc = -EEXIST);
296                 }
297         }
298         if (flags & LLOG_F_IS_CAT) {
299                 LASSERT(list_empty(&handle->u.chd.chd_head));
300                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
301                 llh->llh_size = sizeof(struct llog_logid_rec);
302         } else if (!(flags & LLOG_F_IS_PLAIN)) {
303                 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
304                        handle->lgh_ctxt->loc_obd->obd_name,
305                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
306                 rc = -EINVAL;
307         }
308         llh->llh_flags |= fmt;
309 out:
310         if (rc) {
311                 OBD_FREE_LARGE(llh, chunk_size);
312                 handle->lgh_hdr = NULL;
313         }
314         RETURN(rc);
315 }
316 EXPORT_SYMBOL(llog_init_handle);
317
318 static int llog_process_thread(void *arg)
319 {
320         struct llog_process_info        *lpi = arg;
321         struct llog_handle              *loghandle = lpi->lpi_loghandle;
322         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
323         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
324         char                            *buf;
325         size_t                           chunk_size;
326         __u64                            cur_offset;
327         int                              rc = 0, index = 1, last_index;
328         int                              saved_index = 0;
329         int                              last_called_index = 0;
330
331         ENTRY;
332
333         if (llh == NULL)
334                 RETURN(-EINVAL);
335
336         cur_offset = chunk_size = llh->llh_hdr.lrh_len;
337         /* expect chunk_size to be power of two */
338         LASSERT(is_power_of_2(chunk_size));
339
340         OBD_ALLOC_LARGE(buf, chunk_size);
341         if (buf == NULL) {
342                 lpi->lpi_rc = -ENOMEM;
343                 RETURN(0);
344         }
345
346         if (cd != NULL) {
347                 last_called_index = cd->lpcd_first_idx;
348                 index = cd->lpcd_first_idx + 1;
349         }
350         if (cd != NULL && cd->lpcd_last_idx)
351                 last_index = cd->lpcd_last_idx;
352         else
353                 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
354
355         while (rc == 0) {
356                 struct llog_rec_hdr *rec;
357                 off_t chunk_offset;
358                 unsigned int buf_offset = 0;
359                 bool partial_chunk;
360
361                 /* skip records not set in bitmap */
362                 while (index <= last_index &&
363                        !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
364                         ++index;
365
366                 /* There are no indices prior the last_index */
367                 if (index > last_index)
368                         break;
369
370                 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
371                        last_index);
372
373 repeat:
374                 /* get the buf with our target record; avoid old garbage */
375                 memset(buf, 0, chunk_size);
376                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
377                                      index, &cur_offset, buf, chunk_size);
378                 if (rc != 0)
379                         GOTO(out, rc);
380
381                 /* NB: after llog_next_block() call the cur_offset is the
382                  * offset of the next block after read one.
383                  * The absolute offset of the current chunk is calculated
384                  * from cur_offset value and stored in chunk_offset variable.
385                  */
386                 if (cur_offset % chunk_size != 0) {
387                         partial_chunk = true;
388                         chunk_offset = cur_offset & ~(chunk_size - 1);
389                 } else {
390                         partial_chunk = false;
391                         chunk_offset = cur_offset - chunk_size;
392                 }
393
394                 /* NB: when rec->lrh_len is accessed it is already swabbed
395                  * since it is used at the "end" of the loop and the rec
396                  * swabbing is done at the beginning of the loop. */
397                 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
398                      (char *)rec < buf + chunk_size;
399                      rec = llog_rec_hdr_next(rec)) {
400
401                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
402                                rec, rec->lrh_type);
403
404                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
405                                 lustre_swab_llog_rec(rec);
406
407                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
408                                rec->lrh_type, rec->lrh_index);
409
410                         /* for partial chunk the end of it is zeroed, check
411                          * for index 0 to distinguish it. */
412                         if (partial_chunk && rec->lrh_index == 0) {
413                                 /* concurrent llog_add() might add new records
414                                  * while llog_processing, check this is not
415                                  * the case and re-read the current chunk
416                                  * otherwise. */
417                                 if (index > loghandle->lgh_last_idx)
418                                         GOTO(out, rc = 0);
419                                 CDEBUG(D_OTHER, "Re-read last llog buffer for "
420                                        "new records, index %u, last %u\n",
421                                        index, loghandle->lgh_last_idx);
422                                 /* save offset inside buffer for the re-read */
423                                 buf_offset = (char *)rec - (char *)buf;
424                                 cur_offset = chunk_offset;
425                                 goto repeat;
426                         }
427
428                         if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
429                                 CWARN("invalid length %d in llog record for "
430                                       "index %d/%d\n", rec->lrh_len,
431                                       rec->lrh_index, index);
432                                 GOTO(out, rc = -EINVAL);
433                         }
434
435                         if (rec->lrh_index < index) {
436                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
437                                        rec->lrh_index);
438                                 continue;
439                         }
440
441                         if (rec->lrh_index != index) {
442                                 CERROR("%s: Invalid record: index %u but "
443                                        "expected %u\n",
444                                        loghandle->lgh_ctxt->loc_obd->obd_name,
445                                        rec->lrh_index, index);
446                                 GOTO(out, rc = -ERANGE);
447                         }
448
449                         CDEBUG(D_OTHER,
450                                "lrh_index: %d lrh_len: %d (%d remains)\n",
451                                rec->lrh_index, rec->lrh_len,
452                                (int)(buf + chunk_size - (char *)rec));
453
454                         loghandle->lgh_cur_idx = rec->lrh_index;
455                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
456                                                     chunk_offset;
457
458                         /* if set, process the callback on this record */
459                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
460                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
461                                                  lpi->lpi_cbdata);
462                                 last_called_index = index;
463                                 if (rc == LLOG_PROC_BREAK) {
464                                         GOTO(out, rc);
465                                 } else if (rc == LLOG_DEL_RECORD) {
466                                         rc = llog_cancel_rec(lpi->lpi_env,
467                                                              loghandle,
468                                                              rec->lrh_index);
469                                 }
470                                 if (rc)
471                                         GOTO(out, rc);
472                         }
473                         /* exit if the last index is reached */
474                         if (index >= last_index)
475                                 GOTO(out, rc = 0);
476                         ++index;
477                 }
478         }
479
480 out:
481         if (cd != NULL)
482                 cd->lpcd_last_idx = last_called_index;
483
484         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
485                 /* something bad happened to the processing of a local
486                  * llog file, probably I/O error or the log got corrupted..
487                  * to be able to finally release the log we discard any
488                  * remaining bits in the header */
489                 CERROR("Local llog found corrupted\n");
490                 while (index <= last_index) {
491                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
492                                 llog_cancel_rec(lpi->lpi_env, loghandle, index);
493                         index++;
494                 }
495                 rc = 0;
496         }
497
498         OBD_FREE_LARGE(buf, chunk_size);
499         lpi->lpi_rc = rc;
500         return 0;
501 }
502
503 static int llog_process_thread_daemonize(void *arg)
504 {
505         struct llog_process_info        *lpi = arg;
506         struct lu_env                    env;
507         int                              rc;
508
509         unshare_fs_struct();
510
511         /* client env has no keys, tags is just 0 */
512         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
513         if (rc)
514                 goto out;
515         lpi->lpi_env = &env;
516
517         rc = llog_process_thread(arg);
518
519         lu_env_fini(&env);
520 out:
521         complete(&lpi->lpi_completion);
522         return rc;
523 }
524
525 int llog_process_or_fork(const struct lu_env *env,
526                          struct llog_handle *loghandle,
527                          llog_cb_t cb, void *data, void *catdata, bool fork)
528 {
529         struct llog_process_info *lpi;
530         int                      rc;
531
532         ENTRY;
533
534         OBD_ALLOC_PTR(lpi);
535         if (lpi == NULL) {
536                 CERROR("cannot alloc pointer\n");
537                 RETURN(-ENOMEM);
538         }
539         lpi->lpi_loghandle = loghandle;
540         lpi->lpi_cb        = cb;
541         lpi->lpi_cbdata    = data;
542         lpi->lpi_catdata   = catdata;
543
544         if (fork) {
545                 struct task_struct *task;
546
547                 /* The new thread can't use parent env,
548                  * init the new one in llog_process_thread_daemonize. */
549                 lpi->lpi_env = NULL;
550                 init_completion(&lpi->lpi_completion);
551                 task = kthread_run(llog_process_thread_daemonize, lpi,
552                                    "llog_process_thread");
553                 if (IS_ERR(task)) {
554                         rc = PTR_ERR(task);
555                         CERROR("%s: cannot start thread: rc = %d\n",
556                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
557                         GOTO(out_lpi, rc);
558                 }
559                 wait_for_completion(&lpi->lpi_completion);
560         } else {
561                 lpi->lpi_env = env;
562                 llog_process_thread(lpi);
563         }
564         rc = lpi->lpi_rc;
565
566 out_lpi:
567         OBD_FREE_PTR(lpi);
568         RETURN(rc);
569 }
570 EXPORT_SYMBOL(llog_process_or_fork);
571
572 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
573                  llog_cb_t cb, void *data, void *catdata)
574 {
575         int rc;
576         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
577         return rc == LLOG_DEL_PLAIN ? 0 : rc;
578 }
579 EXPORT_SYMBOL(llog_process);
580
581 int llog_reverse_process(const struct lu_env *env,
582                          struct llog_handle *loghandle, llog_cb_t cb,
583                          void *data, void *catdata)
584 {
585         struct llog_log_hdr *llh = loghandle->lgh_hdr;
586         struct llog_process_cat_data *cd = catdata;
587         void *buf;
588         int rc = 0, first_index = 1, index, idx;
589         __u32   chunk_size = llh->llh_hdr.lrh_len;
590         ENTRY;
591
592         OBD_ALLOC_LARGE(buf, chunk_size);
593         if (buf == NULL)
594                 RETURN(-ENOMEM);
595
596         if (cd != NULL)
597                 first_index = cd->lpcd_first_idx + 1;
598         if (cd != NULL && cd->lpcd_last_idx)
599                 index = cd->lpcd_last_idx;
600         else
601                 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
602
603         while (rc == 0) {
604                 struct llog_rec_hdr *rec;
605                 struct llog_rec_tail *tail;
606
607                 /* skip records not set in bitmap */
608                 while (index >= first_index &&
609                        !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
610                         --index;
611
612                 LASSERT(index >= first_index - 1);
613                 if (index == first_index - 1)
614                         break;
615
616                 /* get the buf with our target record; avoid old garbage */
617                 memset(buf, 0, chunk_size);
618                 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
619                 if (rc)
620                         GOTO(out, rc);
621
622                 rec = buf;
623                 idx = rec->lrh_index;
624                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
625                 while (idx < index) {
626                         rec = (void *)rec + rec->lrh_len;
627                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
628                                 lustre_swab_llog_rec(rec);
629                         idx ++;
630                 }
631                 LASSERT(idx == index);
632                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
633
634                 /* process records in buffer, starting where we found one */
635                 while ((void *)tail > buf) {
636                         if (tail->lrt_index == 0)
637                                 GOTO(out, rc = 0); /* no more records */
638
639                         /* if set, process the callback on this record */
640                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
641                                 rec = (void *)tail - tail->lrt_len +
642                                       sizeof(*tail);
643
644                                 rc = cb(env, loghandle, rec, data);
645                                 if (rc == LLOG_PROC_BREAK) {
646                                         GOTO(out, rc);
647                                 } else if (rc == LLOG_DEL_RECORD) {
648                                         rc = llog_cancel_rec(env, loghandle,
649                                                              tail->lrt_index);
650                                 }
651                                 if (rc)
652                                         GOTO(out, rc);
653                         }
654
655                         /* previous record, still in buffer? */
656                         --index;
657                         if (index < first_index)
658                                 GOTO(out, rc = 0);
659                         tail = (void *)tail - tail->lrt_len;
660                 }
661         }
662
663 out:
664         if (buf != NULL)
665                 OBD_FREE_LARGE(buf, chunk_size);
666         RETURN(rc);
667 }
668 EXPORT_SYMBOL(llog_reverse_process);
669
670 /**
671  * new llog API
672  *
673  * API functions:
674  *      llog_open - open llog, may not exist
675  *      llog_exist - check if llog exists
676  *      llog_close - close opened llog, pair for open, frees llog_handle
677  *      llog_declare_create - declare llog creation
678  *      llog_create - create new llog on disk, need transaction handle
679  *      llog_declare_write_rec - declaration of llog write
680  *      llog_write_rec - write llog record on disk, need transaction handle
681  *      llog_declare_add - declare llog catalog record addition
682  *      llog_add - add llog record in catalog, need transaction handle
683  */
684 int llog_exist(struct llog_handle *loghandle)
685 {
686         struct llog_operations  *lop;
687         int                      rc;
688
689         ENTRY;
690
691         rc = llog_handle2ops(loghandle, &lop);
692         if (rc)
693                 RETURN(rc);
694         if (lop->lop_exist == NULL)
695                 RETURN(-EOPNOTSUPP);
696
697         rc = lop->lop_exist(loghandle);
698         RETURN(rc);
699 }
700 EXPORT_SYMBOL(llog_exist);
701
702 int llog_declare_create(const struct lu_env *env,
703                         struct llog_handle *loghandle, struct thandle *th)
704 {
705         struct llog_operations  *lop;
706         int                      raised, rc;
707
708         ENTRY;
709
710         rc = llog_handle2ops(loghandle, &lop);
711         if (rc)
712                 RETURN(rc);
713         if (lop->lop_declare_create == NULL)
714                 RETURN(-EOPNOTSUPP);
715
716         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
717         if (!raised)
718                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
719         rc = lop->lop_declare_create(env, loghandle, th);
720         if (!raised)
721                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
722         RETURN(rc);
723 }
724
725 int llog_create(const struct lu_env *env, struct llog_handle *handle,
726                 struct thandle *th)
727 {
728         struct llog_operations  *lop;
729         int                      raised, rc;
730
731         ENTRY;
732
733         rc = llog_handle2ops(handle, &lop);
734         if (rc)
735                 RETURN(rc);
736         if (lop->lop_create == NULL)
737                 RETURN(-EOPNOTSUPP);
738
739         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
740         if (!raised)
741                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
742         rc = lop->lop_create(env, handle, th);
743         if (!raised)
744                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
745         RETURN(rc);
746 }
747
748 int llog_declare_write_rec(const struct lu_env *env,
749                            struct llog_handle *handle,
750                            struct llog_rec_hdr *rec, int idx,
751                            struct thandle *th)
752 {
753         struct llog_operations  *lop;
754         int                      raised, rc;
755
756         ENTRY;
757
758         rc = llog_handle2ops(handle, &lop);
759         if (rc)
760                 RETURN(rc);
761         LASSERT(lop);
762         if (lop->lop_declare_write_rec == NULL)
763                 RETURN(-EOPNOTSUPP);
764
765         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
766         if (!raised)
767                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
768         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
769         if (!raised)
770                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
771         RETURN(rc);
772 }
773
774 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
775                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
776                    int idx, struct thandle *th)
777 {
778         struct llog_operations  *lop;
779         int                      raised, rc, buflen;
780
781         ENTRY;
782
783         rc = llog_handle2ops(handle, &lop);
784         if (rc)
785                 RETURN(rc);
786
787         LASSERT(lop);
788         if (lop->lop_write_rec == NULL)
789                 RETURN(-EOPNOTSUPP);
790
791         buflen = rec->lrh_len;
792         LASSERT(cfs_size_round(buflen) == buflen);
793
794         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
795         if (!raised)
796                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
797         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
798         if (!raised)
799                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
800         RETURN(rc);
801 }
802
803 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
804              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
805              struct thandle *th)
806 {
807         int raised, rc;
808
809         ENTRY;
810
811         if (lgh->lgh_logops->lop_add == NULL)
812                 RETURN(-EOPNOTSUPP);
813
814         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
815         if (!raised)
816                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
817         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
818         if (!raised)
819                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
820         RETURN(rc);
821 }
822 EXPORT_SYMBOL(llog_add);
823
824 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
825                      struct llog_rec_hdr *rec, struct thandle *th)
826 {
827         int raised, rc;
828
829         ENTRY;
830
831         if (lgh->lgh_logops->lop_declare_add == NULL)
832                 RETURN(-EOPNOTSUPP);
833
834         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
835         if (!raised)
836                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
837         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
838         if (!raised)
839                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
840         RETURN(rc);
841 }
842 EXPORT_SYMBOL(llog_declare_add);
843
844 /**
845  * Helper function to open llog or create it if doesn't exist.
846  * It hides all transaction handling from caller.
847  */
848 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
849                      struct llog_handle **res, struct llog_logid *logid,
850                      char *name)
851 {
852         struct dt_device        *d;
853         struct thandle          *th;
854         int                      rc;
855
856         ENTRY;
857
858         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
859         if (rc)
860                 RETURN(rc);
861
862         if (llog_exist(*res))
863                 RETURN(0);
864
865         LASSERT((*res)->lgh_obj != NULL);
866
867         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
868
869         th = dt_trans_create(env, d);
870         if (IS_ERR(th))
871                 GOTO(out, rc = PTR_ERR(th));
872
873         /* Create update llog object synchronously, which
874          * happens during inialization process see
875          * lod_sub_prep_llog(), to make sure the update
876          * llog object is created before corss-MDT writing
877          * updates into the llog object */
878         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
879                 th->th_sync = 1;
880
881         th->th_wait_submit = 1;
882         rc = llog_declare_create(env, *res, th);
883         if (rc == 0) {
884                 rc = dt_trans_start_local(env, d, th);
885                 if (rc == 0)
886                         rc = llog_create(env, *res, th);
887         }
888         dt_trans_stop(env, d, th);
889 out:
890         if (rc)
891                 llog_close(env, *res);
892         RETURN(rc);
893 }
894 EXPORT_SYMBOL(llog_open_create);
895
896 /**
897  * Helper function to delete existent llog.
898  */
899 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
900                struct llog_logid *logid, char *name)
901 {
902         struct llog_handle      *handle;
903         int                      rc = 0, rc2;
904
905         ENTRY;
906
907         /* nothing to erase */
908         if (name == NULL && logid == NULL)
909                 RETURN(0);
910
911         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
912         if (rc < 0)
913                 RETURN(rc);
914
915         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
916         if (rc == 0)
917                 rc = llog_destroy(env, handle);
918
919         rc2 = llog_close(env, handle);
920         if (rc == 0)
921                 rc = rc2;
922         RETURN(rc);
923 }
924 EXPORT_SYMBOL(llog_erase);
925
926 /*
927  * Helper function for write record in llog.
928  * It hides all transaction handling from caller.
929  * Valid only with local llog.
930  */
931 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
932                struct llog_rec_hdr *rec, int idx)
933 {
934         struct dt_device        *dt;
935         struct thandle          *th;
936         int                      rc;
937
938         ENTRY;
939
940         LASSERT(loghandle);
941         LASSERT(loghandle->lgh_ctxt);
942         LASSERT(loghandle->lgh_obj != NULL);
943
944         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
945
946         th = dt_trans_create(env, dt);
947         if (IS_ERR(th))
948                 RETURN(PTR_ERR(th));
949
950         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
951         if (rc)
952                 GOTO(out_trans, rc);
953
954         th->th_wait_submit = 1;
955         rc = dt_trans_start_local(env, dt, th);
956         if (rc)
957                 GOTO(out_trans, rc);
958
959         down_write(&loghandle->lgh_lock);
960         rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
961         up_write(&loghandle->lgh_lock);
962 out_trans:
963         dt_trans_stop(env, dt, th);
964         RETURN(rc);
965 }
966 EXPORT_SYMBOL(llog_write);
967
968 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
969               struct llog_handle **lgh, struct llog_logid *logid,
970               char *name, enum llog_open_param open_param)
971 {
972         int      raised;
973         int      rc;
974
975         ENTRY;
976
977         LASSERT(ctxt);
978         LASSERT(ctxt->loc_logops);
979
980         if (ctxt->loc_logops->lop_open == NULL) {
981                 *lgh = NULL;
982                 RETURN(-EOPNOTSUPP);
983         }
984
985         *lgh = llog_alloc_handle();
986         if (*lgh == NULL)
987                 RETURN(-ENOMEM);
988         (*lgh)->lgh_ctxt = ctxt;
989         (*lgh)->lgh_logops = ctxt->loc_logops;
990
991         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
992         if (!raised)
993                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
994         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
995         if (!raised)
996                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
997         if (rc) {
998                 llog_free_handle(*lgh);
999                 *lgh = NULL;
1000         }
1001         RETURN(rc);
1002 }
1003 EXPORT_SYMBOL(llog_open);
1004
1005 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1006 {
1007         struct llog_operations  *lop;
1008         int                      rc;
1009
1010         ENTRY;
1011
1012         rc = llog_handle2ops(loghandle, &lop);
1013         if (rc)
1014                 GOTO(out, rc);
1015         if (lop->lop_close == NULL)
1016                 GOTO(out, rc = -EOPNOTSUPP);
1017         rc = lop->lop_close(env, loghandle);
1018 out:
1019         llog_handle_put(loghandle);
1020         RETURN(rc);
1021 }
1022 EXPORT_SYMBOL(llog_close);
1023
1024 /**
1025  * Helper function to get the llog size in records. It is used by MGS
1026  * mostly to check that config llog exists and contains data.
1027  *
1028  * \param[in] env       execution environment
1029  * \param[in] ctxt      llog context
1030  * \param[in] name      llog name
1031  *
1032  * \retval              true if there are records in llog besides a header
1033  * \retval              false on error or llog without records
1034  */
1035 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1036                   char *name)
1037 {
1038         struct llog_handle      *llh;
1039         int                      rc = 0;
1040
1041         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1042         if (rc < 0) {
1043                 if (likely(rc == -ENOENT))
1044                         rc = 0;
1045                 GOTO(out, rc);
1046         }
1047
1048         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1049         if (rc)
1050                 GOTO(out_close, rc);
1051         rc = llog_get_size(llh);
1052
1053 out_close:
1054         llog_close(env, llh);
1055 out:
1056         /* The header is record 1, the llog is still considered as empty
1057          * if there is only header */
1058         return (rc <= 1);
1059 }
1060 EXPORT_SYMBOL(llog_is_empty);
1061
1062 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1063                       struct llog_rec_hdr *rec, void *data)
1064 {
1065         struct llog_handle      *copy_llh = data;
1066
1067         /* Append all records */
1068         return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1069 }
1070
1071 /* backup plain llog */
1072 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1073                 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1074                 char *name, char *backup)
1075 {
1076         struct llog_handle      *llh, *bllh;
1077         int                      rc;
1078
1079         ENTRY;
1080
1081         /* open original log */
1082         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1083         if (rc < 0) {
1084                 /* the -ENOENT case is also reported to the caller
1085                  * but silently so it should handle that if needed.
1086                  */
1087                 if (rc != -ENOENT)
1088                         CERROR("%s: failed to open log %s: rc = %d\n",
1089                                obd->obd_name, name, rc);
1090                 RETURN(rc);
1091         }
1092
1093         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1094         if (rc)
1095                 GOTO(out_close, rc);
1096
1097         /* Make sure there's no old backup log */
1098         rc = llog_erase(env, bctxt, NULL, backup);
1099         if (rc < 0 && rc != -ENOENT)
1100                 GOTO(out_close, rc);
1101
1102         /* open backup log */
1103         rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1104         if (rc) {
1105                 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1106                        obd->obd_name, backup, rc);
1107                 GOTO(out_close, rc);
1108         }
1109
1110         /* check that backup llog is not the same object as original one */
1111         if (llh->lgh_obj == bllh->lgh_obj) {
1112                 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1113                        obd->obd_name, name, backup, llh->lgh_obj,
1114                        bllh->lgh_obj);
1115                 GOTO(out_backup, rc = -EEXIST);
1116         }
1117
1118         rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1119         if (rc)
1120                 GOTO(out_backup, rc);
1121
1122         /* Copy log record by record */
1123         rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1124                                   NULL, false);
1125         if (rc)
1126                 CERROR("%s: failed to backup log %s: rc = %d\n",
1127                        obd->obd_name, name, rc);
1128 out_backup:
1129         llog_close(env, bllh);
1130 out_close:
1131         llog_close(env, llh);
1132         RETURN(rc);
1133 }
1134 EXPORT_SYMBOL(llog_backup);