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