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