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