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