Whamcloud - gitweb
56413eff997656c61503567f26fa357c4efaace9
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2012, 2014, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/obdclass/llog.c
37  *
38  * OST<->MDS recovery logging infrastructure.
39  * Invariants in implementation:
40  * - we do not share logs among different OST<->MDS connections, so that
41  *   if an OST or MDS fails it need only look at log(s) relevant to itself
42  *
43  * Author: Andreas Dilger <adilger@clusterfs.com>
44  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
45  * Author: Mikhail Pershin <tappro@whamcloud.com>
46  */
47
48 #define DEBUG_SUBSYSTEM S_LOG
49
50 #include <linux/kthread.h>
51 #include <obd_class.h>
52 #include <lustre_log.h>
53 #include "llog_internal.h"
54
55 /*
56  * Allocate a new log or catalog handle
57  * Used inside llog_open().
58  */
59 static struct llog_handle *llog_alloc_handle(void)
60 {
61         struct llog_handle *loghandle;
62
63         OBD_ALLOC_PTR(loghandle);
64         if (loghandle == NULL)
65                 return NULL;
66
67         init_rwsem(&loghandle->lgh_lock);
68         init_rwsem(&loghandle->lgh_hdr_lock);
69         INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
70         atomic_set(&loghandle->lgh_refcount, 1);
71
72         return loghandle;
73 }
74
75 /*
76  * Free llog handle and header data if exists. Used in llog_close() only
77  */
78 static void llog_free_handle(struct llog_handle *loghandle)
79 {
80         LASSERT(loghandle != NULL);
81
82         /* failed llog_init_handle */
83         if (loghandle->lgh_hdr == NULL)
84                 goto out;
85
86         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
87                 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
88         else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
89                 LASSERT(list_empty(&loghandle->u.chd.chd_head));
90         OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
91 out:
92         OBD_FREE_PTR(loghandle);
93 }
94
95 void llog_handle_get(struct llog_handle *loghandle)
96 {
97         atomic_inc(&loghandle->lgh_refcount);
98 }
99
100 void llog_handle_put(struct llog_handle *loghandle)
101 {
102         LASSERT(atomic_read(&loghandle->lgh_refcount) > 0);
103         if (atomic_dec_and_test(&loghandle->lgh_refcount))
104                 llog_free_handle(loghandle);
105 }
106
107 /* returns negative on error; 0 if success; 1 if success & log destroyed */
108 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
109                     int index)
110 {
111         struct llog_log_hdr *llh = loghandle->lgh_hdr;
112         int rc = 0;
113         ENTRY;
114
115         CDEBUG(D_RPCTRACE, "Canceling %d in log "DOSTID"\n",
116                index, POSTID(&loghandle->lgh_id.lgl_oi));
117
118         if (index == 0) {
119                 CERROR("Can't cancel index 0 which is header\n");
120                 RETURN(-EINVAL);
121         }
122
123         down_write(&loghandle->lgh_hdr_lock);
124         if (!ext2_clear_bit(index, LLOG_HDR_BITMAP(llh))) {
125                 up_write(&loghandle->lgh_hdr_lock);
126                 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
127                 RETURN(-ENOENT);
128         }
129
130         llh->llh_count--;
131
132         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
133             (llh->llh_count == 1) &&
134             (loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1)) {
135                 up_write(&loghandle->lgh_hdr_lock);
136                 rc = llog_destroy(env, loghandle);
137                 if (rc < 0) {
138                         CERROR("%s: can't destroy empty llog #"DOSTID
139                                "#%08x: rc = %d\n",
140                                loghandle->lgh_ctxt->loc_obd->obd_name,
141                                POSTID(&loghandle->lgh_id.lgl_oi),
142                                loghandle->lgh_id.lgl_ogen, rc);
143                         GOTO(out_err, rc);
144                 }
145                 RETURN(LLOG_DEL_PLAIN);
146         }
147         up_write(&loghandle->lgh_hdr_lock);
148
149         rc = llog_write(env, loghandle, &llh->llh_hdr, LLOG_HEADER_IDX);
150         if (rc < 0) {
151                 CERROR("%s: fail to write header for llog #"DOSTID
152                        "#%08x: rc = %d\n",
153                        loghandle->lgh_ctxt->loc_obd->obd_name,
154                        POSTID(&loghandle->lgh_id.lgl_oi),
155                        loghandle->lgh_id.lgl_ogen, rc);
156                 GOTO(out_err, rc);
157         }
158         RETURN(0);
159 out_err:
160         down_write(&loghandle->lgh_hdr_lock);
161         ext2_set_bit(index, LLOG_HDR_BITMAP(llh));
162         llh->llh_count++;
163         up_write(&loghandle->lgh_hdr_lock);
164         return rc;
165 }
166
167 static int llog_read_header(const struct lu_env *env,
168                             struct llog_handle *handle,
169                             struct obd_uuid *uuid)
170 {
171         struct llog_operations *lop;
172         int rc;
173
174         rc = llog_handle2ops(handle, &lop);
175         if (rc)
176                 RETURN(rc);
177
178         if (lop->lop_read_header == NULL)
179                 RETURN(-EOPNOTSUPP);
180
181         rc = lop->lop_read_header(env, handle);
182         if (rc == LLOG_EEMPTY) {
183                 struct llog_log_hdr *llh = handle->lgh_hdr;
184
185                 /* lrh_len should be initialized in llog_init_handle */
186                 handle->lgh_last_idx = 0; /* header is record with index 0 */
187                 llh->llh_count = 1;         /* for the header record */
188                 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
189                 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
190                                                 LLOG_MIN_CHUNK_SIZE);
191                 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
192                 llh->llh_hdr.lrh_index = 0;
193                 llh->llh_timestamp = cfs_time_current_sec();
194                 if (uuid)
195                         memcpy(&llh->llh_tgtuuid, uuid,
196                                sizeof(llh->llh_tgtuuid));
197                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
198                 ext2_set_bit(0, LLOG_HDR_BITMAP(llh));
199                 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
200                 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
201                 rc = 0;
202         }
203         return rc;
204 }
205
206 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
207                      int flags, struct obd_uuid *uuid)
208 {
209         struct llog_log_hdr     *llh;
210         enum llog_flag           fmt = flags & LLOG_F_EXT_MASK;
211         int                      rc;
212         int                     chunk_size = handle->lgh_ctxt->loc_chunk_size;
213         ENTRY;
214
215         LASSERT(handle->lgh_hdr == NULL);
216
217         LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
218         OBD_ALLOC_LARGE(llh, chunk_size);
219         if (llh == NULL)
220                 RETURN(-ENOMEM);
221
222         handle->lgh_hdr = llh;
223         handle->lgh_hdr_size = chunk_size;
224         /* first assign flags to use llog_client_ops */
225         llh->llh_flags = flags;
226         rc = llog_read_header(env, handle, uuid);
227         if (rc == 0) {
228                 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
229                               flags & LLOG_F_IS_CAT) ||
230                              (llh->llh_flags & LLOG_F_IS_CAT &&
231                               flags & LLOG_F_IS_PLAIN))) {
232                         CERROR("%s: llog type is %s but initializing %s\n",
233                                handle->lgh_ctxt->loc_obd->obd_name,
234                                llh->llh_flags & LLOG_F_IS_CAT ?
235                                "catalog" : "plain",
236                                flags & LLOG_F_IS_CAT ? "catalog" : "plain");
237                         GOTO(out, rc = -EINVAL);
238                 } else if (llh->llh_flags &
239                            (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
240                         /*
241                          * it is possible to open llog without specifying llog
242                          * type so it is taken from llh_flags
243                          */
244                         flags = llh->llh_flags;
245                 } else {
246                         /* for some reason the llh_flags has no type set */
247                         CERROR("llog type is not specified!\n");
248                         GOTO(out, rc = -EINVAL);
249                 }
250                 if (unlikely(uuid &&
251                              !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
252                         CERROR("%s: llog uuid mismatch: %s/%s\n",
253                                handle->lgh_ctxt->loc_obd->obd_name,
254                                (char *)uuid->uuid,
255                                (char *)llh->llh_tgtuuid.uuid);
256                         GOTO(out, rc = -EEXIST);
257                 }
258         }
259         if (flags & LLOG_F_IS_CAT) {
260                 LASSERT(list_empty(&handle->u.chd.chd_head));
261                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
262                 llh->llh_size = sizeof(struct llog_logid_rec);
263         } else if (!(flags & LLOG_F_IS_PLAIN)) {
264                 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
265                        handle->lgh_ctxt->loc_obd->obd_name,
266                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
267                 rc = -EINVAL;
268         }
269         llh->llh_flags |= fmt;
270 out:
271         if (rc) {
272                 OBD_FREE_LARGE(llh, chunk_size);
273                 handle->lgh_hdr = NULL;
274         }
275         RETURN(rc);
276 }
277 EXPORT_SYMBOL(llog_init_handle);
278
279 static int llog_process_thread(void *arg)
280 {
281         struct llog_process_info        *lpi = arg;
282         struct llog_handle              *loghandle = lpi->lpi_loghandle;
283         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
284         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
285         char                            *buf;
286         size_t                           chunk_size;
287         __u64                            cur_offset;
288         int                              rc = 0, index = 1, last_index;
289         int                              saved_index = 0;
290         int                              last_called_index = 0;
291
292         ENTRY;
293
294         if (llh == NULL)
295                 RETURN(-EINVAL);
296
297         cur_offset = chunk_size = llh->llh_hdr.lrh_len;
298         /* expect chunk_size to be power of two */
299         LASSERT(is_power_of_2(chunk_size));
300
301         OBD_ALLOC_LARGE(buf, chunk_size);
302         if (buf == NULL) {
303                 lpi->lpi_rc = -ENOMEM;
304                 RETURN(0);
305         }
306
307         if (cd != NULL) {
308                 last_called_index = cd->lpcd_first_idx;
309                 index = cd->lpcd_first_idx + 1;
310         }
311         if (cd != NULL && cd->lpcd_last_idx)
312                 last_index = cd->lpcd_last_idx;
313         else
314                 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
315
316         while (rc == 0) {
317                 struct llog_rec_hdr *rec;
318                 off_t chunk_offset;
319                 unsigned int buf_offset = 0;
320                 bool partial_chunk;
321
322                 /* skip records not set in bitmap */
323                 while (index <= last_index &&
324                        !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
325                         ++index;
326
327                 /* There are no indices prior the last_index */
328                 if (index > last_index)
329                         break;
330
331                 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
332                        last_index);
333
334 repeat:
335                 /* get the buf with our target record; avoid old garbage */
336                 memset(buf, 0, chunk_size);
337                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
338                                      index, &cur_offset, buf, chunk_size);
339                 if (rc != 0)
340                         GOTO(out, rc);
341
342                 /* NB: after llog_next_block() call the cur_offset is the
343                  * offset of the next block after read one.
344                  * The absolute offset of the current chunk is calculated
345                  * from cur_offset value and stored in chunk_offset variable.
346                  */
347                 if (cur_offset % chunk_size != 0) {
348                         partial_chunk = true;
349                         chunk_offset = cur_offset & ~(chunk_size - 1);
350                 } else {
351                         partial_chunk = false;
352                         chunk_offset = cur_offset - chunk_size;
353                 }
354
355                 /* NB: when rec->lrh_len is accessed it is already swabbed
356                  * since it is used at the "end" of the loop and the rec
357                  * swabbing is done at the beginning of the loop. */
358                 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
359                      (char *)rec < buf + chunk_size;
360                      rec = llog_rec_hdr_next(rec)) {
361
362                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
363                                rec, rec->lrh_type);
364
365                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
366                                 lustre_swab_llog_rec(rec);
367
368                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
369                                rec->lrh_type, rec->lrh_index);
370
371                         /* for partial chunk the end of it is zeroed, check
372                          * for index 0 to distinguish it. */
373                         if (partial_chunk && rec->lrh_index == 0) {
374                                 /* concurrent llog_add() might add new records
375                                  * while llog_processing, check this is not
376                                  * the case and re-read the current chunk
377                                  * otherwise. */
378                                 if (index > loghandle->lgh_last_idx)
379                                         GOTO(out, rc = 0);
380                                 CDEBUG(D_OTHER, "Re-read last llog buffer for "
381                                        "new records, index %u, last %u\n",
382                                        index, loghandle->lgh_last_idx);
383                                 /* save offset inside buffer for the re-read */
384                                 buf_offset = (char *)rec - (char *)buf;
385                                 cur_offset = chunk_offset;
386                                 goto repeat;
387                         }
388
389                         if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
390                                 CWARN("invalid length %d in llog record for "
391                                       "index %d/%d\n", rec->lrh_len,
392                                       rec->lrh_index, index);
393                                 GOTO(out, rc = -EINVAL);
394                         }
395
396                         if (rec->lrh_index < index) {
397                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
398                                        rec->lrh_index);
399                                 continue;
400                         }
401
402                         if (rec->lrh_index != index) {
403                                 CERROR("%s: Invalid record: index %u but "
404                                        "expected %u\n",
405                                        loghandle->lgh_ctxt->loc_obd->obd_name,
406                                        rec->lrh_index, index);
407                                 GOTO(out, rc = -ERANGE);
408                         }
409
410                         CDEBUG(D_OTHER,
411                                "lrh_index: %d lrh_len: %d (%d remains)\n",
412                                rec->lrh_index, rec->lrh_len,
413                                (int)(buf + chunk_size - (char *)rec));
414
415                         loghandle->lgh_cur_idx = rec->lrh_index;
416                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
417                                                     chunk_offset;
418
419                         /* if set, process the callback on this record */
420                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
421                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
422                                                  lpi->lpi_cbdata);
423                                 last_called_index = index;
424                                 if (rc == LLOG_PROC_BREAK) {
425                                         GOTO(out, rc);
426                                 } else if (rc == LLOG_DEL_RECORD) {
427                                         rc = llog_cancel_rec(lpi->lpi_env,
428                                                              loghandle,
429                                                              rec->lrh_index);
430                                 }
431                                 if (rc)
432                                         GOTO(out, rc);
433                         }
434                         /* exit if the last index is reached */
435                         if (index >= last_index)
436                                 GOTO(out, rc = 0);
437                         ++index;
438                 }
439         }
440
441 out:
442         if (cd != NULL)
443                 cd->lpcd_last_idx = last_called_index;
444
445         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
446                 /* something bad happened to the processing of a local
447                  * llog file, probably I/O error or the log got corrupted..
448                  * to be able to finally release the log we discard any
449                  * remaining bits in the header */
450                 CERROR("Local llog found corrupted\n");
451                 while (index <= last_index) {
452                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh)) != 0)
453                                 llog_cancel_rec(lpi->lpi_env, loghandle, index);
454                         index++;
455                 }
456                 rc = 0;
457         }
458
459         OBD_FREE_LARGE(buf, chunk_size);
460         lpi->lpi_rc = rc;
461         return 0;
462 }
463
464 static int llog_process_thread_daemonize(void *arg)
465 {
466         struct llog_process_info        *lpi = arg;
467         struct lu_env                    env;
468         int                              rc;
469
470         unshare_fs_struct();
471
472         /* client env has no keys, tags is just 0 */
473         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
474         if (rc)
475                 goto out;
476         lpi->lpi_env = &env;
477
478         rc = llog_process_thread(arg);
479
480         lu_env_fini(&env);
481 out:
482         complete(&lpi->lpi_completion);
483         return rc;
484 }
485
486 int llog_process_or_fork(const struct lu_env *env,
487                          struct llog_handle *loghandle,
488                          llog_cb_t cb, void *data, void *catdata, bool fork)
489 {
490         struct llog_process_info *lpi;
491         int                      rc;
492
493         ENTRY;
494
495         OBD_ALLOC_PTR(lpi);
496         if (lpi == NULL) {
497                 CERROR("cannot alloc pointer\n");
498                 RETURN(-ENOMEM);
499         }
500         lpi->lpi_loghandle = loghandle;
501         lpi->lpi_cb        = cb;
502         lpi->lpi_cbdata    = data;
503         lpi->lpi_catdata   = catdata;
504
505         if (fork) {
506                 struct task_struct *task;
507
508                 /* The new thread can't use parent env,
509                  * init the new one in llog_process_thread_daemonize. */
510                 lpi->lpi_env = NULL;
511                 init_completion(&lpi->lpi_completion);
512                 task = kthread_run(llog_process_thread_daemonize, lpi,
513                                    "llog_process_thread");
514                 if (IS_ERR(task)) {
515                         rc = PTR_ERR(task);
516                         CERROR("%s: cannot start thread: rc = %d\n",
517                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
518                         GOTO(out_lpi, rc);
519                 }
520                 wait_for_completion(&lpi->lpi_completion);
521         } else {
522                 lpi->lpi_env = env;
523                 llog_process_thread(lpi);
524         }
525         rc = lpi->lpi_rc;
526
527 out_lpi:
528         OBD_FREE_PTR(lpi);
529         RETURN(rc);
530 }
531 EXPORT_SYMBOL(llog_process_or_fork);
532
533 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
534                  llog_cb_t cb, void *data, void *catdata)
535 {
536         int rc;
537         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
538         return rc == LLOG_DEL_PLAIN ? 0 : rc;
539 }
540 EXPORT_SYMBOL(llog_process);
541
542 int llog_reverse_process(const struct lu_env *env,
543                          struct llog_handle *loghandle, llog_cb_t cb,
544                          void *data, void *catdata)
545 {
546         struct llog_log_hdr *llh = loghandle->lgh_hdr;
547         struct llog_process_cat_data *cd = catdata;
548         void *buf;
549         int rc = 0, first_index = 1, index, idx;
550         __u32   chunk_size = llh->llh_hdr.lrh_len;
551         ENTRY;
552
553         OBD_ALLOC_LARGE(buf, chunk_size);
554         if (buf == NULL)
555                 RETURN(-ENOMEM);
556
557         if (cd != NULL)
558                 first_index = cd->lpcd_first_idx + 1;
559         if (cd != NULL && cd->lpcd_last_idx)
560                 index = cd->lpcd_last_idx;
561         else
562                 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
563
564         while (rc == 0) {
565                 struct llog_rec_hdr *rec;
566                 struct llog_rec_tail *tail;
567
568                 /* skip records not set in bitmap */
569                 while (index >= first_index &&
570                        !ext2_test_bit(index, LLOG_HDR_BITMAP(llh)))
571                         --index;
572
573                 LASSERT(index >= first_index - 1);
574                 if (index == first_index - 1)
575                         break;
576
577                 /* get the buf with our target record; avoid old garbage */
578                 memset(buf, 0, chunk_size);
579                 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
580                 if (rc)
581                         GOTO(out, rc);
582
583                 rec = buf;
584                 idx = rec->lrh_index;
585                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
586                 while (idx < index) {
587                         rec = (void *)rec + rec->lrh_len;
588                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
589                                 lustre_swab_llog_rec(rec);
590                         idx ++;
591                 }
592                 LASSERT(idx == index);
593                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
594
595                 /* process records in buffer, starting where we found one */
596                 while ((void *)tail > buf) {
597                         if (tail->lrt_index == 0)
598                                 GOTO(out, rc = 0); /* no more records */
599
600                         /* if set, process the callback on this record */
601                         if (ext2_test_bit(index, LLOG_HDR_BITMAP(llh))) {
602                                 rec = (void *)tail - tail->lrt_len +
603                                       sizeof(*tail);
604
605                                 rc = cb(env, loghandle, rec, data);
606                                 if (rc == LLOG_PROC_BREAK) {
607                                         GOTO(out, rc);
608                                 } else if (rc == LLOG_DEL_RECORD) {
609                                         rc = llog_cancel_rec(env, loghandle,
610                                                              tail->lrt_index);
611                                 }
612                                 if (rc)
613                                         GOTO(out, rc);
614                         }
615
616                         /* previous record, still in buffer? */
617                         --index;
618                         if (index < first_index)
619                                 GOTO(out, rc = 0);
620                         tail = (void *)tail - tail->lrt_len;
621                 }
622         }
623
624 out:
625         if (buf != NULL)
626                 OBD_FREE_LARGE(buf, chunk_size);
627         RETURN(rc);
628 }
629 EXPORT_SYMBOL(llog_reverse_process);
630
631 /**
632  * new llog API
633  *
634  * API functions:
635  *      llog_open - open llog, may not exist
636  *      llog_exist - check if llog exists
637  *      llog_close - close opened llog, pair for open, frees llog_handle
638  *      llog_declare_create - declare llog creation
639  *      llog_create - create new llog on disk, need transaction handle
640  *      llog_declare_write_rec - declaration of llog write
641  *      llog_write_rec - write llog record on disk, need transaction handle
642  *      llog_declare_add - declare llog catalog record addition
643  *      llog_add - add llog record in catalog, need transaction handle
644  */
645 int llog_exist(struct llog_handle *loghandle)
646 {
647         struct llog_operations  *lop;
648         int                      rc;
649
650         ENTRY;
651
652         rc = llog_handle2ops(loghandle, &lop);
653         if (rc)
654                 RETURN(rc);
655         if (lop->lop_exist == NULL)
656                 RETURN(-EOPNOTSUPP);
657
658         rc = lop->lop_exist(loghandle);
659         RETURN(rc);
660 }
661 EXPORT_SYMBOL(llog_exist);
662
663 int llog_declare_create(const struct lu_env *env,
664                         struct llog_handle *loghandle, struct thandle *th)
665 {
666         struct llog_operations  *lop;
667         int                      raised, rc;
668
669         ENTRY;
670
671         rc = llog_handle2ops(loghandle, &lop);
672         if (rc)
673                 RETURN(rc);
674         if (lop->lop_declare_create == NULL)
675                 RETURN(-EOPNOTSUPP);
676
677         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
678         if (!raised)
679                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
680         rc = lop->lop_declare_create(env, loghandle, th);
681         if (!raised)
682                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
683         RETURN(rc);
684 }
685
686 int llog_create(const struct lu_env *env, struct llog_handle *handle,
687                 struct thandle *th)
688 {
689         struct llog_operations  *lop;
690         int                      raised, rc;
691
692         ENTRY;
693
694         rc = llog_handle2ops(handle, &lop);
695         if (rc)
696                 RETURN(rc);
697         if (lop->lop_create == NULL)
698                 RETURN(-EOPNOTSUPP);
699
700         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
701         if (!raised)
702                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
703         rc = lop->lop_create(env, handle, th);
704         if (!raised)
705                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
706         RETURN(rc);
707 }
708
709 int llog_declare_write_rec(const struct lu_env *env,
710                            struct llog_handle *handle,
711                            struct llog_rec_hdr *rec, int idx,
712                            struct thandle *th)
713 {
714         struct llog_operations  *lop;
715         int                      raised, rc;
716
717         ENTRY;
718
719         rc = llog_handle2ops(handle, &lop);
720         if (rc)
721                 RETURN(rc);
722         LASSERT(lop);
723         if (lop->lop_declare_write_rec == NULL)
724                 RETURN(-EOPNOTSUPP);
725
726         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
727         if (!raised)
728                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
729         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
730         if (!raised)
731                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
732         RETURN(rc);
733 }
734
735 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
736                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
737                    int idx, struct thandle *th)
738 {
739         struct llog_operations  *lop;
740         int                      raised, rc, buflen;
741
742         ENTRY;
743
744         rc = llog_handle2ops(handle, &lop);
745         if (rc)
746                 RETURN(rc);
747
748         LASSERT(lop);
749         if (lop->lop_write_rec == NULL)
750                 RETURN(-EOPNOTSUPP);
751
752         buflen = rec->lrh_len;
753         LASSERT(cfs_size_round(buflen) == buflen);
754
755         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
756         if (!raised)
757                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
758         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
759         if (!raised)
760                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
761         RETURN(rc);
762 }
763
764 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
765              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
766              struct thandle *th)
767 {
768         int raised, rc;
769
770         ENTRY;
771
772         if (lgh->lgh_logops->lop_add == NULL)
773                 RETURN(-EOPNOTSUPP);
774
775         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
776         if (!raised)
777                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
778         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
779         if (!raised)
780                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
781         RETURN(rc);
782 }
783 EXPORT_SYMBOL(llog_add);
784
785 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
786                      struct llog_rec_hdr *rec, struct thandle *th)
787 {
788         int raised, rc;
789
790         ENTRY;
791
792         if (lgh->lgh_logops->lop_declare_add == NULL)
793                 RETURN(-EOPNOTSUPP);
794
795         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
796         if (!raised)
797                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
798         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
799         if (!raised)
800                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
801         RETURN(rc);
802 }
803 EXPORT_SYMBOL(llog_declare_add);
804
805 /**
806  * Helper function to open llog or create it if doesn't exist.
807  * It hides all transaction handling from caller.
808  */
809 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
810                      struct llog_handle **res, struct llog_logid *logid,
811                      char *name)
812 {
813         struct dt_device        *d;
814         struct thandle          *th;
815         int                      rc;
816
817         ENTRY;
818
819         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
820         if (rc)
821                 RETURN(rc);
822
823         if (llog_exist(*res))
824                 RETURN(0);
825
826         LASSERT((*res)->lgh_obj != NULL);
827
828         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
829
830         th = dt_trans_create(env, d);
831         if (IS_ERR(th))
832                 GOTO(out, rc = PTR_ERR(th));
833
834         /* Create the remote update llog object synchronously, which
835          * happens during inialization process see lod_sub_prep_llog(),
836          * to make sure the update llog object is created before
837          * corss-MDT writing updates into the llog object */
838         if (dt_object_remote((*res)->lgh_obj))
839                 th->th_sync = 1;
840
841         th->th_wait_submit = 1;
842         rc = llog_declare_create(env, *res, th);
843         if (rc == 0) {
844                 rc = dt_trans_start_local(env, d, th);
845                 if (rc == 0)
846                         rc = llog_create(env, *res, th);
847         }
848         dt_trans_stop(env, d, th);
849 out:
850         if (rc)
851                 llog_close(env, *res);
852         RETURN(rc);
853 }
854 EXPORT_SYMBOL(llog_open_create);
855
856 /**
857  * Helper function to delete existent llog.
858  */
859 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
860                struct llog_logid *logid, char *name)
861 {
862         struct llog_handle      *handle;
863         int                      rc = 0, rc2;
864
865         ENTRY;
866
867         /* nothing to erase */
868         if (name == NULL && logid == NULL)
869                 RETURN(0);
870
871         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
872         if (rc < 0)
873                 RETURN(rc);
874
875         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
876         if (rc == 0)
877                 rc = llog_destroy(env, handle);
878
879         rc2 = llog_close(env, handle);
880         if (rc == 0)
881                 rc = rc2;
882         RETURN(rc);
883 }
884 EXPORT_SYMBOL(llog_erase);
885
886 /*
887  * Helper function for write record in llog.
888  * It hides all transaction handling from caller.
889  * Valid only with local llog.
890  */
891 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
892                struct llog_rec_hdr *rec, int idx)
893 {
894         struct dt_device        *dt;
895         struct thandle          *th;
896         int                      rc;
897
898         ENTRY;
899
900         LASSERT(loghandle);
901         LASSERT(loghandle->lgh_ctxt);
902         LASSERT(loghandle->lgh_obj != NULL);
903
904         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
905
906         th = dt_trans_create(env, dt);
907         if (IS_ERR(th))
908                 RETURN(PTR_ERR(th));
909
910         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
911         if (rc)
912                 GOTO(out_trans, rc);
913
914         th->th_wait_submit = 1;
915         rc = dt_trans_start_local(env, dt, th);
916         if (rc)
917                 GOTO(out_trans, rc);
918
919         down_write(&loghandle->lgh_lock);
920         rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
921         up_write(&loghandle->lgh_lock);
922 out_trans:
923         dt_trans_stop(env, dt, th);
924         RETURN(rc);
925 }
926 EXPORT_SYMBOL(llog_write);
927
928 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
929               struct llog_handle **lgh, struct llog_logid *logid,
930               char *name, enum llog_open_param open_param)
931 {
932         int      raised;
933         int      rc;
934
935         ENTRY;
936
937         LASSERT(ctxt);
938         LASSERT(ctxt->loc_logops);
939
940         if (ctxt->loc_logops->lop_open == NULL) {
941                 *lgh = NULL;
942                 RETURN(-EOPNOTSUPP);
943         }
944
945         *lgh = llog_alloc_handle();
946         if (*lgh == NULL)
947                 RETURN(-ENOMEM);
948         (*lgh)->lgh_ctxt = ctxt;
949         (*lgh)->lgh_logops = ctxt->loc_logops;
950
951         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
952         if (!raised)
953                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
954         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
955         if (!raised)
956                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
957         if (rc) {
958                 llog_free_handle(*lgh);
959                 *lgh = NULL;
960         }
961         RETURN(rc);
962 }
963 EXPORT_SYMBOL(llog_open);
964
965 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
966 {
967         struct llog_operations  *lop;
968         int                      rc;
969
970         ENTRY;
971
972         rc = llog_handle2ops(loghandle, &lop);
973         if (rc)
974                 GOTO(out, rc);
975         if (lop->lop_close == NULL)
976                 GOTO(out, rc = -EOPNOTSUPP);
977         rc = lop->lop_close(env, loghandle);
978 out:
979         llog_handle_put(loghandle);
980         RETURN(rc);
981 }
982 EXPORT_SYMBOL(llog_close);
983
984 /**
985  * Helper function to get the llog size in records. It is used by MGS
986  * mostly to check that config llog exists and contains data.
987  *
988  * \param[in] env       execution environment
989  * \param[in] ctxt      llog context
990  * \param[in] name      llog name
991  *
992  * \retval              true if there are records in llog besides a header
993  * \retval              false on error or llog without records
994  */
995 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
996                   char *name)
997 {
998         struct llog_handle      *llh;
999         int                      rc = 0;
1000
1001         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1002         if (rc < 0) {
1003                 if (likely(rc == -ENOENT))
1004                         rc = 0;
1005                 GOTO(out, rc);
1006         }
1007
1008         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1009         if (rc)
1010                 GOTO(out_close, rc);
1011         rc = llog_get_size(llh);
1012
1013 out_close:
1014         llog_close(env, llh);
1015 out:
1016         /* The header is record 1, the llog is still considered as empty
1017          * if there is only header */
1018         return (rc <= 1);
1019 }
1020 EXPORT_SYMBOL(llog_is_empty);
1021
1022 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1023                       struct llog_rec_hdr *rec, void *data)
1024 {
1025         struct llog_handle      *copy_llh = data;
1026
1027         /* Append all records */
1028         return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1029 }
1030
1031 /* backup plain llog */
1032 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1033                 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1034                 char *name, char *backup)
1035 {
1036         struct llog_handle      *llh, *bllh;
1037         int                      rc;
1038
1039         ENTRY;
1040
1041         /* open original log */
1042         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1043         if (rc < 0) {
1044                 /* the -ENOENT case is also reported to the caller
1045                  * but silently so it should handle that if needed.
1046                  */
1047                 if (rc != -ENOENT)
1048                         CERROR("%s: failed to open log %s: rc = %d\n",
1049                                obd->obd_name, name, rc);
1050                 RETURN(rc);
1051         }
1052
1053         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1054         if (rc)
1055                 GOTO(out_close, rc);
1056
1057         /* Make sure there's no old backup log */
1058         rc = llog_erase(env, bctxt, NULL, backup);
1059         if (rc < 0 && rc != -ENOENT)
1060                 GOTO(out_close, rc);
1061
1062         /* open backup log */
1063         rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1064         if (rc) {
1065                 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1066                        obd->obd_name, backup, rc);
1067                 GOTO(out_close, rc);
1068         }
1069
1070         /* check that backup llog is not the same object as original one */
1071         if (llh->lgh_obj == bllh->lgh_obj) {
1072                 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1073                        obd->obd_name, name, backup, llh->lgh_obj,
1074                        bllh->lgh_obj);
1075                 GOTO(out_backup, rc = -EEXIST);
1076         }
1077
1078         rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1079         if (rc)
1080                 GOTO(out_backup, rc);
1081
1082         /* Copy log record by record */
1083         rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1084                                   NULL, false);
1085         if (rc)
1086                 CERROR("%s: failed to backup log %s: rc = %d\n",
1087                        obd->obd_name, name, rc);
1088 out_backup:
1089         llog_close(env, bllh);
1090 out_close:
1091         llog_close(env, llh);
1092         RETURN(rc);
1093 }
1094 EXPORT_SYMBOL(llog_backup);