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