Whamcloud - gitweb
LU-16203 llog: skip bad records in llog
[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 #define LLOG_ERROR_REC(lgh, rec, format, a...) \
468         CERROR("%s: "DFID" rec type=%x idx=%u len=%u, " format "\n" , \
469                loghandle2name(lgh), PLOGID(&lgh->lgh_id), (rec)->lrh_type, \
470                (rec)->lrh_index, (rec)->lrh_len, ##a)
471
472 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
473 {
474         int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
475
476         if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC)
477                 LLOG_ERROR_REC(llh, rec, "magic is bad");
478         else if (rec->lrh_len == 0 || rec->lrh_len > chunk_size)
479                 LLOG_ERROR_REC(llh, rec, "bad record len, chunk size is %d",
480                                chunk_size);
481         else if (rec->lrh_index >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr))
482                 LLOG_ERROR_REC(llh, rec, "index is too high");
483         else
484                 return 0;
485
486         return -EINVAL;
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
514         ENTRY;
515
516         if (llh == NULL)
517                 RETURN(-EINVAL);
518
519         lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
520
521         cur_offset = chunk_size = llh->llh_hdr.lrh_len;
522         /* expect chunk_size to be power of two */
523         LASSERT(is_power_of_2(chunk_size));
524
525         OBD_ALLOC_LARGE(buf, chunk_size);
526         if (buf == NULL) {
527                 lpi->lpi_rc = -ENOMEM;
528                 RETURN(0);
529         }
530
531         if (cd != NULL) {
532                 last_called_index = cd->lpcd_first_idx;
533                 index = cd->lpcd_first_idx + 1;
534         }
535         if (cd && cd->lpcd_last_idx)
536                 last_index = cd->lpcd_last_idx;
537         else if (cd && (cd->lpcd_read_mode & LLOG_READ_MODE_RAW))
538                 last_index = loghandle->lgh_last_idx;
539         else
540                 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
541
542         while (rc == 0) {
543                 struct llog_rec_hdr *rec;
544                 off_t chunk_offset = 0;
545                 unsigned int buf_offset = 0;
546                 int lh_last_idx;
547                 int synced_idx = 0;
548
549                 /* skip records not set in bitmap */
550                 while (index <= last_index &&
551                        llog_is_index_skipable(index, llh, cd))
552                         ++index;
553
554                 /* There are no indices prior the last_index */
555                 if (index > last_index)
556                         break;
557
558                 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
559                        last_index);
560
561 repeat:
562                 /* get the buf with our target record; avoid old garbage */
563                 memset(buf, 0, chunk_size);
564                 /* the record index for outdated chunk data */
565                 /* it is safe to process buffer until saved lgh_last_idx */
566                 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
567                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
568                                      index, &cur_offset, buf, chunk_size);
569                 if (repeated && rc)
570                         CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
571                                " buf_offset %u, rc = %d\n", cur_offset,
572                                (__u64)chunk_offset, buf_offset, rc);
573                 if (rc == -ESTALE)
574                         GOTO(out, rc = 0);
575                 /* we`ve tried to reread the chunk, but there is no
576                  * new records */
577                 if (repeated && (chunk_offset + buf_offset) == cur_offset &&
578                     (rc == -EBADR || rc == -EIO))
579                         GOTO(out, rc = 0);
580                 /* EOF while trying to skip to the next chunk */
581                 if (!index && rc == -EBADR)
582                         GOTO(out, rc = 0);
583                 if (rc != 0)
584                         GOTO(out, rc);
585
586                 /* NB: after llog_next_block() call the cur_offset is the
587                  * offset of the next block after read one.
588                  * The absolute offset of the current chunk is calculated
589                  * from cur_offset value and stored in chunk_offset variable.
590                  */
591                 if ((cur_offset & (chunk_size - 1)) != 0)
592                         chunk_offset = cur_offset & ~(chunk_size - 1);
593                 else
594                         chunk_offset = cur_offset - chunk_size;
595
596                 /* NB: when rec->lrh_len is accessed it is already swabbed
597                  * since it is used at the "end" of the loop and the rec
598                  * swabbing is done at the beginning of the loop. */
599                 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
600                      (char *)rec < buf + chunk_size;
601                      rec = llog_rec_hdr_next(rec)) {
602
603                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
604                                rec, rec->lrh_type);
605
606                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
607                                 lustre_swab_llog_rec(rec);
608
609                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
610                                rec->lrh_type, rec->lrh_index);
611
612                         /* start with first rec if block was skipped */
613                         if (!index) {
614                                 CDEBUG(D_OTHER,
615                                        "%s: skipping to the index %u\n",
616                                        loghandle2name(loghandle),
617                                        rec->lrh_index);
618                                 index = rec->lrh_index;
619                         }
620
621                         if (index == (synced_idx + 1) &&
622                             synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
623                                 GOTO(out, rc = 0);
624
625                         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
626                                 cfs_fail_val == (unsigned int)
627                                         (loghandle->lgh_id.lgl_oi.oi.oi_id &
628                                          0xFFFFFFFF)) {
629                                 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
630                         }
631
632                         /* the bitmap could be changed during processing
633                          * records from the chunk. For wrapped catalog
634                          * it means we can read deleted record and try to
635                          * process it. Check this case and reread the chunk.
636                          * It is safe to process to lh_last_idx, including
637                          * lh_last_idx if it was synced. We can not do <=
638                          * comparison, cause for wrapped catalog lgh_last_idx
639                          * could be less than index. So we detect last index
640                          * for processing as index == lh_last_idx+1. But when
641                          * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
642                          * the first processing index is llh_cat_idx+1.The
643                          * exception is !(lgh_last_idx == llh_cat_idx &&
644                          * index == llh_cat_idx + 1), and after simplification
645                          * it turns to
646                          * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
647                          * This exception is working for catalog only.
648                          * The last check is for the partial chunk boundary,
649                          * if it is reached then try to re-read for possible
650                          * new records once.
651                          */
652                         if ((index == lh_last_idx && synced_idx != index) ||
653                             (index == (lh_last_idx + 1) &&
654                              lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
655                             (((char *)rec - buf >= cur_offset - chunk_offset) &&
656                             !repeated)) {
657                                 /* save offset inside buffer for the re-read */
658                                 buf_offset = (char *)rec - (char *)buf;
659                                 cur_offset = chunk_offset;
660                                 repeated = true;
661                                 /* We need to be sure lgh_last_idx
662                                  * record was saved to disk
663                                  */
664                                 down_read(&loghandle->lgh_last_sem);
665                                 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
666                                 up_read(&loghandle->lgh_last_sem);
667                                 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
668                                 goto repeat;
669                         }
670                         repeated = false;
671
672                         rc = llog_verify_record(loghandle, rec);
673                         if (rc) {
674                                 CDEBUG(D_OTHER, "invalid record at index %d\n",
675                                        index);
676                                 /*
677                                  * for fixed-sized llogs we can skip one record
678                                  * by using llh_size from llog header.
679                                  * Otherwise skip the next llog chunk.
680                                  */
681                                 rc = 0;
682                                 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
683                                         rec->lrh_len = llh->llh_size;
684                                         goto next_rec;
685                                 }
686                                 /* make sure that is always next block */
687                                 cur_offset = chunk_offset + chunk_size;
688                                 /* no goal to find, just next block to read */
689                                 index = 0;
690                                 break;
691                         }
692
693                         if (rec->lrh_index < index) {
694                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
695                                        rec->lrh_index);
696                                 continue;
697                         }
698
699                         if (rec->lrh_index > index) {
700                                 /* the record itself looks good, but we met a
701                                  * gap which can be result of old bugs, just
702                                  * keep going */
703                                 LLOG_ERROR_REC(loghandle, rec,
704                                                "gap in index, expected %u",
705                                                index);
706                                 index = rec->lrh_index;
707                         }
708
709                         CDEBUG(D_OTHER,
710                                "lrh_index: %d lrh_len: %d (%d remains)\n",
711                                rec->lrh_index, rec->lrh_len,
712                                (int)(buf + chunk_size - (char *)rec));
713
714                         /* lgh_cur_offset is used only at llog_test_3 */
715                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
716                                                     chunk_offset;
717
718                         /* if needed, process the callback on this record */
719                         if (!llog_is_index_skipable(index, llh, cd)) {
720                                 struct llog_cookie *lgc;
721                                 __u64   tmp_off;
722                                 int     tmp_idx;
723
724                                 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
725                                         D_HA : D_OTHER),
726                                        "index: %d, lh_last_idx: %d "
727                                        "synced_idx: %d lgh_last_idx: %d\n",
728                                        index, lh_last_idx, synced_idx,
729                                        loghandle->lgh_last_idx);
730
731                                 if (lti != NULL) {
732                                         lgc = &lti->lgi_cookie;
733                                         /* store lu_env for recursive calls */
734                                         tmp_off = lgc->lgc_offset;
735                                         tmp_idx = lgc->lgc_index;
736
737                                         lgc->lgc_offset = (char *)rec -
738                                                 (char *)buf + chunk_offset;
739                                         lgc->lgc_index = rec->lrh_index;
740                                 }
741                                 /* using lu_env for passing record offset to
742                                  * llog_write through various callbacks */
743                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
744                                                  lpi->lpi_cbdata);
745                                 last_called_index = index;
746
747                                 if (lti != NULL) {
748                                         lgc->lgc_offset = tmp_off;
749                                         lgc->lgc_index = tmp_idx;
750                                 }
751
752                                 if (rc == LLOG_PROC_BREAK ||
753                                     rc == LLOG_SKIP_PLAIN) {
754                                         GOTO(out, rc);
755                                 } else if (rc == LLOG_DEL_RECORD) {
756                                         rc = llog_cancel_rec(lpi->lpi_env,
757                                                              loghandle,
758                                                              rec->lrh_index);
759                                         /* Allow parallel cancelling, ENOENT
760                                          * means record was canceled at another
761                                          * processing thread or callback
762                                          */
763                                         if (rc == -ENOENT)
764                                                 rc = 0;
765                                 }
766                                 if (rc)
767                                         GOTO(out, rc);
768                                 /* some stupid callbacks directly cancel records
769                                  * and delete llog. Check it and stop
770                                  * processing. */
771                                 if (loghandle->lgh_hdr == NULL ||
772                                     loghandle->lgh_hdr->llh_count == 1)
773                                         GOTO(out, rc = 0);
774                         }
775 next_rec:
776                         /* exit if the last index is reached */
777                         if (index >= last_index)
778                                 GOTO(out, rc = 0);
779                         ++index;
780                 }
781         }
782
783 out:
784         CDEBUG(D_HA, "stop processing %s "DFID" index %d count %d\n",
785                ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
786                PLOGID(&loghandle->lgh_id), index, llh->llh_count);
787
788         if (cd != NULL)
789                 cd->lpcd_last_idx = last_called_index;
790
791         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
792                 if (dt_object_remote(loghandle->lgh_obj)) {
793                         /* If it is remote object, then -EIO might means
794                          * disconnection or eviction, let's return -EAGAIN,
795                          * so for update recovery log processing, it will
796                          * retry until the umount or abort recovery, see
797                          * lod_sub_recovery_thread() */
798                         CERROR("%s retry remote llog process\n",
799                                loghandle2name(loghandle));
800                         rc = -EAGAIN;
801                 } else {
802                         /* something bad happened to the processing of a local
803                          * llog file, probably I/O error or the log got
804                          * corrupted to be able to finally release the log we
805                          * discard any remaining bits in the header */
806                         CERROR("%s: local llog is corrupted "DFID" %s index %d count %d\n",
807                                loghandle2name(loghandle),
808                                PLOGID(&loghandle->lgh_id),
809                                ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
810                                "plain"), index, llh->llh_count);
811
812                         while (index <= last_index) {
813                                 if (test_bit_le(index,
814                                                   LLOG_HDR_BITMAP(llh)) != 0)
815                                         llog_cancel_rec(lpi->lpi_env, loghandle,
816                                                         index);
817                                 index++;
818                         }
819                         rc = 0;
820                 }
821         }
822
823         OBD_FREE_LARGE(buf, chunk_size);
824         lpi->lpi_rc = rc;
825         return 0;
826 }
827
828 static int llog_process_thread_daemonize(void *arg)
829 {
830         struct llog_process_info        *lpi = arg;
831         struct lu_env                    env;
832         int                              rc;
833         struct nsproxy                  *new_ns, *curr_ns = current->nsproxy;
834
835         task_lock(lpi->lpi_reftask);
836         new_ns = lpi->lpi_reftask->nsproxy;
837         if (curr_ns != new_ns) {
838                 get_nsproxy(new_ns);
839
840                 current->nsproxy = new_ns;
841                 /* XXX: we should call put_nsproxy() instead of
842                  * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
843                  * used outside of the kernel itself, because it calls
844                  * free_nsproxy() which is not exported by the kernel
845                  * (defined in kernel/nsproxy.c) */
846                 if (curr_ns)
847                         atomic_dec(&curr_ns->count);
848         }
849         task_unlock(lpi->lpi_reftask);
850
851         unshare_fs_struct();
852         /* client env has no keys, tags is just 0 */
853         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
854         if (rc)
855                 goto out;
856         lpi->lpi_env = &env;
857
858         rc = llog_process_thread(arg);
859
860         lu_env_fini(&env);
861 out:
862         complete(&lpi->lpi_completion);
863         return rc;
864 }
865
866 int llog_process_or_fork(const struct lu_env *env,
867                          struct llog_handle *loghandle,
868                          llog_cb_t cb, void *data, void *catdata, bool fork)
869 {
870         struct llog_process_info *lpi;
871         struct llog_process_data *d = data;
872         struct llog_process_cat_data *cd = catdata;
873         __u32 flags = loghandle->lgh_hdr->llh_flags;
874         int rc;
875
876         ENTRY;
877
878         OBD_ALLOC_PTR(lpi);
879         if (lpi == NULL) {
880                 CERROR("cannot alloc pointer\n");
881                 RETURN(-ENOMEM);
882         }
883         lpi->lpi_loghandle = loghandle;
884         lpi->lpi_cb        = cb;
885         lpi->lpi_cbdata    = data;
886         lpi->lpi_catdata   = catdata;
887
888         CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d read_mode %d\n",
889                PLOGID(&loghandle->lgh_id), flags,
890                (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
891                (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
892                cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1,
893                cd ? cd->lpcd_read_mode : -1);
894         if (fork) {
895                 struct task_struct *task;
896
897                 /* The new thread can't use parent env,
898                  * init the new one in llog_process_thread_daemonize. */
899                 lpi->lpi_env = NULL;
900                 init_completion(&lpi->lpi_completion);
901                 /* take reference to current, so that
902                  * llog_process_thread_daemonize() can use it to switch to
903                  * namespace associated with current  */
904                 lpi->lpi_reftask = current;
905                 task = kthread_run(llog_process_thread_daemonize, lpi,
906                                    "llog_process_thread");
907                 if (IS_ERR(task)) {
908                         rc = PTR_ERR(task);
909                         CERROR("%s: cannot start thread: rc = %d\n",
910                                loghandle2name(loghandle), rc);
911                         GOTO(out_lpi, rc);
912                 }
913                 wait_for_completion(&lpi->lpi_completion);
914         } else {
915                 lpi->lpi_env = env;
916                 llog_process_thread(lpi);
917         }
918         rc = lpi->lpi_rc;
919
920 out_lpi:
921         OBD_FREE_PTR(lpi);
922         RETURN(rc);
923 }
924 EXPORT_SYMBOL(llog_process_or_fork);
925
926 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
927                  llog_cb_t cb, void *data, void *catdata)
928 {
929         int rc;
930         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
931         return rc == LLOG_DEL_PLAIN ? 0 : rc;
932 }
933 EXPORT_SYMBOL(llog_process);
934
935 static inline const struct cred *llog_raise_resource(void)
936 {
937         struct cred *cred = NULL;
938
939         if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
940                 return cred;
941
942         cred = prepare_creds();
943         if (!cred)
944                 return cred;
945
946         cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
947         return override_creds(cred);
948 }
949
950 static inline void llog_restore_resource(const struct cred *old_cred)
951 {
952         if (old_cred)
953                 revert_creds(old_cred);
954 }
955
956 int llog_reverse_process(const struct lu_env *env,
957                          struct llog_handle *loghandle, llog_cb_t cb,
958                          void *data, void *catdata)
959 {
960         struct llog_log_hdr *llh = loghandle->lgh_hdr;
961         struct llog_process_cat_data *cd = catdata;
962         void *buf;
963         int rc = 0, first_index = 1, index, idx;
964         __u32   chunk_size = llh->llh_hdr.lrh_len;
965         ENTRY;
966
967         OBD_ALLOC_LARGE(buf, chunk_size);
968         if (buf == NULL)
969                 RETURN(-ENOMEM);
970
971         if (cd != NULL)
972                 first_index = cd->lpcd_first_idx + 1;
973         if (cd != NULL && cd->lpcd_last_idx)
974                 index = cd->lpcd_last_idx;
975         else
976                 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
977
978         while (rc == 0) {
979                 struct llog_rec_hdr *rec;
980                 struct llog_rec_tail *tail;
981
982                 /* skip records not set in bitmap */
983                 while (index >= first_index &&
984                        llog_is_index_skipable(index, llh, cd))
985                         --index;
986
987                 LASSERT(index >= first_index - 1);
988                 if (index == first_index - 1)
989                         break;
990
991                 /* get the buf with our target record; avoid old garbage */
992                 memset(buf, 0, chunk_size);
993                 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
994                 if (rc)
995                         GOTO(out, rc);
996
997                 rec = buf;
998                 idx = rec->lrh_index;
999                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
1000                 while (idx < index) {
1001                         rec = (void *)rec + rec->lrh_len;
1002                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1003                                 lustre_swab_llog_rec(rec);
1004                         idx ++;
1005                 }
1006                 LASSERT(idx == index);
1007                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
1008
1009                 /* process records in buffer, starting where we found one */
1010                 while ((void *)tail > buf) {
1011                         if (tail->lrt_index == 0)
1012                                 GOTO(out, rc = 0); /* no more records */
1013
1014                         /* if needed, process the callback on this record */
1015                         if (!llog_is_index_skipable(index, llh, cd)) {
1016                                 rec = (void *)tail - tail->lrt_len +
1017                                       sizeof(*tail);
1018
1019                                 rc = cb(env, loghandle, rec, data);
1020                                 if (rc == LLOG_PROC_BREAK ||
1021                                     rc == LLOG_SKIP_PLAIN) {
1022                                         GOTO(out, rc);
1023                                 } else if (rc == LLOG_DEL_RECORD) {
1024                                         rc = llog_cancel_rec(env, loghandle,
1025                                                              tail->lrt_index);
1026                                 }
1027                                 if (rc)
1028                                         GOTO(out, rc);
1029                         }
1030
1031                         /* previous record, still in buffer? */
1032                         --index;
1033                         if (index < first_index)
1034                                 GOTO(out, rc = 0);
1035                         tail = (void *)tail - tail->lrt_len;
1036                 }
1037         }
1038
1039 out:
1040         if (buf != NULL)
1041                 OBD_FREE_LARGE(buf, chunk_size);
1042         RETURN(rc);
1043 }
1044 EXPORT_SYMBOL(llog_reverse_process);
1045
1046 /**
1047  * new llog API
1048  *
1049  * API functions:
1050  *      llog_open - open llog, may not exist
1051  *      llog_exist - check if llog exists
1052  *      llog_close - close opened llog, pair for open, frees llog_handle
1053  *      llog_declare_create - declare llog creation
1054  *      llog_create - create new llog on disk, need transaction handle
1055  *      llog_declare_write_rec - declaration of llog write
1056  *      llog_write_rec - write llog record on disk, need transaction handle
1057  *      llog_declare_add - declare llog catalog record addition
1058  *      llog_add - add llog record in catalog, need transaction handle
1059  */
1060 int llog_exist(struct llog_handle *loghandle)
1061 {
1062         const struct llog_operations *lop;
1063         int rc;
1064
1065         ENTRY;
1066
1067         rc = llog_handle2ops(loghandle, &lop);
1068         if (rc)
1069                 RETURN(rc);
1070         if (lop->lop_exist == NULL)
1071                 RETURN(-EOPNOTSUPP);
1072
1073         rc = lop->lop_exist(loghandle);
1074         RETURN(rc);
1075 }
1076 EXPORT_SYMBOL(llog_exist);
1077
1078 int llog_declare_create(const struct lu_env *env,
1079                         struct llog_handle *loghandle, struct thandle *th)
1080 {
1081         const struct cred *old_cred;
1082         const struct llog_operations *lop;
1083         int rc;
1084
1085         ENTRY;
1086
1087         rc = llog_handle2ops(loghandle, &lop);
1088         if (rc)
1089                 RETURN(rc);
1090         if (lop->lop_declare_create == NULL)
1091                 RETURN(-EOPNOTSUPP);
1092
1093         old_cred = llog_raise_resource();
1094         rc = lop->lop_declare_create(env, loghandle, th);
1095         llog_restore_resource(old_cred);
1096         RETURN(rc);
1097 }
1098
1099 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1100                 struct thandle *th)
1101 {
1102         const struct cred *old_cred;
1103         const struct llog_operations *lop;
1104         int rc;
1105
1106         ENTRY;
1107
1108         rc = llog_handle2ops(handle, &lop);
1109         if (rc)
1110                 RETURN(rc);
1111         if (lop->lop_create == NULL)
1112                 RETURN(-EOPNOTSUPP);
1113
1114         old_cred = llog_raise_resource();
1115         rc = lop->lop_create(env, handle, th);
1116         llog_restore_resource(old_cred);
1117         RETURN(rc);
1118 }
1119
1120 int llog_declare_write_rec(const struct lu_env *env,
1121                            struct llog_handle *handle,
1122                            struct llog_rec_hdr *rec, int idx,
1123                            struct thandle *th)
1124 {
1125         const struct cred *old_cred;
1126         const struct llog_operations *lop;
1127         int rc;
1128
1129         ENTRY;
1130
1131         rc = llog_handle2ops(handle, &lop);
1132         if (rc)
1133                 RETURN(rc);
1134         LASSERT(lop);
1135         if (lop->lop_declare_write_rec == NULL)
1136                 RETURN(-EOPNOTSUPP);
1137
1138         old_cred = llog_raise_resource();
1139         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1140         llog_restore_resource(old_cred);
1141         RETURN(rc);
1142 }
1143
1144 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1145                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1146                    int idx, struct thandle *th)
1147 {
1148         const struct cred *old_cred;
1149         const struct llog_operations *lop;
1150         int rc, buflen;
1151
1152         ENTRY;
1153
1154         /* API sanity checks */
1155         if (handle == NULL) {
1156                 CERROR("loghandle is missed\n");
1157                 RETURN(-EPROTO);
1158         } else if (handle->lgh_obj == NULL) {
1159                 CERROR("loghandle %p with NULL object\n",
1160                         handle);
1161                 RETURN(-EPROTO);
1162         } else if (th == NULL) {
1163                 CERROR("%s: missed transaction handle\n",
1164                        loghandle2name(handle));
1165                 RETURN(-EPROTO);
1166         } else if (handle->lgh_hdr == NULL) {
1167                 CERROR("%s: loghandle %p with no header\n",
1168                        loghandle2name(handle), handle);
1169                 RETURN(-EPROTO);
1170         }
1171
1172         rc = llog_handle2ops(handle, &lop);
1173         if (rc)
1174                 RETURN(rc);
1175
1176         if (lop->lop_write_rec == NULL)
1177                 RETURN(-EOPNOTSUPP);
1178
1179         buflen = rec->lrh_len;
1180         LASSERT(cfs_size_round(buflen) == buflen);
1181
1182         old_cred = llog_raise_resource();
1183         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1184         llog_restore_resource(old_cred);
1185         RETURN(rc);
1186 }
1187
1188 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1189              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1190              struct thandle *th)
1191 {
1192         const struct cred *old_cred;
1193         int rc;
1194
1195         ENTRY;
1196
1197         if (lgh->lgh_logops->lop_add == NULL)
1198                 RETURN(-EOPNOTSUPP);
1199
1200         old_cred = llog_raise_resource();
1201         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1202         llog_restore_resource(old_cred);
1203         RETURN(rc);
1204 }
1205 EXPORT_SYMBOL(llog_add);
1206
1207 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1208                      struct llog_rec_hdr *rec, struct thandle *th)
1209 {
1210         const struct cred *old_cred;
1211         int rc;
1212
1213         ENTRY;
1214
1215         if (lgh->lgh_logops->lop_declare_add == NULL)
1216                 RETURN(-EOPNOTSUPP);
1217
1218         old_cred = llog_raise_resource();
1219         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1220         llog_restore_resource(old_cred);
1221         RETURN(rc);
1222 }
1223 EXPORT_SYMBOL(llog_declare_add);
1224
1225 /**
1226  * Helper function to open llog or create it if doesn't exist.
1227  * It hides all transaction handling from caller.
1228  */
1229 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1230                      struct llog_handle **res, struct llog_logid *logid,
1231                      char *name)
1232 {
1233         struct dt_device        *d;
1234         struct thandle          *th;
1235         int                      rc;
1236
1237         ENTRY;
1238
1239         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1240         if (rc)
1241                 RETURN(rc);
1242
1243         if (llog_exist(*res))
1244                 RETURN(0);
1245
1246         LASSERT((*res)->lgh_obj != NULL);
1247
1248         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1249
1250         if (unlikely(unlikely(d->dd_rdonly)))
1251                 RETURN(-EROFS);
1252
1253         th = dt_trans_create(env, d);
1254         if (IS_ERR(th))
1255                 GOTO(out, rc = PTR_ERR(th));
1256
1257         /* Create update llog object synchronously, which
1258          * happens during inialization process see
1259          * lod_sub_prep_llog(), to make sure the update
1260          * llog object is created before corss-MDT writing
1261          * updates into the llog object */
1262         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1263                 th->th_sync = 1;
1264
1265         th->th_wait_submit = 1;
1266         rc = llog_declare_create(env, *res, th);
1267         if (rc == 0) {
1268                 rc = dt_trans_start_local(env, d, th);
1269                 if (rc == 0)
1270                         rc = llog_create(env, *res, th);
1271         }
1272         dt_trans_stop(env, d, th);
1273 out:
1274         if (rc)
1275                 llog_close(env, *res);
1276         RETURN(rc);
1277 }
1278 EXPORT_SYMBOL(llog_open_create);
1279
1280 /**
1281  * Helper function to delete existent llog.
1282  */
1283 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1284                struct llog_logid *logid, char *name)
1285 {
1286         struct llog_handle      *handle;
1287         int                      rc = 0, rc2;
1288
1289         ENTRY;
1290
1291         /* nothing to erase */
1292         if (name == NULL && logid == NULL)
1293                 RETURN(0);
1294
1295         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1296         if (rc < 0)
1297                 RETURN(rc);
1298
1299         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1300         if (rc == 0)
1301                 rc = llog_destroy(env, handle);
1302
1303         rc2 = llog_close(env, handle);
1304         if (rc == 0)
1305                 rc = rc2;
1306         RETURN(rc);
1307 }
1308 EXPORT_SYMBOL(llog_erase);
1309
1310 /*
1311  * Helper function for write record in llog.
1312  * It hides all transaction handling from caller.
1313  * Valid only with local llog.
1314  */
1315 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1316                struct llog_rec_hdr *rec, int idx)
1317 {
1318         struct dt_device        *dt;
1319         struct thandle          *th;
1320         bool                    need_cookie;
1321         int                     rc;
1322
1323         ENTRY;
1324
1325         LASSERT(loghandle);
1326         LASSERT(loghandle->lgh_ctxt);
1327         LASSERT(loghandle->lgh_obj != NULL);
1328
1329         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1330
1331         if (unlikely(unlikely(dt->dd_rdonly)))
1332                 RETURN(-EROFS);
1333
1334         th = dt_trans_create(env, dt);
1335         if (IS_ERR(th))
1336                 RETURN(PTR_ERR(th));
1337
1338         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1339         if (rc)
1340                 GOTO(out_trans, rc);
1341
1342         th->th_wait_submit = 1;
1343         rc = dt_trans_start_local(env, dt, th);
1344         if (rc)
1345                 GOTO(out_trans, rc);
1346
1347         need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1348
1349         down_write(&loghandle->lgh_lock);
1350         if (need_cookie) {
1351                 struct llog_thread_info *lti = llog_info(env);
1352
1353                 /* cookie comes from llog_process_thread */
1354                 rc = llog_write_rec(env, loghandle, rec, &lti->lgi_cookie,
1355                                     rec->lrh_index, th);
1356                 /* upper layer didn`t pass cookie so change rc */
1357                 rc = (rc == 1 ? 0 : rc);
1358         } else {
1359                 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1360         }
1361
1362         up_write(&loghandle->lgh_lock);
1363 out_trans:
1364         dt_trans_stop(env, dt, th);
1365         RETURN(rc);
1366 }
1367 EXPORT_SYMBOL(llog_write);
1368
1369 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1370               struct llog_handle **lgh, struct llog_logid *logid,
1371               char *name, enum llog_open_param open_param)
1372 {
1373         const struct cred *old_cred;
1374         int      rc;
1375
1376         ENTRY;
1377
1378         LASSERT(ctxt);
1379         LASSERT(ctxt->loc_logops);
1380
1381         if (ctxt->loc_logops->lop_open == NULL) {
1382                 *lgh = NULL;
1383                 RETURN(-EOPNOTSUPP);
1384         }
1385
1386         *lgh = llog_alloc_handle();
1387         if (*lgh == NULL)
1388                 RETURN(-ENOMEM);
1389         (*lgh)->lgh_ctxt = ctxt;
1390         (*lgh)->lgh_logops = ctxt->loc_logops;
1391
1392         old_cred = llog_raise_resource();
1393         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1394         llog_restore_resource(old_cred);
1395         if (rc) {
1396                 llog_free_handle(*lgh);
1397                 *lgh = NULL;
1398         }
1399         RETURN(rc);
1400 }
1401 EXPORT_SYMBOL(llog_open);
1402
1403 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1404 {
1405         return llog_handle_put(env, loghandle);
1406 }
1407 EXPORT_SYMBOL(llog_close);
1408
1409 /**
1410  * Helper function to get the llog size in records. It is used by MGS
1411  * mostly to check that config llog exists and contains data.
1412  *
1413  * \param[in] env       execution environment
1414  * \param[in] ctxt      llog context
1415  * \param[in] name      llog name
1416  *
1417  * \retval              true if there are records in llog besides a header
1418  * \retval              false on error or llog without records
1419  */
1420 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1421                   char *name)
1422 {
1423         struct llog_handle      *llh;
1424         int                      rc = 0;
1425
1426         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1427         if (rc < 0) {
1428                 if (likely(rc == -ENOENT))
1429                         rc = 0;
1430                 GOTO(out, rc);
1431         }
1432
1433         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1434         if (rc)
1435                 GOTO(out_close, rc);
1436         rc = llog_get_size(llh);
1437
1438 out_close:
1439         llog_close(env, llh);
1440 out:
1441         /* The header is record 1, the llog is still considered as empty
1442          * if there is only header */
1443         return (rc <= 1);
1444 }
1445 EXPORT_SYMBOL(llog_is_empty);
1446
1447 /* this callback run in raw read mode (canceled record are processed) */
1448 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1449                       struct llog_rec_hdr *rec, void *data)
1450 {
1451         struct llog_handle *copy_llh = data;
1452         int idx = rec->lrh_index;
1453         int rc;
1454
1455         ENTRY;
1456
1457         /* Append all records */
1458         rc = llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1459
1460         /* Cancel the record if it is canceled on the source */
1461         if (!rc && !test_bit_le(idx, LLOG_HDR_BITMAP(llh->lgh_hdr)))
1462                 rc = llog_cancel_rec(env, copy_llh, copy_llh->lgh_last_idx);
1463
1464         RETURN(rc);
1465 }
1466
1467 /* backup plain llog */
1468 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1469                 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1470                 char *name, char *backup)
1471 {
1472         struct llog_handle *llh, *bllh;
1473         struct llog_process_cat_data cd = {0};
1474         int rc;
1475
1476         ENTRY;
1477
1478         /* open original log */
1479         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1480         if (rc < 0) {
1481                 /* the -ENOENT case is also reported to the caller
1482                  * but silently so it should handle that if needed.
1483                  */
1484                 if (rc != -ENOENT)
1485                         CERROR("%s: failed to open log %s: rc = %d\n",
1486                                obd->obd_name, name, rc);
1487                 RETURN(rc);
1488         }
1489
1490         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1491         if (rc)
1492                 GOTO(out_close, rc);
1493
1494         /* Make sure there's no old backup log */
1495         rc = llog_erase(env, bctxt, NULL, backup);
1496         if (rc < 0 && rc != -ENOENT)
1497                 GOTO(out_close, rc);
1498
1499         /* open backup log */
1500         rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1501         if (rc) {
1502                 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1503                        obd->obd_name, backup, rc);
1504                 GOTO(out_close, rc);
1505         }
1506
1507         /* check that backup llog is not the same object as original one */
1508         if (llh->lgh_obj == bllh->lgh_obj) {
1509                 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1510                        obd->obd_name, name, backup, llh->lgh_obj,
1511                        bllh->lgh_obj);
1512                 GOTO(out_backup, rc = -EEXIST);
1513         }
1514
1515         rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1516         if (rc)
1517                 GOTO(out_backup, rc);
1518
1519         /* Read canceled records to have an exact copy */
1520         cd.lpcd_read_mode = LLOG_READ_MODE_RAW;
1521         /* Copy log record by record */
1522         rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1523                                   &cd, false);
1524         if (rc)
1525                 CERROR("%s: failed to backup log %s: rc = %d\n",
1526                        obd->obd_name, name, rc);
1527 out_backup:
1528         llog_close(env, bllh);
1529 out_close:
1530         llog_close(env, llh);
1531         RETURN(rc);
1532 }
1533 EXPORT_SYMBOL(llog_backup);
1534
1535 /* Get size of llog */
1536 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1537 {
1538         int rc;
1539         struct lu_attr la;
1540
1541         rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1542         if (rc) {
1543                 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1544                        loghandle2name(llh), PLOGID(&llh->lgh_id), rc);
1545                 return 0;
1546         }
1547
1548         return la.la_size;
1549 }
1550 EXPORT_SYMBOL(llog_size);
1551