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