Whamcloud - gitweb
LU-13102 llog: fix processing of a wrapped catalog
[fs/lustre-release.git] / lustre / obdclass / llog.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  * Lustre is a trademark of Sun Microsystems, Inc.
31  *
32  * lustre/obdclass/llog.c
33  *
34  * OST<->MDS recovery logging infrastructure.
35  * Invariants in implementation:
36  * - we do not share logs among different OST<->MDS connections, so that
37  *   if an OST or MDS fails it need only look at log(s) relevant to itself
38  *
39  * Author: Andreas Dilger <adilger@clusterfs.com>
40  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
41  * Author: Mikhail Pershin <tappro@whamcloud.com>
42  */
43
44 #define DEBUG_SUBSYSTEM S_LOG
45
46 #include <linux/pid_namespace.h>
47 #include <linux/kthread.h>
48 #include <llog_swab.h>
49 #include <lustre_log.h>
50 #include <obd_support.h>
51 #include <obd_class.h>
52 #include "llog_internal.h"
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                 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         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         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         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                                loghandle->lgh_ctxt->loc_obd->obd_name,
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         mutex_unlock(&loghandle->lgh_hdr_mutex);
316         up_write(&loghandle->lgh_lock);
317 out_trans:
318         rc1 = dt_trans_stop(env, dt, th);
319         if (rc == 0)
320                 rc = rc1;
321         if (rc < 0) {
322                 mutex_lock(&loghandle->lgh_hdr_mutex);
323                 if (subtract_count)
324                         loghandle->lgh_hdr->llh_count += num;
325                 for (i = i - 1; i >= 0; i--)
326                         set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
327                 mutex_unlock(&loghandle->lgh_hdr_mutex);
328         }
329         RETURN(rc);
330 }
331
332 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
333                     int index)
334 {
335         return llog_cancel_arr_rec(env, loghandle, 1, &index);
336 }
337
338 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
339                      const struct obd_uuid *uuid)
340 {
341         struct llog_operations *lop;
342         int rc;
343         ENTRY;
344
345         rc = llog_handle2ops(handle, &lop);
346         if (rc)
347                 RETURN(rc);
348
349         if (lop->lop_read_header == NULL)
350                 RETURN(-EOPNOTSUPP);
351
352         rc = lop->lop_read_header(env, handle);
353         if (rc == LLOG_EEMPTY) {
354                 struct llog_log_hdr *llh = handle->lgh_hdr;
355
356                 /* lrh_len should be initialized in llog_init_handle */
357                 handle->lgh_last_idx = 0; /* header is record with index 0 */
358                 llh->llh_count = 1;         /* for the header record */
359                 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
360                 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
361                                                 LLOG_MIN_CHUNK_SIZE);
362                 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
363                 llh->llh_hdr.lrh_index = 0;
364                 llh->llh_timestamp = ktime_get_real_seconds();
365                 if (uuid)
366                         memcpy(&llh->llh_tgtuuid, uuid,
367                                sizeof(llh->llh_tgtuuid));
368                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
369                 /* Since update llog header might also call this function,
370                  * let's reset the bitmap to 0 here */
371                 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
372                                                 llh->llh_bitmap_offset -
373                                                 sizeof(llh->llh_tail));
374                 set_bit_le(0, LLOG_HDR_BITMAP(llh));
375                 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
376                 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
377                 rc = 0;
378         }
379         RETURN(rc);
380 }
381 EXPORT_SYMBOL(llog_read_header);
382
383 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
384                      int flags, struct obd_uuid *uuid)
385 {
386         struct llog_log_hdr     *llh;
387         enum llog_flag           fmt = flags & LLOG_F_EXT_MASK;
388         int                      rc;
389         int                     chunk_size = handle->lgh_ctxt->loc_chunk_size;
390         ENTRY;
391
392         LASSERT(handle->lgh_hdr == NULL);
393
394         LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
395         OBD_ALLOC_LARGE(llh, chunk_size);
396         if (llh == NULL)
397                 RETURN(-ENOMEM);
398
399         handle->lgh_hdr = llh;
400         handle->lgh_hdr_size = chunk_size;
401         /* first assign flags to use llog_client_ops */
402         llh->llh_flags = flags;
403         rc = llog_read_header(env, handle, uuid);
404         if (rc == 0) {
405                 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
406                               flags & LLOG_F_IS_CAT) ||
407                              (llh->llh_flags & LLOG_F_IS_CAT &&
408                               flags & LLOG_F_IS_PLAIN))) {
409                         CERROR("%s: llog type is %s but initializing %s\n",
410                                handle->lgh_ctxt->loc_obd->obd_name,
411                                llh->llh_flags & LLOG_F_IS_CAT ?
412                                "catalog" : "plain",
413                                flags & LLOG_F_IS_CAT ? "catalog" : "plain");
414                         GOTO(out, rc = -EINVAL);
415                 } else if (llh->llh_flags &
416                            (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
417                         /*
418                          * it is possible to open llog without specifying llog
419                          * type so it is taken from llh_flags
420                          */
421                         flags = llh->llh_flags;
422                 } else {
423                         /* for some reason the llh_flags has no type set */
424                         CERROR("llog type is not specified!\n");
425                         GOTO(out, rc = -EINVAL);
426                 }
427                 if (unlikely(uuid &&
428                              !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
429                         CERROR("%s: llog uuid mismatch: %s/%s\n",
430                                handle->lgh_ctxt->loc_obd->obd_name,
431                                (char *)uuid->uuid,
432                                (char *)llh->llh_tgtuuid.uuid);
433                         GOTO(out, rc = -EEXIST);
434                 }
435         }
436         if (flags & LLOG_F_IS_CAT) {
437                 LASSERT(list_empty(&handle->u.chd.chd_head));
438                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
439                 llh->llh_size = sizeof(struct llog_logid_rec);
440                 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
441         } else if (!(flags & LLOG_F_IS_PLAIN)) {
442                 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
443                        handle->lgh_ctxt->loc_obd->obd_name,
444                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
445                 rc = -EINVAL;
446         }
447         llh->llh_flags |= fmt;
448 out:
449         if (rc) {
450                 OBD_FREE_LARGE(llh, chunk_size);
451                 handle->lgh_hdr = NULL;
452         }
453         RETURN(rc);
454 }
455 EXPORT_SYMBOL(llog_init_handle);
456
457 static int llog_process_thread(void *arg)
458 {
459         struct llog_process_info        *lpi = arg;
460         struct llog_handle              *loghandle = lpi->lpi_loghandle;
461         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
462         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
463         struct llog_thread_info         *lti;
464         char                            *buf;
465         size_t                           chunk_size;
466         __u64                            cur_offset;
467         int                              rc = 0, index = 1, last_index;
468         int                              saved_index = 0;
469         int                              last_called_index = 0;
470         bool                             repeated = false;
471
472         ENTRY;
473
474         if (llh == NULL)
475                 RETURN(-EINVAL);
476
477         lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
478
479         cur_offset = chunk_size = llh->llh_hdr.lrh_len;
480         /* expect chunk_size to be power of two */
481         LASSERT(is_power_of_2(chunk_size));
482
483         OBD_ALLOC_LARGE(buf, chunk_size);
484         if (buf == NULL) {
485                 lpi->lpi_rc = -ENOMEM;
486                 RETURN(0);
487         }
488
489         if (cd != NULL) {
490                 last_called_index = cd->lpcd_first_idx;
491                 index = cd->lpcd_first_idx + 1;
492         }
493         if (cd != NULL && cd->lpcd_last_idx)
494                 last_index = cd->lpcd_last_idx;
495         else
496                 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
497
498         while (rc == 0) {
499                 struct llog_rec_hdr *rec;
500                 off_t chunk_offset = 0;
501                 unsigned int buf_offset = 0;
502                 bool partial_chunk;
503                 int     lh_last_idx;
504                 int     synced_idx = 0;
505
506                 /* skip records not set in bitmap */
507                 while (index <= last_index &&
508                        !test_bit_le(index, LLOG_HDR_BITMAP(llh)))
509                         ++index;
510
511                 /* There are no indices prior the last_index */
512                 if (index > last_index)
513                         break;
514
515                 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
516                        last_index);
517
518 repeat:
519                 /* get the buf with our target record; avoid old garbage */
520                 memset(buf, 0, chunk_size);
521                 /* the record index for outdated chunk data */
522                 /* it is safe to process buffer until saved lgh_last_idx */
523                 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
524                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
525                                      index, &cur_offset, buf, chunk_size);
526                 if (repeated && rc)
527                         CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
528                                " buf_offset %u, rc = %d\n", cur_offset,
529                                (__u64)chunk_offset, buf_offset, rc);
530                 /* we`ve tried to reread the chunk, but there is no
531                  * new records */
532                 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
533                     cur_offset)
534                         GOTO(out, rc = 0);
535                 if (rc != 0)
536                         GOTO(out, rc);
537
538                 /* NB: after llog_next_block() call the cur_offset is the
539                  * offset of the next block after read one.
540                  * The absolute offset of the current chunk is calculated
541                  * from cur_offset value and stored in chunk_offset variable.
542                  */
543                 if ((cur_offset & (chunk_size - 1)) != 0) {
544                         partial_chunk = true;
545                         chunk_offset = cur_offset & ~(chunk_size - 1);
546                 } else {
547                         partial_chunk = false;
548                         chunk_offset = cur_offset - chunk_size;
549                 }
550
551                 /* NB: when rec->lrh_len is accessed it is already swabbed
552                  * since it is used at the "end" of the loop and the rec
553                  * swabbing is done at the beginning of the loop. */
554                 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
555                      (char *)rec < buf + chunk_size;
556                      rec = llog_rec_hdr_next(rec)) {
557
558                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
559                                rec, rec->lrh_type);
560
561                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
562                                 lustre_swab_llog_rec(rec);
563
564                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
565                                rec->lrh_type, rec->lrh_index);
566
567                         if (index == (synced_idx + 1) &&
568                             synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
569                                 GOTO(out, rc = 0);
570
571                         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
572                                 cfs_fail_val == (unsigned int)
573                                         (loghandle->lgh_id.lgl_oi.oi.oi_id &
574                                          0xFFFFFFFF)) {
575                                 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
576                         }
577
578                         /* the bitmap could be changed during processing
579                          * records from the chunk. For wrapped catalog
580                          * it means we can read deleted record and try to
581                          * process it. Check this case and reread the chunk.
582                          * It is safe to process to lh_last_idx, including
583                          * lh_last_idx if it was synced. We can not do <=
584                          * comparison, cause for wrapped catalog lgh_last_idx
585                          * could be less than index. So we detect last index
586                          * for processing as index == lh_last_idx+1. But when
587                          * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
588                          * the first processing index is llh_cat_idx+1.The
589                          * exception is !(lgh_last_idx == llh_cat_idx &&
590                          * index == llh_cat_idx + 1), and after simplification
591                          * it turns to
592                          * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
593                          * This exception is working for catalog only.
594                          */
595
596                         if ((index == lh_last_idx && synced_idx != index) ||
597                             (index == (lh_last_idx + 1) &&
598                              lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
599                             (rec->lrh_index == 0 && !repeated)) {
600
601                                 /* save offset inside buffer for the re-read */
602                                 buf_offset = (char *)rec - (char *)buf;
603                                 cur_offset = chunk_offset;
604                                 repeated = true;
605                                 /* We need to be sure lgh_last_idx
606                                  * record was saved to disk
607                                  */
608                                 down_read(&loghandle->lgh_last_sem);
609                                 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
610                                 up_read(&loghandle->lgh_last_sem);
611                                 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
612                                 goto repeat;
613
614                         }
615
616                         repeated = false;
617
618                         if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
619                                 CWARN("%s: invalid length %d in llog "DFID
620                                       "record for index %d/%d\n",
621                                        loghandle->lgh_ctxt->loc_obd->obd_name,
622                                        rec->lrh_len,
623                                        PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
624                                        rec->lrh_index, index);
625
626                                 GOTO(out, rc = -EINVAL);
627                         }
628
629                         if (rec->lrh_index < index) {
630                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
631                                        rec->lrh_index);
632                                 continue;
633                         }
634
635                         if (rec->lrh_index != index) {
636                                 CERROR("%s: "DFID" Invalid record: index %u"
637                                        " but expected %u\n",
638                                        loghandle->lgh_ctxt->loc_obd->obd_name,
639                                        PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
640                                        rec->lrh_index, index);
641                                 GOTO(out, rc = -ERANGE);
642                         }
643
644                         CDEBUG(D_OTHER,
645                                "lrh_index: %d lrh_len: %d (%d remains)\n",
646                                rec->lrh_index, rec->lrh_len,
647                                (int)(buf + chunk_size - (char *)rec));
648
649                         /* lgh_cur_offset is used only at llog_test_3 */
650                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
651                                                     chunk_offset;
652
653                         /* if set, process the callback on this record */
654                         if (test_bit_le(index, LLOG_HDR_BITMAP(llh))) {
655                                 struct llog_cookie *lgc;
656                                 __u64   tmp_off;
657                                 int     tmp_idx;
658
659                                 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
660                                         D_HA : D_OTHER),
661                                        "index: %d, lh_last_idx: %d "
662                                        "synced_idx: %d lgh_last_idx: %d\n",
663                                        index, lh_last_idx, synced_idx,
664                                        loghandle->lgh_last_idx);
665
666                                 if (lti != NULL) {
667                                         lgc = &lti->lgi_cookie;
668                                         /* store lu_env for recursive calls */
669                                         tmp_off = lgc->lgc_offset;
670                                         tmp_idx = lgc->lgc_index;
671
672                                         lgc->lgc_offset = (char *)rec -
673                                                 (char *)buf + chunk_offset;
674                                         lgc->lgc_index = rec->lrh_index;
675                                 }
676                                 /* using lu_env for passing record offset to
677                                  * llog_write through various callbacks */
678                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
679                                                  lpi->lpi_cbdata);
680                                 last_called_index = index;
681
682                                 if (lti != NULL) {
683                                         lgc->lgc_offset = tmp_off;
684                                         lgc->lgc_index = tmp_idx;
685                                 }
686
687                                 if (rc == LLOG_PROC_BREAK) {
688                                         GOTO(out, rc);
689                                 } else if (rc == LLOG_DEL_RECORD) {
690                                         rc = llog_cancel_rec(lpi->lpi_env,
691                                                              loghandle,
692                                                              rec->lrh_index);
693                                 }
694                                 if (rc)
695                                         GOTO(out, rc);
696                                 /* some stupid callbacks directly cancel records
697                                  * and delete llog. Check it and stop
698                                  * processing. */
699                                 if (loghandle->lgh_hdr == NULL ||
700                                     loghandle->lgh_hdr->llh_count == 1)
701                                         GOTO(out, rc = 0);
702                         }
703                         /* exit if the last index is reached */
704                         if (index >= last_index)
705                                 GOTO(out, rc = 0);
706                         ++index;
707                 }
708         }
709
710 out:
711         CDEBUG(D_HA, "stop processing %s "DOSTID":%x index %d count %d\n",
712                ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
713                POSTID(&loghandle->lgh_id.lgl_oi), loghandle->lgh_id.lgl_ogen,
714                index, llh->llh_count);
715
716         if (cd != NULL)
717                 cd->lpcd_last_idx = last_called_index;
718
719         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
720                 if (dt_object_remote(loghandle->lgh_obj)) {
721                         /* If it is remote object, then -EIO might means
722                          * disconnection or eviction, let's return -EAGAIN,
723                          * so for update recovery log processing, it will
724                          * retry until the umount or abort recovery, see
725                          * lod_sub_recovery_thread() */
726                         CERROR("%s retry remote llog process\n",
727                                loghandle->lgh_ctxt->loc_obd->obd_name);
728                         rc = -EAGAIN;
729                 } else {
730                         /* something bad happened to the processing of a local
731                          * llog file, probably I/O error or the log got
732                          * corrupted to be able to finally release the log we
733                          * discard any remaining bits in the header */
734                         CERROR("%s: Local llog found corrupted #"DOSTID":%x"
735                                " %s index %d count %d\n",
736                                loghandle->lgh_ctxt->loc_obd->obd_name,
737                                POSTID(&loghandle->lgh_id.lgl_oi),
738                                loghandle->lgh_id.lgl_ogen,
739                                ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
740                                 "plain"), index, llh->llh_count);
741
742                         while (index <= last_index) {
743                                 if (test_bit_le(index,
744                                                   LLOG_HDR_BITMAP(llh)) != 0)
745                                         llog_cancel_rec(lpi->lpi_env, loghandle,
746                                                         index);
747                                 index++;
748                         }
749                         rc = 0;
750                 }
751         }
752
753         OBD_FREE_LARGE(buf, chunk_size);
754         lpi->lpi_rc = rc;
755         return 0;
756 }
757
758 static int llog_process_thread_daemonize(void *arg)
759 {
760         struct llog_process_info        *lpi = arg;
761         struct lu_env                    env;
762         int                              rc;
763         struct nsproxy                  *new_ns, *curr_ns = current->nsproxy;
764
765         task_lock(lpi->lpi_reftask);
766         new_ns = lpi->lpi_reftask->nsproxy;
767         if (curr_ns != new_ns) {
768                 get_nsproxy(new_ns);
769
770                 current->nsproxy = new_ns;
771                 /* XXX: we should call put_nsproxy() instead of
772                  * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
773                  * used outside of the kernel itself, because it calls
774                  * free_nsproxy() which is not exported by the kernel
775                  * (defined in kernel/nsproxy.c) */
776                 if (curr_ns)
777                         atomic_dec(&curr_ns->count);
778         }
779         task_unlock(lpi->lpi_reftask);
780
781         unshare_fs_struct();
782
783         /* client env has no keys, tags is just 0 */
784         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
785         if (rc)
786                 goto out;
787         lpi->lpi_env = &env;
788
789         rc = llog_process_thread(arg);
790
791         lu_env_fini(&env);
792 out:
793         complete(&lpi->lpi_completion);
794         return rc;
795 }
796
797 int llog_process_or_fork(const struct lu_env *env,
798                          struct llog_handle *loghandle,
799                          llog_cb_t cb, void *data, void *catdata, bool fork)
800 {
801         struct llog_process_info *lpi;
802         struct llog_process_data *d = data;
803         struct llog_process_cat_data *cd = catdata;
804         int                      rc;
805
806         ENTRY;
807
808         OBD_ALLOC_PTR(lpi);
809         if (lpi == NULL) {
810                 CERROR("cannot alloc pointer\n");
811                 RETURN(-ENOMEM);
812         }
813         lpi->lpi_loghandle = loghandle;
814         lpi->lpi_cb        = cb;
815         lpi->lpi_cbdata    = data;
816         lpi->lpi_catdata   = catdata;
817
818         CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d\n",
819                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
820                loghandle->lgh_hdr->llh_flags, d ? d->lpd_startcat : -1,
821                d ? d->lpd_startidx : -1, cd ? cd->lpcd_first_idx : -1,
822                cd ? cd->lpcd_last_idx : -1);
823         if (fork) {
824                 struct task_struct *task;
825
826                 /* The new thread can't use parent env,
827                  * init the new one in llog_process_thread_daemonize. */
828                 lpi->lpi_env = NULL;
829                 init_completion(&lpi->lpi_completion);
830                 /* take reference to current, so that
831                  * llog_process_thread_daemonize() can use it to switch to
832                  * namespace associated with current  */
833                 lpi->lpi_reftask = current;
834                 task = kthread_run(llog_process_thread_daemonize, lpi,
835                                    "llog_process_thread");
836                 if (IS_ERR(task)) {
837                         rc = PTR_ERR(task);
838                         CERROR("%s: cannot start thread: rc = %d\n",
839                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
840                         GOTO(out_lpi, rc);
841                 }
842                 wait_for_completion(&lpi->lpi_completion);
843         } else {
844                 lpi->lpi_env = env;
845                 llog_process_thread(lpi);
846         }
847         rc = lpi->lpi_rc;
848
849 out_lpi:
850         OBD_FREE_PTR(lpi);
851         RETURN(rc);
852 }
853 EXPORT_SYMBOL(llog_process_or_fork);
854
855 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
856                  llog_cb_t cb, void *data, void *catdata)
857 {
858         int rc;
859         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
860         return rc == LLOG_DEL_PLAIN ? 0 : rc;
861 }
862 EXPORT_SYMBOL(llog_process);
863
864 static inline const struct cred *llog_raise_resource(void)
865 {
866         struct cred *cred = NULL;
867
868         if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
869                 return cred;
870
871         cred = prepare_creds();
872         if (!cred)
873                 return cred;
874
875         cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
876         return override_creds(cred);
877 }
878
879 static inline void llog_restore_resource(const struct cred *old_cred)
880 {
881         if (old_cred)
882                 revert_creds(old_cred);
883 }
884
885 int llog_reverse_process(const struct lu_env *env,
886                          struct llog_handle *loghandle, llog_cb_t cb,
887                          void *data, void *catdata)
888 {
889         struct llog_log_hdr *llh = loghandle->lgh_hdr;
890         struct llog_process_cat_data *cd = catdata;
891         void *buf;
892         int rc = 0, first_index = 1, index, idx;
893         __u32   chunk_size = llh->llh_hdr.lrh_len;
894         ENTRY;
895
896         OBD_ALLOC_LARGE(buf, chunk_size);
897         if (buf == NULL)
898                 RETURN(-ENOMEM);
899
900         if (cd != NULL)
901                 first_index = cd->lpcd_first_idx + 1;
902         if (cd != NULL && cd->lpcd_last_idx)
903                 index = cd->lpcd_last_idx;
904         else
905                 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
906
907         while (rc == 0) {
908                 struct llog_rec_hdr *rec;
909                 struct llog_rec_tail *tail;
910
911                 /* skip records not set in bitmap */
912                 while (index >= first_index &&
913                        !test_bit_le(index, LLOG_HDR_BITMAP(llh)))
914                         --index;
915
916                 LASSERT(index >= first_index - 1);
917                 if (index == first_index - 1)
918                         break;
919
920                 /* get the buf with our target record; avoid old garbage */
921                 memset(buf, 0, chunk_size);
922                 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
923                 if (rc)
924                         GOTO(out, rc);
925
926                 rec = buf;
927                 idx = rec->lrh_index;
928                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
929                 while (idx < index) {
930                         rec = (void *)rec + rec->lrh_len;
931                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
932                                 lustre_swab_llog_rec(rec);
933                         idx ++;
934                 }
935                 LASSERT(idx == index);
936                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
937
938                 /* process records in buffer, starting where we found one */
939                 while ((void *)tail > buf) {
940                         if (tail->lrt_index == 0)
941                                 GOTO(out, rc = 0); /* no more records */
942
943                         /* if set, process the callback on this record */
944                         if (test_bit_le(index, LLOG_HDR_BITMAP(llh))) {
945                                 rec = (void *)tail - tail->lrt_len +
946                                       sizeof(*tail);
947
948                                 rc = cb(env, loghandle, rec, data);
949                                 if (rc == LLOG_PROC_BREAK) {
950                                         GOTO(out, rc);
951                                 } else if (rc == LLOG_DEL_RECORD) {
952                                         rc = llog_cancel_rec(env, loghandle,
953                                                              tail->lrt_index);
954                                 }
955                                 if (rc)
956                                         GOTO(out, rc);
957                         }
958
959                         /* previous record, still in buffer? */
960                         --index;
961                         if (index < first_index)
962                                 GOTO(out, rc = 0);
963                         tail = (void *)tail - tail->lrt_len;
964                 }
965         }
966
967 out:
968         if (buf != NULL)
969                 OBD_FREE_LARGE(buf, chunk_size);
970         RETURN(rc);
971 }
972 EXPORT_SYMBOL(llog_reverse_process);
973
974 /**
975  * new llog API
976  *
977  * API functions:
978  *      llog_open - open llog, may not exist
979  *      llog_exist - check if llog exists
980  *      llog_close - close opened llog, pair for open, frees llog_handle
981  *      llog_declare_create - declare llog creation
982  *      llog_create - create new llog on disk, need transaction handle
983  *      llog_declare_write_rec - declaration of llog write
984  *      llog_write_rec - write llog record on disk, need transaction handle
985  *      llog_declare_add - declare llog catalog record addition
986  *      llog_add - add llog record in catalog, need transaction handle
987  */
988 int llog_exist(struct llog_handle *loghandle)
989 {
990         struct llog_operations  *lop;
991         int                      rc;
992
993         ENTRY;
994
995         rc = llog_handle2ops(loghandle, &lop);
996         if (rc)
997                 RETURN(rc);
998         if (lop->lop_exist == NULL)
999                 RETURN(-EOPNOTSUPP);
1000
1001         rc = lop->lop_exist(loghandle);
1002         RETURN(rc);
1003 }
1004 EXPORT_SYMBOL(llog_exist);
1005
1006 int llog_declare_create(const struct lu_env *env,
1007                         struct llog_handle *loghandle, struct thandle *th)
1008 {
1009         const struct cred *old_cred;
1010         struct llog_operations  *lop;
1011         int rc;
1012
1013         ENTRY;
1014
1015         rc = llog_handle2ops(loghandle, &lop);
1016         if (rc)
1017                 RETURN(rc);
1018         if (lop->lop_declare_create == NULL)
1019                 RETURN(-EOPNOTSUPP);
1020
1021         old_cred = llog_raise_resource();
1022         rc = lop->lop_declare_create(env, loghandle, th);
1023         llog_restore_resource(old_cred);
1024         RETURN(rc);
1025 }
1026
1027 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1028                 struct thandle *th)
1029 {
1030         const struct cred *old_cred;
1031         struct llog_operations  *lop;
1032         int rc;
1033
1034         ENTRY;
1035
1036         rc = llog_handle2ops(handle, &lop);
1037         if (rc)
1038                 RETURN(rc);
1039         if (lop->lop_create == NULL)
1040                 RETURN(-EOPNOTSUPP);
1041
1042         old_cred = llog_raise_resource();
1043         rc = lop->lop_create(env, handle, th);
1044         llog_restore_resource(old_cred);
1045         RETURN(rc);
1046 }
1047
1048 int llog_declare_write_rec(const struct lu_env *env,
1049                            struct llog_handle *handle,
1050                            struct llog_rec_hdr *rec, int idx,
1051                            struct thandle *th)
1052 {
1053         const struct cred *old_cred;
1054         struct llog_operations  *lop;
1055         int rc;
1056
1057         ENTRY;
1058
1059         rc = llog_handle2ops(handle, &lop);
1060         if (rc)
1061                 RETURN(rc);
1062         LASSERT(lop);
1063         if (lop->lop_declare_write_rec == NULL)
1064                 RETURN(-EOPNOTSUPP);
1065
1066         old_cred = llog_raise_resource();
1067         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1068         llog_restore_resource(old_cred);
1069         RETURN(rc);
1070 }
1071
1072 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1073                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1074                    int idx, struct thandle *th)
1075 {
1076         const struct cred *old_cred;
1077         struct llog_operations  *lop;
1078         int rc, buflen;
1079
1080         ENTRY;
1081
1082         /* API sanity checks */
1083         if (handle == NULL) {
1084                 CERROR("loghandle is missed\n");
1085                 RETURN(-EPROTO);
1086         } else if (handle->lgh_obj == NULL) {
1087                 CERROR("loghandle %p with NULL object\n",
1088                         handle);
1089                 RETURN(-EPROTO);
1090         } else if (th == NULL) {
1091                 CERROR("%s: missed transaction handle\n",
1092                         handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name);
1093                 RETURN(-EPROTO);
1094         } else if (handle->lgh_hdr == NULL) {
1095                 CERROR("%s: loghandle %p with no header\n",
1096                         handle->lgh_obj->do_lu.lo_dev->ld_obd->obd_name,
1097                         handle);
1098                 RETURN(-EPROTO);
1099         }
1100
1101         rc = llog_handle2ops(handle, &lop);
1102         if (rc)
1103                 RETURN(rc);
1104
1105         if (lop->lop_write_rec == NULL)
1106                 RETURN(-EOPNOTSUPP);
1107
1108         buflen = rec->lrh_len;
1109         LASSERT(cfs_size_round(buflen) == buflen);
1110
1111         old_cred = llog_raise_resource();
1112         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1113         llog_restore_resource(old_cred);
1114         RETURN(rc);
1115 }
1116
1117 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1118              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1119              struct thandle *th)
1120 {
1121         const struct cred *old_cred;
1122         int rc;
1123
1124         ENTRY;
1125
1126         if (lgh->lgh_logops->lop_add == NULL)
1127                 RETURN(-EOPNOTSUPP);
1128
1129         old_cred = llog_raise_resource();
1130         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1131         llog_restore_resource(old_cred);
1132         RETURN(rc);
1133 }
1134 EXPORT_SYMBOL(llog_add);
1135
1136 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1137                      struct llog_rec_hdr *rec, struct thandle *th)
1138 {
1139         const struct cred *old_cred;
1140         int rc;
1141
1142         ENTRY;
1143
1144         if (lgh->lgh_logops->lop_declare_add == NULL)
1145                 RETURN(-EOPNOTSUPP);
1146
1147         old_cred = llog_raise_resource();
1148         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1149         llog_restore_resource(old_cred);
1150         RETURN(rc);
1151 }
1152 EXPORT_SYMBOL(llog_declare_add);
1153
1154 /**
1155  * Helper function to open llog or create it if doesn't exist.
1156  * It hides all transaction handling from caller.
1157  */
1158 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1159                      struct llog_handle **res, struct llog_logid *logid,
1160                      char *name)
1161 {
1162         struct dt_device        *d;
1163         struct thandle          *th;
1164         int                      rc;
1165
1166         ENTRY;
1167
1168         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1169         if (rc)
1170                 RETURN(rc);
1171
1172         if (llog_exist(*res))
1173                 RETURN(0);
1174
1175         LASSERT((*res)->lgh_obj != NULL);
1176
1177         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1178
1179         if (unlikely(unlikely(d->dd_rdonly)))
1180                 RETURN(-EROFS);
1181
1182         th = dt_trans_create(env, d);
1183         if (IS_ERR(th))
1184                 GOTO(out, rc = PTR_ERR(th));
1185
1186         /* Create update llog object synchronously, which
1187          * happens during inialization process see
1188          * lod_sub_prep_llog(), to make sure the update
1189          * llog object is created before corss-MDT writing
1190          * updates into the llog object */
1191         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1192                 th->th_sync = 1;
1193
1194         th->th_wait_submit = 1;
1195         rc = llog_declare_create(env, *res, th);
1196         if (rc == 0) {
1197                 rc = dt_trans_start_local(env, d, th);
1198                 if (rc == 0)
1199                         rc = llog_create(env, *res, th);
1200         }
1201         dt_trans_stop(env, d, th);
1202 out:
1203         if (rc)
1204                 llog_close(env, *res);
1205         RETURN(rc);
1206 }
1207 EXPORT_SYMBOL(llog_open_create);
1208
1209 /**
1210  * Helper function to delete existent llog.
1211  */
1212 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1213                struct llog_logid *logid, char *name)
1214 {
1215         struct llog_handle      *handle;
1216         int                      rc = 0, rc2;
1217
1218         ENTRY;
1219
1220         /* nothing to erase */
1221         if (name == NULL && logid == NULL)
1222                 RETURN(0);
1223
1224         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1225         if (rc < 0)
1226                 RETURN(rc);
1227
1228         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1229         if (rc == 0)
1230                 rc = llog_destroy(env, handle);
1231
1232         rc2 = llog_close(env, handle);
1233         if (rc == 0)
1234                 rc = rc2;
1235         RETURN(rc);
1236 }
1237 EXPORT_SYMBOL(llog_erase);
1238
1239 /*
1240  * Helper function for write record in llog.
1241  * It hides all transaction handling from caller.
1242  * Valid only with local llog.
1243  */
1244 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1245                struct llog_rec_hdr *rec, int idx)
1246 {
1247         struct dt_device        *dt;
1248         struct thandle          *th;
1249         bool                    need_cookie;
1250         int                     rc;
1251
1252         ENTRY;
1253
1254         LASSERT(loghandle);
1255         LASSERT(loghandle->lgh_ctxt);
1256         LASSERT(loghandle->lgh_obj != NULL);
1257
1258         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1259
1260         if (unlikely(unlikely(dt->dd_rdonly)))
1261                 RETURN(-EROFS);
1262
1263         th = dt_trans_create(env, dt);
1264         if (IS_ERR(th))
1265                 RETURN(PTR_ERR(th));
1266
1267         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1268         if (rc)
1269                 GOTO(out_trans, rc);
1270
1271         th->th_wait_submit = 1;
1272         rc = dt_trans_start_local(env, dt, th);
1273         if (rc)
1274                 GOTO(out_trans, rc);
1275
1276         need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1277
1278         down_write(&loghandle->lgh_lock);
1279         if (need_cookie) {
1280                 struct llog_thread_info *lti = llog_info(env);
1281
1282                 /* cookie comes from llog_process_thread */
1283                 rc = llog_write_rec(env, loghandle, rec, &lti->lgi_cookie,
1284                                     rec->lrh_index, th);
1285                 /* upper layer didn`t pass cookie so change rc */
1286                 rc = (rc == 1 ? 0 : rc);
1287         } else {
1288                 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1289         }
1290
1291         up_write(&loghandle->lgh_lock);
1292 out_trans:
1293         dt_trans_stop(env, dt, th);
1294         RETURN(rc);
1295 }
1296 EXPORT_SYMBOL(llog_write);
1297
1298 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1299               struct llog_handle **lgh, struct llog_logid *logid,
1300               char *name, enum llog_open_param open_param)
1301 {
1302         const struct cred *old_cred;
1303         int      rc;
1304
1305         ENTRY;
1306
1307         LASSERT(ctxt);
1308         LASSERT(ctxt->loc_logops);
1309
1310         if (ctxt->loc_logops->lop_open == NULL) {
1311                 *lgh = NULL;
1312                 RETURN(-EOPNOTSUPP);
1313         }
1314
1315         *lgh = llog_alloc_handle();
1316         if (*lgh == NULL)
1317                 RETURN(-ENOMEM);
1318         (*lgh)->lgh_ctxt = ctxt;
1319         (*lgh)->lgh_logops = ctxt->loc_logops;
1320
1321         old_cred = llog_raise_resource();
1322         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1323         llog_restore_resource(old_cred);
1324         if (rc) {
1325                 llog_free_handle(*lgh);
1326                 *lgh = NULL;
1327         }
1328         RETURN(rc);
1329 }
1330 EXPORT_SYMBOL(llog_open);
1331
1332 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1333 {
1334         return llog_handle_put(env, loghandle);
1335 }
1336 EXPORT_SYMBOL(llog_close);
1337
1338 /**
1339  * Helper function to get the llog size in records. It is used by MGS
1340  * mostly to check that config llog exists and contains data.
1341  *
1342  * \param[in] env       execution environment
1343  * \param[in] ctxt      llog context
1344  * \param[in] name      llog name
1345  *
1346  * \retval              true if there are records in llog besides a header
1347  * \retval              false on error or llog without records
1348  */
1349 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1350                   char *name)
1351 {
1352         struct llog_handle      *llh;
1353         int                      rc = 0;
1354
1355         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1356         if (rc < 0) {
1357                 if (likely(rc == -ENOENT))
1358                         rc = 0;
1359                 GOTO(out, rc);
1360         }
1361
1362         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1363         if (rc)
1364                 GOTO(out_close, rc);
1365         rc = llog_get_size(llh);
1366
1367 out_close:
1368         llog_close(env, llh);
1369 out:
1370         /* The header is record 1, the llog is still considered as empty
1371          * if there is only header */
1372         return (rc <= 1);
1373 }
1374 EXPORT_SYMBOL(llog_is_empty);
1375
1376 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1377                       struct llog_rec_hdr *rec, void *data)
1378 {
1379         struct llog_handle      *copy_llh = data;
1380
1381         /* Append all records */
1382         return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1383 }
1384
1385 /* backup plain llog */
1386 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1387                 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1388                 char *name, char *backup)
1389 {
1390         struct llog_handle      *llh, *bllh;
1391         int                      rc;
1392
1393         ENTRY;
1394
1395         /* open original log */
1396         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1397         if (rc < 0) {
1398                 /* the -ENOENT case is also reported to the caller
1399                  * but silently so it should handle that if needed.
1400                  */
1401                 if (rc != -ENOENT)
1402                         CERROR("%s: failed to open log %s: rc = %d\n",
1403                                obd->obd_name, name, rc);
1404                 RETURN(rc);
1405         }
1406
1407         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1408         if (rc)
1409                 GOTO(out_close, rc);
1410
1411         /* Make sure there's no old backup log */
1412         rc = llog_erase(env, bctxt, NULL, backup);
1413         if (rc < 0 && rc != -ENOENT)
1414                 GOTO(out_close, rc);
1415
1416         /* open backup log */
1417         rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1418         if (rc) {
1419                 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1420                        obd->obd_name, backup, rc);
1421                 GOTO(out_close, rc);
1422         }
1423
1424         /* check that backup llog is not the same object as original one */
1425         if (llh->lgh_obj == bllh->lgh_obj) {
1426                 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1427                        obd->obd_name, name, backup, llh->lgh_obj,
1428                        bllh->lgh_obj);
1429                 GOTO(out_backup, rc = -EEXIST);
1430         }
1431
1432         rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1433         if (rc)
1434                 GOTO(out_backup, rc);
1435
1436         /* Copy log record by record */
1437         rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1438                                   NULL, false);
1439         if (rc)
1440                 CERROR("%s: failed to backup log %s: rc = %d\n",
1441                        obd->obd_name, name, rc);
1442 out_backup:
1443         llog_close(env, bllh);
1444 out_close:
1445         llog_close(env, llh);
1446         RETURN(rc);
1447 }
1448 EXPORT_SYMBOL(llog_backup);
1449
1450 /* Get size of llog */
1451 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1452 {
1453         int rc;
1454         struct lu_attr la;
1455
1456         rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1457         if (rc) {
1458                 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1459                        llh->lgh_ctxt->loc_obd->obd_name,
1460                        PFID(&llh->lgh_id.lgl_oi.oi_fid), rc);
1461                 return 0;
1462         }
1463
1464         return la.la_size;
1465 }
1466 EXPORT_SYMBOL(llog_size);
1467