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