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