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