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