Whamcloud - gitweb
LU-8753 llog: remove lgh_write_offset
[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("invalid length %d in llog record for "
542                                       "index %d/%d\n", rec->lrh_len,
543                                       rec->lrh_index, index);
544                                 GOTO(out, rc = -EINVAL);
545                         }
546
547                         if (rec->lrh_index < index) {
548                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
549                                        rec->lrh_index);
550                                 continue;
551                         }
552
553                         if (rec->lrh_index != index) {
554                                 CERROR("%s: Invalid record: index %u but "
555                                        "expected %u\n",
556                                        loghandle->lgh_ctxt->loc_obd->obd_name,
557                                        rec->lrh_index, index);
558                                 GOTO(out, rc = -ERANGE);
559                         }
560
561                         CDEBUG(D_OTHER,
562                                "lrh_index: %d lrh_len: %d (%d remains)\n",
563                                rec->lrh_index, rec->lrh_len,
564                                (int)(buf + chunk_size - (char *)rec));
565
566                         loghandle->lgh_cur_idx = rec->lrh_index;
567                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
568                                                     chunk_offset;
569
570                         /* if set, process the callback on this record */
571                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
572                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
573                                                  lpi->lpi_cbdata);
574                                 last_called_index = index;
575                                 if (rc == LLOG_PROC_BREAK) {
576                                         GOTO(out, rc);
577                                 } else if (rc == LLOG_DEL_RECORD) {
578                                         rc = llog_cancel_rec(lpi->lpi_env,
579                                                              loghandle,
580                                                              rec->lrh_index);
581                                 }
582                                 if (rc)
583                                         GOTO(out, rc);
584                         }
585                         /* exit if the last index is reached */
586                         if (index >= last_index)
587                                 GOTO(out, rc = 0);
588                         ++index;
589                 }
590         }
591
592 out:
593         if (cd != NULL)
594                 cd->lpcd_last_idx = last_called_index;
595
596         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
597                 if (dt_object_remote(loghandle->lgh_obj)) {
598                         /* If it is remote object, then -EIO might means
599                          * disconnection or eviction, let's return -EAGAIN,
600                          * so for update recovery log processing, it will
601                          * retry until the umount or abort recovery, see
602                          * lod_sub_recovery_thread() */
603                         CERROR("%s retry remote llog process\n",
604                                loghandle->lgh_ctxt->loc_obd->obd_name);
605                         rc = -EAGAIN;
606                 } else {
607                         /* something bad happened to the processing of a local
608                          * llog file, probably I/O error or the log got
609                          * corrupted to be able to finally release the log we
610                          * discard any remaining bits in the header */
611                         CERROR("Local llog found corrupted\n");
612                         while (index <= last_index) {
613                                 if (ext2_test_bit(index,
614                                                   LLOG_HDR_BITMAP(llh)) != 0)
615                                         llog_cancel_rec(lpi->lpi_env, loghandle,
616                                                         index);
617                                 index++;
618                         }
619                         rc = 0;
620                 }
621         }
622
623         OBD_FREE_LARGE(buf, chunk_size);
624         lpi->lpi_rc = rc;
625         return 0;
626 }
627
628 static int llog_process_thread_daemonize(void *arg)
629 {
630         struct llog_process_info        *lpi = arg;
631         struct lu_env                    env;
632         int                              rc;
633         struct nsproxy                  *new_ns, *curr_ns = current->nsproxy;
634
635         task_lock(lpi->lpi_reftask);
636         new_ns = lpi->lpi_reftask->nsproxy;
637         if (curr_ns != new_ns) {
638                 get_nsproxy(new_ns);
639
640                 current->nsproxy = new_ns;
641                 /* XXX: we should call put_nsproxy() instead of
642                  * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
643                  * used outside of the kernel itself, because it calls
644                  * free_nsproxy() which is not exported by the kernel
645                  * (defined in kernel/nsproxy.c) */
646                 atomic_dec(&curr_ns->count);
647         }
648         task_unlock(lpi->lpi_reftask);
649
650         unshare_fs_struct();
651
652         /* client env has no keys, tags is just 0 */
653         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
654         if (rc)
655                 goto out;
656         lpi->lpi_env = &env;
657
658         rc = llog_process_thread(arg);
659
660         lu_env_fini(&env);
661 out:
662         complete(&lpi->lpi_completion);
663         return rc;
664 }
665
666 int llog_process_or_fork(const struct lu_env *env,
667                          struct llog_handle *loghandle,
668                          llog_cb_t cb, void *data, void *catdata, bool fork)
669 {
670         struct llog_process_info *lpi;
671         int                      rc;
672
673         ENTRY;
674
675         OBD_ALLOC_PTR(lpi);
676         if (lpi == NULL) {
677                 CERROR("cannot alloc pointer\n");
678                 RETURN(-ENOMEM);
679         }
680         lpi->lpi_loghandle = loghandle;
681         lpi->lpi_cb        = cb;
682         lpi->lpi_cbdata    = data;
683         lpi->lpi_catdata   = catdata;
684
685         if (fork) {
686                 struct task_struct *task;
687
688                 /* The new thread can't use parent env,
689                  * init the new one in llog_process_thread_daemonize. */
690                 lpi->lpi_env = NULL;
691                 init_completion(&lpi->lpi_completion);
692                 /* take reference to current, so that
693                  * llog_process_thread_daemonize() can use it to switch to
694                  * namespace associated with current  */
695                 lpi->lpi_reftask = current;
696                 task = kthread_run(llog_process_thread_daemonize, lpi,
697                                    "llog_process_thread");
698                 if (IS_ERR(task)) {
699                         rc = PTR_ERR(task);
700                         CERROR("%s: cannot start thread: rc = %d\n",
701                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
702                         GOTO(out_lpi, rc);
703                 }
704                 wait_for_completion(&lpi->lpi_completion);
705         } else {
706                 lpi->lpi_env = env;
707                 llog_process_thread(lpi);
708         }
709         rc = lpi->lpi_rc;
710
711 out_lpi:
712         OBD_FREE_PTR(lpi);
713         RETURN(rc);
714 }
715 EXPORT_SYMBOL(llog_process_or_fork);
716
717 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
718                  llog_cb_t cb, void *data, void *catdata)
719 {
720         int rc;
721         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
722         return rc == LLOG_DEL_PLAIN ? 0 : rc;
723 }
724 EXPORT_SYMBOL(llog_process);
725
726 int llog_reverse_process(const struct lu_env *env,
727                          struct llog_handle *loghandle, llog_cb_t cb,
728                          void *data, void *catdata)
729 {
730         struct llog_log_hdr *llh = loghandle->lgh_hdr;
731         struct llog_process_cat_data *cd = catdata;
732         void *buf;
733         int rc = 0, first_index = 1, index, idx;
734         __u32   chunk_size = llh->llh_hdr.lrh_len;
735         ENTRY;
736
737         OBD_ALLOC_LARGE(buf, chunk_size);
738         if (buf == NULL)
739                 RETURN(-ENOMEM);
740
741         if (cd != NULL)
742                 first_index = cd->lpcd_first_idx + 1;
743         if (cd != NULL && cd->lpcd_last_idx)
744                 index = cd->lpcd_last_idx;
745         else
746                 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
747
748         while (rc == 0) {
749                 struct llog_rec_hdr *rec;
750                 struct llog_rec_tail *tail;
751
752                 /* skip records not set in bitmap */
753                 while (index >= first_index &&
754                        !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
755                         --index;
756
757                 LASSERT(index >= first_index - 1);
758                 if (index == first_index - 1)
759                         break;
760
761                 /* get the buf with our target record; avoid old garbage */
762                 memset(buf, 0, chunk_size);
763                 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
764                 if (rc)
765                         GOTO(out, rc);
766
767                 rec = buf;
768                 idx = rec->lrh_index;
769                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
770                 while (idx < index) {
771                         rec = (void *)rec + rec->lrh_len;
772                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
773                                 lustre_swab_llog_rec(rec);
774                         idx ++;
775                 }
776                 LASSERT(idx == index);
777                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
778
779                 /* process records in buffer, starting where we found one */
780                 while ((void *)tail > buf) {
781                         if (tail->lrt_index == 0)
782                                 GOTO(out, rc = 0); /* no more records */
783
784                         /* if set, process the callback on this record */
785                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
786                                 rec = (void *)tail - tail->lrt_len +
787                                       sizeof(*tail);
788
789                                 rc = cb(env, loghandle, rec, data);
790                                 if (rc == LLOG_PROC_BREAK) {
791                                         GOTO(out, rc);
792                                 } else if (rc == LLOG_DEL_RECORD) {
793                                         rc = llog_cancel_rec(env, loghandle,
794                                                              tail->lrt_index);
795                                 }
796                                 if (rc)
797                                         GOTO(out, rc);
798                         }
799
800                         /* previous record, still in buffer? */
801                         --index;
802                         if (index < first_index)
803                                 GOTO(out, rc = 0);
804                         tail = (void *)tail - tail->lrt_len;
805                 }
806         }
807
808 out:
809         if (buf != NULL)
810                 OBD_FREE_LARGE(buf, chunk_size);
811         RETURN(rc);
812 }
813 EXPORT_SYMBOL(llog_reverse_process);
814
815 /**
816  * new llog API
817  *
818  * API functions:
819  *      llog_open - open llog, may not exist
820  *      llog_exist - check if llog exists
821  *      llog_close - close opened llog, pair for open, frees llog_handle
822  *      llog_declare_create - declare llog creation
823  *      llog_create - create new llog on disk, need transaction handle
824  *      llog_declare_write_rec - declaration of llog write
825  *      llog_write_rec - write llog record on disk, need transaction handle
826  *      llog_declare_add - declare llog catalog record addition
827  *      llog_add - add llog record in catalog, need transaction handle
828  */
829 int llog_exist(struct llog_handle *loghandle)
830 {
831         struct llog_operations  *lop;
832         int                      rc;
833
834         ENTRY;
835
836         rc = llog_handle2ops(loghandle, &lop);
837         if (rc)
838                 RETURN(rc);
839         if (lop->lop_exist == NULL)
840                 RETURN(-EOPNOTSUPP);
841
842         rc = lop->lop_exist(loghandle);
843         RETURN(rc);
844 }
845 EXPORT_SYMBOL(llog_exist);
846
847 int llog_declare_create(const struct lu_env *env,
848                         struct llog_handle *loghandle, struct thandle *th)
849 {
850         struct llog_operations  *lop;
851         int                      raised, rc;
852
853         ENTRY;
854
855         rc = llog_handle2ops(loghandle, &lop);
856         if (rc)
857                 RETURN(rc);
858         if (lop->lop_declare_create == NULL)
859                 RETURN(-EOPNOTSUPP);
860
861         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
862         if (!raised)
863                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
864         rc = lop->lop_declare_create(env, loghandle, th);
865         if (!raised)
866                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
867         RETURN(rc);
868 }
869
870 int llog_create(const struct lu_env *env, struct llog_handle *handle,
871                 struct thandle *th)
872 {
873         struct llog_operations  *lop;
874         int                      raised, rc;
875
876         ENTRY;
877
878         rc = llog_handle2ops(handle, &lop);
879         if (rc)
880                 RETURN(rc);
881         if (lop->lop_create == NULL)
882                 RETURN(-EOPNOTSUPP);
883
884         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
885         if (!raised)
886                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
887         rc = lop->lop_create(env, handle, th);
888         if (!raised)
889                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
890         RETURN(rc);
891 }
892
893 int llog_declare_write_rec(const struct lu_env *env,
894                            struct llog_handle *handle,
895                            struct llog_rec_hdr *rec, int idx,
896                            struct thandle *th)
897 {
898         struct llog_operations  *lop;
899         int                      raised, rc;
900
901         ENTRY;
902
903         rc = llog_handle2ops(handle, &lop);
904         if (rc)
905                 RETURN(rc);
906         LASSERT(lop);
907         if (lop->lop_declare_write_rec == NULL)
908                 RETURN(-EOPNOTSUPP);
909
910         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
911         if (!raised)
912                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
913         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
914         if (!raised)
915                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
916         RETURN(rc);
917 }
918
919 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
920                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
921                    int idx, struct thandle *th)
922 {
923         struct llog_operations  *lop;
924         int                      raised, rc, buflen;
925
926         ENTRY;
927
928         /* API sanity checks */
929         if (handle == NULL) {
930                 CERROR("loghandle is missed\n");
931                 RETURN(-EPROTO);
932         } else if (handle->lgh_obj == NULL) {
933                 CERROR("loghandle %p with NULL object\n",
934                         handle);
935                 RETURN(-EPROTO);
936         } else if (th == NULL) {
937                 CERROR("%s: missed transaction handle\n",
938                         handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
939                 RETURN(-EPROTO);
940         } else if (handle->lgh_hdr == NULL) {
941                 CERROR("%s: loghandle %p with no header\n",
942                         handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
943                         handle);
944                 RETURN(-EPROTO);
945         }
946
947         rc = llog_handle2ops(handle, &lop);
948         if (rc)
949                 RETURN(rc);
950
951         if (lop->lop_write_rec == NULL)
952                 RETURN(-EOPNOTSUPP);
953
954         buflen = rec->lrh_len;
955         LASSERT(cfs_size_round(buflen) == buflen);
956
957         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
958         if (!raised)
959                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
960         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
961         if (!raised)
962                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
963         RETURN(rc);
964 }
965
966 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
967              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
968              struct thandle *th)
969 {
970         int raised, rc;
971
972         ENTRY;
973
974         if (lgh->lgh_logops->lop_add == NULL)
975                 RETURN(-EOPNOTSUPP);
976
977         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
978         if (!raised)
979                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
980         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
981         if (!raised)
982                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
983         RETURN(rc);
984 }
985 EXPORT_SYMBOL(llog_add);
986
987 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
988                      struct llog_rec_hdr *rec, struct thandle *th)
989 {
990         int raised, rc;
991
992         ENTRY;
993
994         if (lgh->lgh_logops->lop_declare_add == NULL)
995                 RETURN(-EOPNOTSUPP);
996
997         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
998         if (!raised)
999                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1000         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1001         if (!raised)
1002                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1003         RETURN(rc);
1004 }
1005 EXPORT_SYMBOL(llog_declare_add);
1006
1007 /**
1008  * Helper function to open llog or create it if doesn't exist.
1009  * It hides all transaction handling from caller.
1010  */
1011 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1012                      struct llog_handle **res, struct llog_logid *logid,
1013                      char *name)
1014 {
1015         struct dt_device        *d;
1016         struct thandle          *th;
1017         int                      rc;
1018
1019         ENTRY;
1020
1021         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1022         if (rc)
1023                 RETURN(rc);
1024
1025         if (llog_exist(*res))
1026                 RETURN(0);
1027
1028         LASSERT((*res)->lgh_obj != NULL);
1029
1030         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1031
1032         th = dt_trans_create(env, d);
1033         if (IS_ERR(th))
1034                 GOTO(out, rc = PTR_ERR(th));
1035
1036         /* Create update llog object synchronously, which
1037          * happens during inialization process see
1038          * lod_sub_prep_llog(), to make sure the update
1039          * llog object is created before corss-MDT writing
1040          * updates into the llog object */
1041         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1042                 th->th_sync = 1;
1043
1044         th->th_wait_submit = 1;
1045         rc = llog_declare_create(env, *res, th);
1046         if (rc == 0) {
1047                 rc = dt_trans_start_local(env, d, th);
1048                 if (rc == 0)
1049                         rc = llog_create(env, *res, th);
1050         }
1051         dt_trans_stop(env, d, th);
1052 out:
1053         if (rc)
1054                 llog_close(env, *res);
1055         RETURN(rc);
1056 }
1057 EXPORT_SYMBOL(llog_open_create);
1058
1059 /**
1060  * Helper function to delete existent llog.
1061  */
1062 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1063                struct llog_logid *logid, char *name)
1064 {
1065         struct llog_handle      *handle;
1066         int                      rc = 0, rc2;
1067
1068         ENTRY;
1069
1070         /* nothing to erase */
1071         if (name == NULL && logid == NULL)
1072                 RETURN(0);
1073
1074         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1075         if (rc < 0)
1076                 RETURN(rc);
1077
1078         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1079         if (rc == 0)
1080                 rc = llog_destroy(env, handle);
1081
1082         rc2 = llog_close(env, handle);
1083         if (rc == 0)
1084                 rc = rc2;
1085         RETURN(rc);
1086 }
1087 EXPORT_SYMBOL(llog_erase);
1088
1089 /*
1090  * Helper function for write record in llog.
1091  * It hides all transaction handling from caller.
1092  * Valid only with local llog.
1093  */
1094 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1095                struct llog_rec_hdr *rec, int idx)
1096 {
1097         struct dt_device        *dt;
1098         struct thandle          *th;
1099         int                      rc;
1100
1101         ENTRY;
1102
1103         LASSERT(loghandle);
1104         LASSERT(loghandle->lgh_ctxt);
1105         LASSERT(loghandle->lgh_obj != NULL);
1106
1107         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1108
1109         th = dt_trans_create(env, dt);
1110         if (IS_ERR(th))
1111                 RETURN(PTR_ERR(th));
1112
1113         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1114         if (rc)
1115                 GOTO(out_trans, rc);
1116
1117         th->th_wait_submit = 1;
1118         rc = dt_trans_start_local(env, dt, th);
1119         if (rc)
1120                 GOTO(out_trans, rc);
1121
1122         down_write(&loghandle->lgh_lock);
1123         rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1124         up_write(&loghandle->lgh_lock);
1125 out_trans:
1126         dt_trans_stop(env, dt, th);
1127         RETURN(rc);
1128 }
1129 EXPORT_SYMBOL(llog_write);
1130
1131 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1132               struct llog_handle **lgh, struct llog_logid *logid,
1133               char *name, enum llog_open_param open_param)
1134 {
1135         int      raised;
1136         int      rc;
1137
1138         ENTRY;
1139
1140         LASSERT(ctxt);
1141         LASSERT(ctxt->loc_logops);
1142
1143         if (ctxt->loc_logops->lop_open == NULL) {
1144                 *lgh = NULL;
1145                 RETURN(-EOPNOTSUPP);
1146         }
1147
1148         *lgh = llog_alloc_handle();
1149         if (*lgh == NULL)
1150                 RETURN(-ENOMEM);
1151         (*lgh)->lgh_ctxt = ctxt;
1152         (*lgh)->lgh_logops = ctxt->loc_logops;
1153
1154         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
1155         if (!raised)
1156                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
1157         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1158         if (!raised)
1159                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
1160         if (rc) {
1161                 llog_free_handle(*lgh);
1162                 *lgh = NULL;
1163         }
1164         RETURN(rc);
1165 }
1166 EXPORT_SYMBOL(llog_open);
1167
1168 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1169 {
1170         struct llog_operations  *lop;
1171         int                      rc;
1172
1173         ENTRY;
1174
1175         rc = llog_handle2ops(loghandle, &lop);
1176         if (rc)
1177                 GOTO(out, rc);
1178         if (lop->lop_close == NULL)
1179                 GOTO(out, rc = -EOPNOTSUPP);
1180         rc = lop->lop_close(env, loghandle);
1181 out:
1182         llog_handle_put(loghandle);
1183         RETURN(rc);
1184 }
1185 EXPORT_SYMBOL(llog_close);
1186
1187 /**
1188  * Helper function to get the llog size in records. It is used by MGS
1189  * mostly to check that config llog exists and contains data.
1190  *
1191  * \param[in] env       execution environment
1192  * \param[in] ctxt      llog context
1193  * \param[in] name      llog name
1194  *
1195  * \retval              true if there are records in llog besides a header
1196  * \retval              false on error or llog without records
1197  */
1198 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1199                   char *name)
1200 {
1201         struct llog_handle      *llh;
1202         int                      rc = 0;
1203
1204         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1205         if (rc < 0) {
1206                 if (likely(rc == -ENOENT))
1207                         rc = 0;
1208                 GOTO(out, rc);
1209         }
1210
1211         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1212         if (rc)
1213                 GOTO(out_close, rc);
1214         rc = llog_get_size(llh);
1215
1216 out_close:
1217         llog_close(env, llh);
1218 out:
1219         /* The header is record 1, the llog is still considered as empty
1220          * if there is only header */
1221         return (rc <= 1);
1222 }
1223 EXPORT_SYMBOL(llog_is_empty);
1224
1225 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1226                       struct llog_rec_hdr *rec, void *data)
1227 {
1228         struct llog_handle      *copy_llh = data;
1229
1230         /* Append all records */
1231         return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1232 }
1233
1234 /* backup plain llog */
1235 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1236                 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1237                 char *name, char *backup)
1238 {
1239         struct llog_handle      *llh, *bllh;
1240         int                      rc;
1241
1242         ENTRY;
1243
1244         /* open original log */
1245         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1246         if (rc < 0) {
1247                 /* the -ENOENT case is also reported to the caller
1248                  * but silently so it should handle that if needed.
1249                  */
1250                 if (rc != -ENOENT)
1251                         CERROR("%s: failed to open log %s: rc = %d\n",
1252                                obd->obd_name, name, rc);
1253                 RETURN(rc);
1254         }
1255
1256         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1257         if (rc)
1258                 GOTO(out_close, rc);
1259
1260         /* Make sure there's no old backup log */
1261         rc = llog_erase(env, bctxt, NULL, backup);
1262         if (rc < 0 && rc != -ENOENT)
1263                 GOTO(out_close, rc);
1264
1265         /* open backup log */
1266         rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1267         if (rc) {
1268                 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1269                        obd->obd_name, backup, rc);
1270                 GOTO(out_close, rc);
1271         }
1272
1273         /* check that backup llog is not the same object as original one */
1274         if (llh->lgh_obj == bllh->lgh_obj) {
1275                 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1276                        obd->obd_name, name, backup, llh->lgh_obj,
1277                        bllh->lgh_obj);
1278                 GOTO(out_backup, rc = -EEXIST);
1279         }
1280
1281         rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1282         if (rc)
1283                 GOTO(out_backup, rc);
1284
1285         /* Copy log record by record */
1286         rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1287                                   NULL, false);
1288         if (rc)
1289                 CERROR("%s: failed to backup log %s: rc = %d\n",
1290                        obd->obd_name, name, rc);
1291 out_backup:
1292         llog_close(env, bllh);
1293 out_close:
1294         llog_close(env, llh);
1295         RETURN(rc);
1296 }
1297 EXPORT_SYMBOL(llog_backup);
1298
1299 /* Get size of llog */
1300 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1301 {
1302         int rc;
1303         struct lu_attr la;
1304
1305         rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1306         if (rc) {
1307                 CERROR("%s: attr_get failed, rc = %d\n",
1308                        llh->lgh_ctxt->loc_obd->obd_name, rc);
1309                 return 0;
1310         }
1311
1312         return la.la_size;
1313 }
1314 EXPORT_SYMBOL(llog_size);
1315