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