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