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