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