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