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