Whamcloud - gitweb
9346f18993da3eeae54ae03b9ddae8cc33ab0b7c
[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         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 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)
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
168 static int llog_read_header(const struct lu_env *env,
169                             struct llog_handle *handle,
170                             struct obd_uuid *uuid)
171 {
172         struct llog_operations *lop;
173         int rc;
174
175         rc = llog_handle2ops(handle, &lop);
176         if (rc)
177                 RETURN(rc);
178
179         if (lop->lop_read_header == NULL)
180                 RETURN(-EOPNOTSUPP);
181
182         rc = lop->lop_read_header(env, handle);
183         if (rc == LLOG_EEMPTY) {
184                 struct llog_log_hdr *llh = handle->lgh_hdr;
185
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                 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
190                 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
191                 llh->llh_timestamp = cfs_time_current_sec();
192                 if (uuid)
193                         memcpy(&llh->llh_tgtuuid, uuid,
194                                sizeof(llh->llh_tgtuuid));
195                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
196                 ext2_set_bit(0, llh->llh_bitmap);
197                 rc = 0;
198         }
199         return rc;
200 }
201
202 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
203                      int flags, struct obd_uuid *uuid)
204 {
205         struct llog_log_hdr     *llh;
206         enum llog_flag           fmt = flags & LLOG_F_EXT_MASK;
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         llh->llh_flags |= fmt;
262 out:
263         if (rc) {
264                 OBD_FREE_PTR(llh);
265                 handle->lgh_hdr = NULL;
266         }
267         RETURN(rc);
268 }
269 EXPORT_SYMBOL(llog_init_handle);
270
271 static int llog_process_thread(void *arg)
272 {
273         struct llog_process_info        *lpi = arg;
274         struct llog_handle              *loghandle = lpi->lpi_loghandle;
275         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
276         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
277         char                            *buf;
278         __u64                            cur_offset = LLOG_CHUNK_SIZE;
279         __u64                            last_offset;
280         int                              rc = 0, index = 1, last_index;
281         int                              saved_index = 0;
282         int                              last_called_index = 0;
283
284         ENTRY;
285
286         LASSERT(llh);
287
288         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
289         if (!buf) {
290                 lpi->lpi_rc = -ENOMEM;
291                 RETURN(0);
292         }
293
294         if (cd != NULL) {
295                 last_called_index = cd->lpcd_first_idx;
296                 index = cd->lpcd_first_idx + 1;
297         }
298         if (cd != NULL && cd->lpcd_last_idx)
299                 last_index = cd->lpcd_last_idx;
300         else
301                 last_index = LLOG_BITMAP_BYTES * 8 - 1;
302
303         if (index > last_index) {
304                 /* Record is not in this buffer. */
305                 GOTO(out, rc);
306         }
307
308         while (rc == 0) {
309                 struct llog_rec_hdr *rec;
310
311                 /* skip records not set in bitmap */
312                 while (index <= last_index &&
313                        !ext2_test_bit(index, llh->llh_bitmap))
314                         ++index;
315
316                 LASSERT(index <= last_index + 1);
317                 if (index == last_index + 1)
318                         break;
319 repeat:
320                 CDEBUG(D_OTHER, "index: %d last_index %d\n",
321                        index, last_index);
322
323                 /* get the buf with our target record; avoid old garbage */
324                 memset(buf, 0, LLOG_CHUNK_SIZE);
325                 last_offset = cur_offset;
326                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
327                                      index, &cur_offset, buf, LLOG_CHUNK_SIZE);
328                 if (rc)
329                         GOTO(out, rc);
330
331                 /* NB: when rec->lrh_len is accessed it is already swabbed
332                  * since it is used at the "end" of the loop and the rec
333                  * swabbing is done at the beginning of the loop. */
334                 for (rec = (struct llog_rec_hdr *)buf;
335                      (char *)rec < buf + LLOG_CHUNK_SIZE;
336                      rec = llog_rec_hdr_next(rec)) {
337
338                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
339                                rec, rec->lrh_type);
340
341                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
342                                 lustre_swab_llog_rec(rec);
343
344                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
345                                rec->lrh_type, rec->lrh_index);
346
347                         if (rec->lrh_index == 0) {
348                                 /* probably another rec just got added? */
349                                 if (index <= loghandle->lgh_last_idx)
350                                         GOTO(repeat, rc = 0);
351                                 GOTO(out, rc = 0); /* no more records */
352                         }
353                         if (rec->lrh_len == 0 ||
354                             rec->lrh_len > LLOG_CHUNK_SIZE) {
355                                 CWARN("invalid length %d in llog record for "
356                                       "index %d/%d\n", rec->lrh_len,
357                                       rec->lrh_index, index);
358                                 GOTO(out, rc = -EINVAL);
359                         }
360
361                         if (rec->lrh_index < index) {
362                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
363                                        rec->lrh_index);
364                                 continue;
365                         }
366
367                         CDEBUG(D_OTHER,
368                                "lrh_index: %d lrh_len: %d (%d remains)\n",
369                                rec->lrh_index, rec->lrh_len,
370                                (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
371
372                         loghandle->lgh_cur_idx = rec->lrh_index;
373                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
374                                                     last_offset;
375
376                         /* if set, process the callback on this record */
377                         if (ext2_test_bit(index, llh->llh_bitmap)) {
378                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
379                                                  lpi->lpi_cbdata);
380                                 last_called_index = index;
381                                 if (rc == LLOG_PROC_BREAK) {
382                                         GOTO(out, rc);
383                                 } else if (rc == LLOG_DEL_RECORD) {
384                                         rc = llog_cancel_rec(lpi->lpi_env,
385                                                              loghandle,
386                                                              rec->lrh_index);
387                                 }
388                                 if (rc)
389                                         GOTO(out, rc);
390                         } else {
391                                 CDEBUG(D_OTHER, "Skipped index %d\n", index);
392                         }
393
394                         /* next record, still in buffer? */
395                         ++index;
396                         if (index > last_index)
397                                 GOTO(out, rc = 0);
398                 }
399         }
400
401 out:
402         if (cd != NULL)
403                 cd->lpcd_last_idx = last_called_index;
404
405         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
406                 /* something bad happened to the processing of a local
407                  * llog file, probably I/O error or the log got corrupted..
408                  * to be able to finally release the log we discard any
409                  * remaining bits in the header */
410                 CERROR("Local llog found corrupted\n");
411                 while (index <= last_index) {
412                         if (ext2_test_bit(index, llh->llh_bitmap) != 0)
413                                 llog_cancel_rec(lpi->lpi_env, loghandle, index);
414                         index++;
415                 }
416                 rc = 0;
417         }
418
419         OBD_FREE(buf, LLOG_CHUNK_SIZE);
420         lpi->lpi_rc = rc;
421         return 0;
422 }
423
424 static int llog_process_thread_daemonize(void *arg)
425 {
426         struct llog_process_info        *lpi = arg;
427         struct lu_env                    env;
428         int                              rc;
429
430         unshare_fs_struct();
431
432         /* client env has no keys, tags is just 0 */
433         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
434         if (rc)
435                 goto out;
436         lpi->lpi_env = &env;
437
438         rc = llog_process_thread(arg);
439
440         lu_env_fini(&env);
441 out:
442         complete(&lpi->lpi_completion);
443         return rc;
444 }
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         if (fork) {
466                 struct task_struct *task;
467
468                 /* The new thread can't use parent env,
469                  * init the new one in llog_process_thread_daemonize. */
470                 lpi->lpi_env = NULL;
471                 init_completion(&lpi->lpi_completion);
472                 task = kthread_run(llog_process_thread_daemonize, lpi,
473                                    "llog_process_thread");
474                 if (IS_ERR(task)) {
475                         rc = PTR_ERR(task);
476                         CERROR("%s: cannot start thread: rc = %d\n",
477                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
478                         GOTO(out_lpi, rc);
479                 }
480                 wait_for_completion(&lpi->lpi_completion);
481         } else {
482                 lpi->lpi_env = env;
483                 llog_process_thread(lpi);
484         }
485         rc = lpi->lpi_rc;
486
487 out_lpi:
488         OBD_FREE_PTR(lpi);
489         RETURN(rc);
490 }
491 EXPORT_SYMBOL(llog_process_or_fork);
492
493 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
494                  llog_cb_t cb, void *data, void *catdata)
495 {
496         int rc;
497         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
498         return rc == LLOG_DEL_PLAIN ? 0 : rc;
499 }
500 EXPORT_SYMBOL(llog_process);
501
502 int llog_reverse_process(const struct lu_env *env,
503                          struct llog_handle *loghandle, llog_cb_t cb,
504                          void *data, void *catdata)
505 {
506         struct llog_log_hdr *llh = loghandle->lgh_hdr;
507         struct llog_process_cat_data *cd = catdata;
508         void *buf;
509         int rc = 0, first_index = 1, index, idx;
510         ENTRY;
511
512         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
513         if (!buf)
514                 RETURN(-ENOMEM);
515
516         if (cd != NULL)
517                 first_index = cd->lpcd_first_idx + 1;
518         if (cd != NULL && cd->lpcd_last_idx)
519                 index = cd->lpcd_last_idx;
520         else
521                 index = LLOG_BITMAP_BYTES * 8 - 1;
522
523         while (rc == 0) {
524                 struct llog_rec_hdr *rec;
525                 struct llog_rec_tail *tail;
526
527                 /* skip records not set in bitmap */
528                 while (index >= first_index &&
529                        !ext2_test_bit(index, llh->llh_bitmap))
530                         --index;
531
532                 LASSERT(index >= first_index - 1);
533                 if (index == first_index - 1)
534                         break;
535
536                 /* get the buf with our target record; avoid old garbage */
537                 memset(buf, 0, LLOG_CHUNK_SIZE);
538                 rc = llog_prev_block(env, loghandle, index, buf,
539                                      LLOG_CHUNK_SIZE);
540                 if (rc)
541                         GOTO(out, rc);
542
543                 rec = buf;
544                 idx = rec->lrh_index;
545                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
546                 while (idx < index) {
547                         rec = (void *)rec + rec->lrh_len;
548                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
549                                 lustre_swab_llog_rec(rec);
550                         idx ++;
551                 }
552                 LASSERT(idx == index);
553                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
554
555                 /* process records in buffer, starting where we found one */
556                 while ((void *)tail > buf) {
557                         if (tail->lrt_index == 0)
558                                 GOTO(out, rc = 0); /* no more records */
559
560                         /* if set, process the callback on this record */
561                         if (ext2_test_bit(index, llh->llh_bitmap)) {
562                                 rec = (void *)tail - tail->lrt_len +
563                                       sizeof(*tail);
564
565                                 rc = cb(env, loghandle, rec, data);
566                                 if (rc == LLOG_PROC_BREAK) {
567                                         GOTO(out, rc);
568                                 } else if (rc == LLOG_DEL_RECORD) {
569                                         rc = llog_cancel_rec(env, loghandle,
570                                                              tail->lrt_index);
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
646 int llog_create(const struct lu_env *env, struct llog_handle *handle,
647                 struct thandle *th)
648 {
649         struct llog_operations  *lop;
650         int                      raised, rc;
651
652         ENTRY;
653
654         rc = llog_handle2ops(handle, &lop);
655         if (rc)
656                 RETURN(rc);
657         if (lop->lop_create == NULL)
658                 RETURN(-EOPNOTSUPP);
659
660         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
661         if (!raised)
662                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
663         rc = lop->lop_create(env, handle, th);
664         if (!raised)
665                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
666         RETURN(rc);
667 }
668
669 int llog_declare_write_rec(const struct lu_env *env,
670                            struct llog_handle *handle,
671                            struct llog_rec_hdr *rec, int idx,
672                            struct thandle *th)
673 {
674         struct llog_operations  *lop;
675         int                      raised, rc;
676
677         ENTRY;
678
679         rc = llog_handle2ops(handle, &lop);
680         if (rc)
681                 RETURN(rc);
682         LASSERT(lop);
683         if (lop->lop_declare_write_rec == NULL)
684                 RETURN(-EOPNOTSUPP);
685
686         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
687         if (!raised)
688                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
689         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
690         if (!raised)
691                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
692         RETURN(rc);
693 }
694
695 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
696                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
697                    int idx, struct thandle *th)
698 {
699         struct llog_operations  *lop;
700         int                      raised, rc, buflen;
701
702         ENTRY;
703
704         rc = llog_handle2ops(handle, &lop);
705         if (rc)
706                 RETURN(rc);
707
708         LASSERT(lop);
709         if (lop->lop_write_rec == NULL)
710                 RETURN(-EOPNOTSUPP);
711
712         buflen = rec->lrh_len;
713         LASSERT(cfs_size_round(buflen) == buflen);
714
715         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
716         if (!raised)
717                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
718         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
719         if (!raised)
720                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
721         RETURN(rc);
722 }
723
724 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
725              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
726              struct thandle *th)
727 {
728         int raised, rc;
729
730         ENTRY;
731
732         if (lgh->lgh_logops->lop_add == NULL)
733                 RETURN(-EOPNOTSUPP);
734
735         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
736         if (!raised)
737                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
738         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
739         if (!raised)
740                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
741         RETURN(rc);
742 }
743 EXPORT_SYMBOL(llog_add);
744
745 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
746                      struct llog_rec_hdr *rec, struct thandle *th)
747 {
748         int raised, rc;
749
750         ENTRY;
751
752         if (lgh->lgh_logops->lop_declare_add == NULL)
753                 RETURN(-EOPNOTSUPP);
754
755         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
756         if (!raised)
757                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
758         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
759         if (!raised)
760                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
761         RETURN(rc);
762 }
763 EXPORT_SYMBOL(llog_declare_add);
764
765 /**
766  * Helper function to open llog or create it if doesn't exist.
767  * It hides all transaction handling from caller.
768  */
769 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
770                      struct llog_handle **res, struct llog_logid *logid,
771                      char *name)
772 {
773         struct dt_device        *d;
774         struct thandle          *th;
775         int                      rc;
776
777         ENTRY;
778
779         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
780         if (rc)
781                 RETURN(rc);
782
783         if (llog_exist(*res))
784                 RETURN(0);
785
786         LASSERT((*res)->lgh_obj != NULL);
787
788         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
789
790         th = dt_trans_create(env, d);
791         if (IS_ERR(th))
792                 GOTO(out, rc = PTR_ERR(th));
793
794         rc = llog_declare_create(env, *res, th);
795         if (rc == 0) {
796                 rc = dt_trans_start_local(env, d, th);
797                 if (rc == 0)
798                         rc = llog_create(env, *res, th);
799         }
800         dt_trans_stop(env, d, th);
801 out:
802         if (rc)
803                 llog_close(env, *res);
804         RETURN(rc);
805 }
806 EXPORT_SYMBOL(llog_open_create);
807
808 /**
809  * Helper function to delete existent llog.
810  */
811 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
812                struct llog_logid *logid, char *name)
813 {
814         struct llog_handle      *handle;
815         int                      rc = 0, rc2;
816
817         ENTRY;
818
819         /* nothing to erase */
820         if (name == NULL && logid == NULL)
821                 RETURN(0);
822
823         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
824         if (rc < 0)
825                 RETURN(rc);
826
827         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
828         if (rc == 0)
829                 rc = llog_destroy(env, handle);
830
831         rc2 = llog_close(env, handle);
832         if (rc == 0)
833                 rc = rc2;
834         RETURN(rc);
835 }
836 EXPORT_SYMBOL(llog_erase);
837
838 /*
839  * Helper function for write record in llog.
840  * It hides all transaction handling from caller.
841  * Valid only with local llog.
842  */
843 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
844                struct llog_rec_hdr *rec, int idx)
845 {
846         struct dt_device        *dt;
847         struct thandle          *th;
848         int                      rc;
849
850         ENTRY;
851
852         LASSERT(loghandle);
853         LASSERT(loghandle->lgh_ctxt);
854         LASSERT(loghandle->lgh_obj != NULL);
855
856         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
857
858         th = dt_trans_create(env, dt);
859         if (IS_ERR(th))
860                 RETURN(PTR_ERR(th));
861
862         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
863         if (rc)
864                 GOTO(out_trans, rc);
865
866         rc = dt_trans_start_local(env, dt, th);
867         if (rc)
868                 GOTO(out_trans, rc);
869
870         down_write(&loghandle->lgh_lock);
871         rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
872         up_write(&loghandle->lgh_lock);
873 out_trans:
874         dt_trans_stop(env, dt, th);
875         RETURN(rc);
876 }
877 EXPORT_SYMBOL(llog_write);
878
879 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
880               struct llog_handle **lgh, struct llog_logid *logid,
881               char *name, enum llog_open_param open_param)
882 {
883         int      raised;
884         int      rc;
885
886         ENTRY;
887
888         LASSERT(ctxt);
889         LASSERT(ctxt->loc_logops);
890
891         if (ctxt->loc_logops->lop_open == NULL) {
892                 *lgh = NULL;
893                 RETURN(-EOPNOTSUPP);
894         }
895
896         *lgh = llog_alloc_handle();
897         if (*lgh == NULL)
898                 RETURN(-ENOMEM);
899         (*lgh)->lgh_ctxt = ctxt;
900         (*lgh)->lgh_logops = ctxt->loc_logops;
901
902         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
903         if (!raised)
904                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
905         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
906         if (!raised)
907                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
908         if (rc) {
909                 llog_free_handle(*lgh);
910                 *lgh = NULL;
911         }
912         RETURN(rc);
913 }
914 EXPORT_SYMBOL(llog_open);
915
916 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
917 {
918         struct llog_operations  *lop;
919         int                      rc;
920
921         ENTRY;
922
923         rc = llog_handle2ops(loghandle, &lop);
924         if (rc)
925                 GOTO(out, rc);
926         if (lop->lop_close == NULL)
927                 GOTO(out, rc = -EOPNOTSUPP);
928         rc = lop->lop_close(env, loghandle);
929 out:
930         llog_handle_put(loghandle);
931         RETURN(rc);
932 }
933 EXPORT_SYMBOL(llog_close);
934
935 /**
936  * Helper function to get the llog size in records. It is used by MGS
937  * mostly to check that config llog exists and contains data.
938  *
939  * \param[in] env       execution environment
940  * \param[in] ctxt      llog context
941  * \param[in] name      llog name
942  *
943  * \retval              true if there are records in llog besides a header
944  * \retval              false on error or llog without records
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         /* The header is record 1, the llog is still considered as empty
968          * if there is only header */
969         return (rc <= 1);
970 }
971 EXPORT_SYMBOL(llog_is_empty);
972
973 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
974                       struct llog_rec_hdr *rec, void *data)
975 {
976         struct llog_handle      *copy_llh = data;
977
978         /* Append all records */
979         return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
980 }
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);