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