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