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