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