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