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