Whamcloud - gitweb
LU-1302 llog: introduce llog_open
[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 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/obdclass/llog.c
35  *
36  * OST<->MDS recovery logging infrastructure.
37  * Invariants in implementation:
38  * - we do not share logs among different OST<->MDS connections, so that
39  *   if an OST or MDS fails it need only look at log(s) relevant to itself
40  *
41  * Author: Andreas Dilger <adilger@clusterfs.com>
42  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
43  * Author: Mikhail Pershin <tappro@whamcloud.com>
44  */
45
46 #define DEBUG_SUBSYSTEM S_LOG
47
48 #ifndef __KERNEL__
49 #include <liblustre.h>
50 #endif
51
52 #include <obd_class.h>
53 #include <lustre_log.h>
54 #include "llog_internal.h"
55
56 /*
57  * Allocate a new log or catalog handle
58  * Used inside llog_open().
59  */
60 struct llog_handle *llog_alloc_handle(void)
61 {
62         struct llog_handle *loghandle;
63
64         OBD_ALLOC_PTR(loghandle);
65         if (loghandle == NULL)
66                 return ERR_PTR(-ENOMEM);
67
68         cfs_init_rwsem(&loghandle->lgh_lock);
69         cfs_spin_lock_init(&loghandle->lgh_hdr_lock);
70         CFS_INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
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         if (!loghandle)
81                 return;
82
83         if (!loghandle->lgh_hdr)
84                 goto out;
85         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
86                 cfs_list_del_init(&loghandle->u.phd.phd_entry);
87         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
88                 LASSERT(cfs_list_empty(&loghandle->u.chd.chd_head));
89         LASSERT(sizeof(*(loghandle->lgh_hdr)) == LLOG_CHUNK_SIZE);
90         OBD_FREE(loghandle->lgh_hdr, LLOG_CHUNK_SIZE);
91
92 out:
93         OBD_FREE_PTR(loghandle);
94 }
95
96 /* returns negative on error; 0 if success; 1 if success & log destroyed */
97 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
98                     int index)
99 {
100         struct llog_log_hdr *llh = loghandle->lgh_hdr;
101         int rc = 0;
102         ENTRY;
103
104         CDEBUG(D_RPCTRACE, "Canceling %d in log "LPX64"\n",
105                index, loghandle->lgh_id.lgl_oid);
106
107         if (index == 0) {
108                 CERROR("Can't cancel index 0 which is header\n");
109                 RETURN(-EINVAL);
110         }
111
112         cfs_spin_lock(&loghandle->lgh_hdr_lock);
113         if (!ext2_clear_bit(index, llh->llh_bitmap)) {
114                 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
115                 CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n", index);
116                 RETURN(-ENOENT);
117         }
118
119         llh->llh_count--;
120
121         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
122             (llh->llh_count == 1) &&
123             (loghandle->lgh_last_idx == (LLOG_BITMAP_BYTES * 8) - 1)) {
124                 cfs_spin_unlock(&loghandle->lgh_hdr_lock);
125                 rc = llog_destroy(env, loghandle);
126                 if (rc < 0) {
127                         CERROR("%s: can't destroy empty llog #"LPX64"#"LPX64
128                                "#%08x: rc = %d\n",
129                                loghandle->lgh_ctxt->loc_obd->obd_name,
130                                loghandle->lgh_id.lgl_oid,
131                                loghandle->lgh_id.lgl_oseq,
132                                loghandle->lgh_id.lgl_ogen, rc);
133                         GOTO(out_err, rc);
134                 }
135                 RETURN(1);
136         }
137         cfs_spin_unlock(&loghandle->lgh_hdr_lock);
138
139         rc = llog_write_rec(env, loghandle, &llh->llh_hdr, NULL, 0, NULL, 0);
140         if (rc < 0) {
141                 CERROR("%s: fail to write header for llog #"LPX64"#"LPX64
142                        "#%08x: rc = %d\n",
143                        loghandle->lgh_ctxt->loc_obd->obd_name,
144                        loghandle->lgh_id.lgl_oid,
145                        loghandle->lgh_id.lgl_oseq,
146                        loghandle->lgh_id.lgl_ogen, rc);
147                 GOTO(out_err, rc);
148         }
149         RETURN(0);
150 out_err:
151         cfs_spin_lock(&loghandle->lgh_hdr_lock);
152         ext2_set_bit(index, llh->llh_bitmap);
153         llh->llh_count++;
154         cfs_spin_unlock(&loghandle->lgh_hdr_lock);
155         return rc;
156 }
157 EXPORT_SYMBOL(llog_cancel_rec);
158
159 static int llog_read_header(const struct lu_env *env,
160                             struct llog_handle *handle,
161                             struct obd_uuid *uuid)
162 {
163         struct llog_operations *lop;
164         int rc;
165
166         rc = llog_handle2ops(handle, &lop);
167         if (rc)
168                 RETURN(rc);
169
170         if (lop->lop_read_header == NULL)
171                 RETURN(-EOPNOTSUPP);
172
173         rc = lop->lop_read_header(env, handle);
174         if (rc == LLOG_EEMPTY) {
175                 struct llog_log_hdr *llh = handle->lgh_hdr;
176
177                 handle->lgh_last_idx = 0; /* header is record with index 0 */
178                 llh->llh_count = 1;         /* for the header record */
179                 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
180                 llh->llh_hdr.lrh_len = llh->llh_tail.lrt_len = LLOG_CHUNK_SIZE;
181                 llh->llh_hdr.lrh_index = llh->llh_tail.lrt_index = 0;
182                 llh->llh_timestamp = cfs_time_current_sec();
183                 if (uuid)
184                         memcpy(&llh->llh_tgtuuid, uuid,
185                                sizeof(llh->llh_tgtuuid));
186                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
187                 ext2_set_bit(0, llh->llh_bitmap);
188                 rc = 0;
189         }
190         return rc;
191 }
192
193 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
194                      int flags, struct obd_uuid *uuid)
195 {
196         struct llog_log_hdr     *llh;
197         int                      rc;
198
199         ENTRY;
200         LASSERT(handle->lgh_hdr == NULL);
201
202         OBD_ALLOC_PTR(llh);
203         if (llh == NULL)
204                 RETURN(-ENOMEM);
205         handle->lgh_hdr = llh;
206         /* first assign flags to use llog_client_ops */
207         llh->llh_flags = flags;
208         rc = llog_read_header(env, handle, uuid);
209         if (rc == 0) {
210                 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
211                               flags & LLOG_F_IS_CAT) ||
212                              (llh->llh_flags & LLOG_F_IS_CAT &&
213                               flags & LLOG_F_IS_PLAIN))) {
214                         CERROR("%s: llog type is %s but initializing %s\n",
215                                handle->lgh_ctxt->loc_obd->obd_name,
216                                llh->llh_flags & LLOG_F_IS_CAT ?
217                                "catalog" : "plain",
218                                flags & LLOG_F_IS_CAT ? "catalog" : "plain");
219                         GOTO(out, rc = -EINVAL);
220                 } else if (llh->llh_flags &
221                            (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
222                         /*
223                          * it is possible to open llog without specifying llog
224                          * type so it is taken from llh_flags
225                          */
226                         flags = llh->llh_flags;
227                 } else {
228                         /* for some reason the llh_flags has no type set */
229                         CERROR("llog type is not specified!\n");
230                         GOTO(out, rc = -EINVAL);
231                 }
232                 if (unlikely(uuid &&
233                              !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
234                         CERROR("%s: llog uuid mismatch: %s/%s\n",
235                                handle->lgh_ctxt->loc_obd->obd_name,
236                                (char *)uuid->uuid,
237                                (char *)llh->llh_tgtuuid.uuid);
238                         GOTO(out, rc = -EEXIST);
239                 }
240         }
241         if (flags & LLOG_F_IS_CAT) {
242                 LASSERT(cfs_list_empty(&handle->u.chd.chd_head));
243                 CFS_INIT_LIST_HEAD(&handle->u.chd.chd_head);
244                 llh->llh_size = sizeof(struct llog_logid_rec);
245         } else if (!(flags & LLOG_F_IS_PLAIN)) {
246                 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
247                        handle->lgh_ctxt->loc_obd->obd_name,
248                        flags, LLOG_F_IS_CAT, LLOG_F_IS_PLAIN);
249                 rc = -EINVAL;
250         }
251 out:
252         if (rc) {
253                 OBD_FREE_PTR(llh);
254                 handle->lgh_hdr = NULL;
255         }
256         RETURN(rc);
257 }
258 EXPORT_SYMBOL(llog_init_handle);
259
260 static int llog_process_thread(void *arg)
261 {
262         struct llog_process_info        *lpi = arg;
263         struct llog_handle              *loghandle = lpi->lpi_loghandle;
264         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
265         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
266         char                            *buf;
267         __u64                            cur_offset = LLOG_CHUNK_SIZE;
268         __u64                            last_offset;
269         int                              rc = 0, index = 1, last_index;
270         int                              saved_index = 0;
271         int                              last_called_index = 0;
272
273         ENTRY;
274
275         LASSERT(llh);
276
277         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
278         if (!buf) {
279                 lpi->lpi_rc = -ENOMEM;
280                 RETURN(0);
281         }
282
283         if (cd != NULL) {
284                 last_called_index = cd->lpcd_first_idx;
285                 index = cd->lpcd_first_idx + 1;
286         }
287         if (cd != NULL && cd->lpcd_last_idx)
288                 last_index = cd->lpcd_last_idx;
289         else
290                 last_index = LLOG_BITMAP_BYTES * 8 - 1;
291
292         while (rc == 0) {
293                 struct llog_rec_hdr *rec;
294
295                 /* skip records not set in bitmap */
296                 while (index <= last_index &&
297                        !ext2_test_bit(index, llh->llh_bitmap))
298                         ++index;
299
300                 LASSERT(index <= last_index + 1);
301                 if (index == last_index + 1)
302                         break;
303 repeat:
304                 CDEBUG(D_OTHER, "index: %d last_index %d\n",
305                        index, last_index);
306
307                 /* get the buf with our target record; avoid old garbage */
308                 memset(buf, 0, LLOG_CHUNK_SIZE);
309                 last_offset = cur_offset;
310                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
311                                      index, &cur_offset, buf, LLOG_CHUNK_SIZE);
312                 if (rc)
313                         GOTO(out, rc);
314
315                 /* NB: when rec->lrh_len is accessed it is already swabbed
316                  * since it is used at the "end" of the loop and the rec
317                  * swabbing is done at the beginning of the loop. */
318                 for (rec = (struct llog_rec_hdr *)buf;
319                      (char *)rec < buf + LLOG_CHUNK_SIZE;
320                      rec = (struct llog_rec_hdr *)((char *)rec + rec->lrh_len)){
321
322                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
323                                rec, rec->lrh_type);
324
325                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
326                                 lustre_swab_llog_rec(rec);
327
328                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
329                                rec->lrh_type, rec->lrh_index);
330
331                         if (rec->lrh_index == 0) {
332                                 /* probably another rec just got added? */
333                                 if (index <= loghandle->lgh_last_idx)
334                                         GOTO(repeat, rc = 0);
335                                 GOTO(out, rc = 0); /* no more records */
336                         }
337                         if (rec->lrh_len == 0 ||
338                             rec->lrh_len > LLOG_CHUNK_SIZE) {
339                                 CWARN("invalid length %d in llog record for "
340                                       "index %d/%d\n", rec->lrh_len,
341                                       rec->lrh_index, index);
342                                 GOTO(out, rc = -EINVAL);
343                         }
344
345                         if (rec->lrh_index < index) {
346                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
347                                        rec->lrh_index);
348                                 continue;
349                         }
350
351                         CDEBUG(D_OTHER,
352                                "lrh_index: %d lrh_len: %d (%d remains)\n",
353                                rec->lrh_index, rec->lrh_len,
354                                (int)(buf + LLOG_CHUNK_SIZE - (char *)rec));
355
356                         loghandle->lgh_cur_idx = rec->lrh_index;
357                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
358                                                     last_offset;
359
360                         /* if set, process the callback on this record */
361                         if (ext2_test_bit(index, llh->llh_bitmap)) {
362                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
363                                                  lpi->lpi_cbdata);
364                                 last_called_index = index;
365                                 if (rc == LLOG_PROC_BREAK) {
366                                         GOTO(out, rc);
367                                 } else if (rc == LLOG_DEL_RECORD) {
368                                         llog_cancel_rec(lpi->lpi_env,
369                                                         loghandle,
370                                                         rec->lrh_index);
371                                         rc = 0;
372                                 }
373                                 if (rc)
374                                         GOTO(out, rc);
375                         } else {
376                                 CDEBUG(D_OTHER, "Skipped index %d\n", index);
377                         }
378
379                         /* next record, still in buffer? */
380                         ++index;
381                         if (index > last_index)
382                                 GOTO(out, rc = 0);
383                 }
384         }
385
386 out:
387         if (cd != NULL)
388                 cd->lpcd_last_idx = last_called_index;
389
390         OBD_FREE(buf, LLOG_CHUNK_SIZE);
391         lpi->lpi_rc = rc;
392         return 0;
393 }
394
395 #ifdef __KERNEL__
396 static int llog_process_thread_daemonize(void *arg)
397 {
398         struct llog_process_info        *lpi = arg;
399         struct lu_env                    env;
400         int                              rc;
401
402         cfs_daemonize_ctxt("llog_process_thread");
403
404         /* client env has no keys, tags is just 0 */
405         rc = lu_env_init(&env, LCT_LOCAL);
406         if (rc)
407                 goto out;
408         lpi->lpi_env = &env;
409
410         rc = llog_process_thread(arg);
411
412         lu_env_fini(&env);
413 out:
414         cfs_complete(&lpi->lpi_completion);
415         return rc;
416 }
417 #endif
418
419 int llog_process_or_fork(const struct lu_env *env,
420                          struct llog_handle *loghandle,
421                          llog_cb_t cb, void *data, void *catdata, bool fork)
422 {
423         struct llog_process_info *lpi;
424         int                      rc;
425
426         ENTRY;
427
428         OBD_ALLOC_PTR(lpi);
429         if (lpi == NULL) {
430                 CERROR("cannot alloc pointer\n");
431                 RETURN(-ENOMEM);
432         }
433         lpi->lpi_loghandle = loghandle;
434         lpi->lpi_cb        = cb;
435         lpi->lpi_cbdata    = data;
436         lpi->lpi_catdata   = catdata;
437
438 #ifdef __KERNEL__
439         if (fork) {
440                 /* The new thread can't use parent env,
441                  * init the new one in llog_process_thread_daemonize. */
442                 lpi->lpi_env = NULL;
443                 cfs_init_completion(&lpi->lpi_completion);
444                 rc = cfs_create_thread(llog_process_thread_daemonize, lpi,
445                                        CFS_DAEMON_FLAGS);
446                 if (rc < 0) {
447                         CERROR("%s: cannot start thread: rc = %d\n",
448                                loghandle->lgh_ctxt->loc_obd->obd_name, rc);
449                         OBD_FREE_PTR(lpi);
450                         RETURN(rc);
451                 }
452                 cfs_wait_for_completion(&lpi->lpi_completion);
453         } else {
454                 lpi->lpi_env = env;
455                 llog_process_thread(lpi);
456         }
457 #else
458         lpi->lpi_env = env;
459         llog_process_thread(lpi);
460 #endif
461         rc = lpi->lpi_rc;
462         OBD_FREE_PTR(lpi);
463         RETURN(rc);
464 }
465
466 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
467                  llog_cb_t cb, void *data, void *catdata)
468 {
469         return llog_process_or_fork(env, loghandle, cb, data, catdata, true);
470 }
471 EXPORT_SYMBOL(llog_process);
472
473 inline int llog_get_size(struct llog_handle *loghandle)
474 {
475         if (loghandle && loghandle->lgh_hdr)
476                 return loghandle->lgh_hdr->llh_count;
477         return 0;
478 }
479 EXPORT_SYMBOL(llog_get_size);
480
481 int llog_reverse_process(const struct lu_env *env,
482                          struct llog_handle *loghandle, llog_cb_t cb,
483                          void *data, void *catdata)
484 {
485         struct llog_log_hdr *llh = loghandle->lgh_hdr;
486         struct llog_process_cat_data *cd = catdata;
487         void *buf;
488         int rc = 0, first_index = 1, index, idx;
489         ENTRY;
490
491         OBD_ALLOC(buf, LLOG_CHUNK_SIZE);
492         if (!buf)
493                 RETURN(-ENOMEM);
494
495         if (cd != NULL)
496                 first_index = cd->lpcd_first_idx + 1;
497         if (cd != NULL && cd->lpcd_last_idx)
498                 index = cd->lpcd_last_idx;
499         else
500                 index = LLOG_BITMAP_BYTES * 8 - 1;
501
502         while (rc == 0) {
503                 struct llog_rec_hdr *rec;
504                 struct llog_rec_tail *tail;
505
506                 /* skip records not set in bitmap */
507                 while (index >= first_index &&
508                        !ext2_test_bit(index, llh->llh_bitmap))
509                         --index;
510
511                 LASSERT(index >= first_index - 1);
512                 if (index == first_index - 1)
513                         break;
514
515                 /* get the buf with our target record; avoid old garbage */
516                 memset(buf, 0, LLOG_CHUNK_SIZE);
517                 rc = llog_prev_block(env, loghandle, index, buf,
518                                      LLOG_CHUNK_SIZE);
519                 if (rc)
520                         GOTO(out, rc);
521
522                 rec = buf;
523                 idx = rec->lrh_index;
524                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
525                 while (idx < index) {
526                         rec = (void *)rec + rec->lrh_len;
527                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
528                                 lustre_swab_llog_rec(rec);
529                         idx ++;
530                 }
531                 LASSERT(idx == index);
532                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
533
534                 /* process records in buffer, starting where we found one */
535                 while ((void *)tail > buf) {
536                         if (tail->lrt_index == 0)
537                                 GOTO(out, rc = 0); /* no more records */
538
539                         /* if set, process the callback on this record */
540                         if (ext2_test_bit(index, llh->llh_bitmap)) {
541                                 rec = (void *)tail - tail->lrt_len +
542                                       sizeof(*tail);
543
544                                 rc = cb(env, loghandle, rec, data);
545                                 if (rc == LLOG_PROC_BREAK) {
546                                         GOTO(out, rc);
547                                 } else if (rc == LLOG_DEL_RECORD) {
548                                         llog_cancel_rec(env, loghandle,
549                                                         tail->lrt_index);
550                                         rc = 0;
551                                 }
552                                 if (rc)
553                                         GOTO(out, rc);
554                         }
555
556                         /* previous record, still in buffer? */
557                         --index;
558                         if (index < first_index)
559                                 GOTO(out, rc = 0);
560                         tail = (void *)tail - tail->lrt_len;
561                 }
562         }
563
564 out:
565         if (buf)
566                 OBD_FREE(buf, LLOG_CHUNK_SIZE);
567         RETURN(rc);
568 }
569 EXPORT_SYMBOL(llog_reverse_process);
570
571 /**
572  * Helper function to open llog or create it if doesn't exist.
573  * It hides all transaction handling from caller.
574  */
575 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
576                      struct llog_handle **res, struct llog_logid *logid,
577                      char *name)
578 {
579         struct thandle  *th;
580         int              rc;
581
582         ENTRY;
583
584         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
585         if (rc)
586                 RETURN(rc);
587
588         if (llog_exist(*res))
589                 RETURN(0);
590
591         if ((*res)->lgh_obj != NULL) {
592                 struct dt_device *d;
593
594                 d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
595
596                 th = dt_trans_create(env, d);
597                 if (IS_ERR(th))
598                         GOTO(out, rc = PTR_ERR(th));
599
600                 rc = llog_declare_create(env, *res, th);
601                 if (rc == 0) {
602                         rc = dt_trans_start_local(env, d, th);
603                         if (rc == 0)
604                                 rc = llog_create(env, *res, th);
605                 }
606                 dt_trans_stop(env, d, th);
607         } else {
608                 /* lvfs compat code */
609                 LASSERT((*res)->lgh_file == NULL);
610                 rc = llog_create(env, *res, NULL);
611         }
612 out:
613         if (rc)
614                 llog_close(env, *res);
615         RETURN(rc);
616 }
617 EXPORT_SYMBOL(llog_open_create);
618
619 /**
620  * Helper function to delete existent llog.
621  */
622 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
623                struct llog_logid *logid, char *name)
624 {
625         struct llog_handle      *handle;
626         int                      rc = 0, rc2;
627
628         ENTRY;
629
630         /* nothing to erase */
631         if (name == NULL && logid == NULL)
632                 RETURN(0);
633
634         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
635         if (rc < 0)
636                 RETURN(rc);
637
638         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
639         if (rc == 0)
640                 rc = llog_destroy(env, handle);
641
642         rc2 = llog_close(env, handle);
643         if (rc == 0)
644                 rc = rc2;
645         RETURN(rc);
646 }
647 EXPORT_SYMBOL(llog_erase);
648
649 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
650               struct llog_handle **lgh, struct llog_logid *logid,
651               char *name, enum llog_open_param open_param)
652 {
653         int      raised;
654         int      rc;
655
656         ENTRY;
657
658         LASSERT(ctxt);
659         LASSERT(ctxt->loc_logops);
660
661         if (ctxt->loc_logops->lop_open == NULL) {
662                 *lgh = NULL;
663                 RETURN(-EOPNOTSUPP);
664         }
665
666         *lgh = llog_alloc_handle();
667         if (*lgh == NULL)
668                 RETURN(-ENOMEM);
669         (*lgh)->lgh_ctxt = ctxt;
670         (*lgh)->lgh_logops = ctxt->loc_logops;
671
672         raised = cfs_cap_raised(CFS_CAP_SYS_RESOURCE);
673         if (!raised)
674                 cfs_cap_raise(CFS_CAP_SYS_RESOURCE);
675         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
676         if (!raised)
677                 cfs_cap_lower(CFS_CAP_SYS_RESOURCE);
678         if (rc) {
679                 llog_free_handle(*lgh);
680                 *lgh = NULL;
681         }
682         RETURN(rc);
683 }
684 EXPORT_SYMBOL(llog_open);
685
686 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
687 {
688         struct llog_operations  *lop;
689         int                      rc;
690
691         ENTRY;
692
693         rc = llog_handle2ops(loghandle, &lop);
694         if (rc)
695                 GOTO(out, rc);
696         if (lop->lop_close == NULL)
697                 GOTO(out, -EOPNOTSUPP);
698         rc = lop->lop_close(env, loghandle);
699 out:
700         llog_free_handle(loghandle);
701         RETURN(rc);
702 }
703 EXPORT_SYMBOL(llog_close);