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