Whamcloud - gitweb
LU-14688 mdt: changelog purge deletes plain llog
[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.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  *
26  * Copyright (c) 2012, 2017, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  *
31  * lustre/obdclass/llog.c
32  *
33  * OST<->MDS recovery logging infrastructure.
34  * Invariants in implementation:
35  * - we do not share logs among different OST<->MDS connections, so that
36  *   if an OST or MDS fails it need only look at log(s) relevant to itself
37  *
38  * Author: Andreas Dilger <adilger@clusterfs.com>
39  * Author: Alex Zhuravlev <bzzz@whamcloud.com>
40  * Author: Mikhail Pershin <tappro@whamcloud.com>
41  */
42
43 #define DEBUG_SUBSYSTEM S_LOG
44
45 #include <linux/pid_namespace.h>
46 #include <linux/kthread.h>
47 #include <llog_swab.h>
48 #include <lustre_log.h>
49 #include <obd_support.h>
50 #include <obd_class.h>
51 #include "llog_internal.h"
52
53 /*
54  * Allocate a new log or catalog handle
55  * Used inside llog_open().
56  */
57 static struct llog_handle *llog_alloc_handle(void)
58 {
59         struct llog_handle *loghandle;
60
61         OBD_ALLOC_PTR(loghandle);
62         if (loghandle == NULL)
63                 return NULL;
64
65         init_rwsem(&loghandle->lgh_lock);
66         mutex_init(&loghandle->lgh_hdr_mutex);
67         init_rwsem(&loghandle->lgh_last_sem);
68         INIT_LIST_HEAD(&loghandle->u.phd.phd_entry);
69         refcount_set(&loghandle->lgh_refcount, 1);
70
71         return loghandle;
72 }
73
74 /*
75  * Free llog handle and header data if exists. Used in llog_close() only
76  */
77 static void llog_free_handle(struct llog_handle *loghandle)
78 {
79         LASSERT(loghandle != NULL);
80
81         /* failed llog_init_handle */
82         if (loghandle->lgh_hdr == NULL)
83                 goto out;
84
85         if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_PLAIN)
86                 LASSERT(list_empty(&loghandle->u.phd.phd_entry));
87         else if (loghandle->lgh_hdr->llh_flags & LLOG_F_IS_CAT)
88                 LASSERT(list_empty(&loghandle->u.chd.chd_head));
89         OBD_FREE_LARGE(loghandle->lgh_hdr, loghandle->lgh_hdr_size);
90 out:
91         OBD_FREE_PTR(loghandle);
92 }
93
94 struct llog_handle *llog_handle_get(struct llog_handle *loghandle)
95 {
96         if (refcount_inc_not_zero(&loghandle->lgh_refcount))
97                 return loghandle;
98         return NULL;
99 }
100
101 int llog_handle_put(const struct lu_env *env, struct llog_handle *loghandle)
102 {
103         int rc = 0;
104
105         if (refcount_dec_and_test(&loghandle->lgh_refcount)) {
106                 const struct llog_operations *lop;
107
108                 rc = llog_handle2ops(loghandle, &lop);
109                 if (!rc) {
110                         if (lop->lop_close)
111                                 rc = lop->lop_close(env, loghandle);
112                         else
113                                 rc = -EOPNOTSUPP;
114                 }
115                 llog_free_handle(loghandle);
116         }
117         return rc;
118 }
119
120 static int llog_declare_destroy(const struct lu_env *env,
121                                 struct llog_handle *handle,
122                                 struct thandle *th)
123 {
124         const struct llog_operations *lop;
125         int rc;
126
127         ENTRY;
128
129         rc = llog_handle2ops(handle, &lop);
130         if (rc)
131                 RETURN(rc);
132         if (lop->lop_declare_destroy == NULL)
133                 RETURN(-EOPNOTSUPP);
134
135         rc = lop->lop_declare_destroy(env, handle, th);
136
137         RETURN(rc);
138 }
139
140 int llog_trans_destroy(const struct lu_env *env, struct llog_handle *handle,
141                        struct thandle *th)
142 {
143         const struct llog_operations *lop;
144         int rc;
145         ENTRY;
146
147         rc = llog_handle2ops(handle, &lop);
148         if (rc < 0)
149                 RETURN(rc);
150         if (lop->lop_destroy == NULL)
151                 RETURN(-EOPNOTSUPP);
152
153         LASSERT(handle->lgh_obj != NULL);
154         if (!llog_exist(handle))
155                 RETURN(0);
156
157         rc = lop->lop_destroy(env, handle, th);
158
159         RETURN(rc);
160 }
161
162 int llog_destroy(const struct lu_env *env, struct llog_handle *handle)
163 {
164         const struct llog_operations *lop;
165         struct dt_device *dt;
166         struct thandle *th;
167         int rc;
168
169         ENTRY;
170
171         rc = llog_handle2ops(handle, &lop);
172         if (rc < 0)
173                 RETURN(rc);
174         if (lop->lop_destroy == NULL)
175                 RETURN(-EOPNOTSUPP);
176
177         if (handle->lgh_obj == NULL) {
178                 /* if lgh_obj == NULL, then it is from client side destroy */
179                 rc = lop->lop_destroy(env, handle, NULL);
180                 RETURN(rc);
181         }
182
183         if (!llog_exist(handle))
184                 RETURN(0);
185
186         dt = lu2dt_dev(handle->lgh_obj->do_lu.lo_dev);
187
188         if (unlikely(unlikely(dt->dd_rdonly)))
189                 RETURN(-EROFS);
190
191         th = dt_trans_create(env, dt);
192         if (IS_ERR(th))
193                 RETURN(PTR_ERR(th));
194
195         rc = llog_declare_destroy(env, handle, th);
196         if (rc != 0)
197                 GOTO(out_trans, rc);
198
199         rc = dt_trans_start_local(env, dt, th);
200         if (rc < 0)
201                 GOTO(out_trans, rc);
202
203         rc = lop->lop_destroy(env, handle, th);
204
205 out_trans:
206         dt_trans_stop(env, dt, th);
207
208         RETURN(rc);
209 }
210 EXPORT_SYMBOL(llog_destroy);
211
212 /* returns negative on error; 0 if success; 1 if success & log destroyed */
213 int llog_cancel_arr_rec(const struct lu_env *env, struct llog_handle *loghandle,
214                         int num, int *index)
215 {
216         struct llog_thread_info *lgi = llog_info(env);
217         struct dt_device        *dt;
218         struct llog_log_hdr     *llh;
219         struct thandle          *th;
220         __u32                    tmp_lgc_index;
221         int                      rc, i = 0;
222         int rc1;
223         bool subtract_count = false;
224
225         ENTRY;
226
227         LASSERT(loghandle != NULL);
228         LASSERT(loghandle->lgh_ctxt != NULL);
229         LASSERT(loghandle->lgh_obj != NULL);
230
231         llh = loghandle->lgh_hdr;
232
233         CDEBUG(D_RPCTRACE, "Canceling %d records, first %d in log "DFID"\n",
234                num, index[0], PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
235
236         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
237
238         if (unlikely(unlikely(dt->dd_rdonly)))
239                 RETURN(0);
240
241         th = dt_trans_create(env, dt);
242         if (IS_ERR(th))
243                 RETURN(PTR_ERR(th));
244
245         rc = llog_declare_write_rec(env, loghandle, &llh->llh_hdr, 0, th);
246         if (rc < 0)
247                 GOTO(out_trans, rc);
248
249         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY)) {
250                 rc = llog_declare_destroy(env, loghandle, th);
251                 if (rc < 0)
252                         GOTO(out_trans, rc);
253         }
254
255         th->th_wait_submit = 1;
256         rc = dt_trans_start_local(env, dt, th);
257         if (rc < 0)
258                 GOTO(out_trans, rc);
259
260         down_write(&loghandle->lgh_lock);
261         /* clear bitmap */
262         mutex_lock(&loghandle->lgh_hdr_mutex);
263         for (i = 0; i < num; ++i) {
264                 if (index[i] == 0) {
265                         CERROR("Can't cancel index 0 which is header\n");
266                         GOTO(out_unlock, rc = -EINVAL);
267                 }
268                 if (!__test_and_clear_bit_le(index[i], LLOG_HDR_BITMAP(llh))) {
269                         CDEBUG(D_RPCTRACE, "Catalog index %u already clear?\n",
270                                index[i]);
271                         GOTO(out_unlock, rc = -ENOENT);
272                 }
273         }
274         loghandle->lgh_hdr->llh_count -= num;
275         subtract_count = true;
276
277         /* Since llog_process_thread use lgi_cookie, it`s better to save them
278          * and restore after using
279          */
280         tmp_lgc_index = lgi->lgi_cookie.lgc_index;
281         /* Pass this index to llog_osd_write_rec(), which will use the index
282          * to only update the necesary bitmap. */
283         lgi->lgi_cookie.lgc_index = index[0];
284         /* update header */
285         rc = llog_write_rec(env, loghandle, &llh->llh_hdr, (num != 1 ? NULL :
286                             &lgi->lgi_cookie), LLOG_HEADER_IDX, th);
287         lgi->lgi_cookie.lgc_index = tmp_lgc_index;
288
289         if (rc != 0)
290                 GOTO(out_unlock, rc);
291
292         if ((llh->llh_flags & LLOG_F_ZAP_WHEN_EMPTY) &&
293             (llh->llh_count == 1) &&
294             ((loghandle->lgh_last_idx == LLOG_HDR_BITMAP_SIZE(llh) - 1) ||
295              (loghandle->u.phd.phd_cat_handle != NULL &&
296               loghandle->u.phd.phd_cat_handle->u.chd.chd_current_log !=
297                 loghandle))) {
298                 /* never try to destroy it again */
299                 llh->llh_flags &= ~LLOG_F_ZAP_WHEN_EMPTY;
300                 rc = llog_trans_destroy(env, loghandle, th);
301                 if (rc < 0) {
302                         /* Sigh, can not destroy the final plain llog, but
303                          * the bitmap has been clearly, so the record can not
304                          * be accessed anymore, let's return 0 for now, and
305                          * the orphan will be handled by LFSCK. */
306                         CERROR("%s: can't destroy empty llog "DFID": rc = %d\n",
307                                loghandle2name(loghandle),
308                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid), rc);
309                         GOTO(out_unlock, rc = 0);
310                 }
311                 rc = LLOG_DEL_PLAIN;
312         }
313
314 out_unlock:
315         if (rc < 0) {
316                 /* restore bitmap while holding a mutex */
317                 if (subtract_count) {
318                         loghandle->lgh_hdr->llh_count += num;
319                         subtract_count = false;
320                 }
321                 for (i = i - 1; i >= 0; i--)
322                         set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
323         }
324         mutex_unlock(&loghandle->lgh_hdr_mutex);
325         up_write(&loghandle->lgh_lock);
326 out_trans:
327         rc1 = dt_trans_stop(env, dt, th);
328         if (rc == 0)
329                 rc = rc1;
330         if (rc1 < 0) {
331                 mutex_lock(&loghandle->lgh_hdr_mutex);
332                 if (subtract_count)
333                         loghandle->lgh_hdr->llh_count += num;
334                 for (i = i - 1; i >= 0; i--)
335                         set_bit_le(index[i], LLOG_HDR_BITMAP(llh));
336                 mutex_unlock(&loghandle->lgh_hdr_mutex);
337         }
338         RETURN(rc);
339 }
340
341 int llog_cancel_rec(const struct lu_env *env, struct llog_handle *loghandle,
342                     int index)
343 {
344         return llog_cancel_arr_rec(env, loghandle, 1, &index);
345 }
346
347 int llog_read_header(const struct lu_env *env, struct llog_handle *handle,
348                      const struct obd_uuid *uuid)
349 {
350         const struct llog_operations *lop;
351         int rc;
352         ENTRY;
353
354         rc = llog_handle2ops(handle, &lop);
355         if (rc)
356                 RETURN(rc);
357
358         if (lop->lop_read_header == NULL)
359                 RETURN(-EOPNOTSUPP);
360
361         rc = lop->lop_read_header(env, handle);
362         if (rc == LLOG_EEMPTY) {
363                 struct llog_log_hdr *llh = handle->lgh_hdr;
364
365                 /* lrh_len should be initialized in llog_init_handle */
366                 handle->lgh_last_idx = 0; /* header is record with index 0 */
367                 llh->llh_count = 1;         /* for the header record */
368                 llh->llh_hdr.lrh_type = LLOG_HDR_MAGIC;
369                 LASSERT(handle->lgh_ctxt->loc_chunk_size >=
370                                                 LLOG_MIN_CHUNK_SIZE);
371                 llh->llh_hdr.lrh_len = handle->lgh_ctxt->loc_chunk_size;
372                 llh->llh_hdr.lrh_index = 0;
373                 llh->llh_timestamp = ktime_get_real_seconds();
374                 if (uuid)
375                         memcpy(&llh->llh_tgtuuid, uuid,
376                                sizeof(llh->llh_tgtuuid));
377                 llh->llh_bitmap_offset = offsetof(typeof(*llh), llh_bitmap);
378                 /* Since update llog header might also call this function,
379                  * let's reset the bitmap to 0 here */
380                 memset(LLOG_HDR_BITMAP(llh), 0, llh->llh_hdr.lrh_len -
381                                                 llh->llh_bitmap_offset -
382                                                 sizeof(llh->llh_tail));
383                 set_bit_le(0, LLOG_HDR_BITMAP(llh));
384                 LLOG_HDR_TAIL(llh)->lrt_len = llh->llh_hdr.lrh_len;
385                 LLOG_HDR_TAIL(llh)->lrt_index = llh->llh_hdr.lrh_index;
386                 rc = 0;
387         }
388         RETURN(rc);
389 }
390 EXPORT_SYMBOL(llog_read_header);
391
392 int llog_init_handle(const struct lu_env *env, struct llog_handle *handle,
393                      int flags, struct obd_uuid *uuid)
394 {
395         struct llog_log_hdr     *llh;
396         enum llog_flag           fmt = flags & LLOG_F_EXT_MASK;
397         int                      rc;
398         int                     chunk_size = handle->lgh_ctxt->loc_chunk_size;
399         ENTRY;
400
401         LASSERT(handle->lgh_hdr == NULL);
402
403         LASSERT(chunk_size >= LLOG_MIN_CHUNK_SIZE);
404         OBD_ALLOC_LARGE(llh, chunk_size);
405         if (llh == NULL)
406                 RETURN(-ENOMEM);
407
408         handle->lgh_hdr = llh;
409         handle->lgh_hdr_size = chunk_size;
410         /* first assign flags to use llog_client_ops */
411         llh->llh_flags = flags;
412         rc = llog_read_header(env, handle, uuid);
413         if (rc == 0) {
414                 if (unlikely((llh->llh_flags & LLOG_F_IS_PLAIN &&
415                               flags & LLOG_F_IS_CAT) ||
416                              (llh->llh_flags & LLOG_F_IS_CAT &&
417                               flags & LLOG_F_IS_PLAIN))) {
418                         CERROR("%s: llog type is %s but initializing %s\n",
419                                loghandle2name(handle),
420                                llh->llh_flags & LLOG_F_IS_CAT ?
421                                "catalog" : "plain",
422                                flags & LLOG_F_IS_CAT ? "catalog" : "plain");
423                         GOTO(out, rc = -EINVAL);
424                 } else if (llh->llh_flags &
425                            (LLOG_F_IS_PLAIN | LLOG_F_IS_CAT)) {
426                         /*
427                          * it is possible to open llog without specifying llog
428                          * type so it is taken from llh_flags
429                          */
430                         flags = llh->llh_flags;
431                 } else {
432                         /* for some reason the llh_flags has no type set */
433                         CERROR("llog type is not specified!\n");
434                         GOTO(out, rc = -EINVAL);
435                 }
436                 if (unlikely(uuid &&
437                              !obd_uuid_equals(uuid, &llh->llh_tgtuuid))) {
438                         CERROR("%s: llog uuid mismatch: %s/%s\n",
439                                loghandle2name(handle),
440                                (char *)uuid->uuid,
441                                (char *)llh->llh_tgtuuid.uuid);
442                         GOTO(out, rc = -EEXIST);
443                 }
444         }
445         if (flags & LLOG_F_IS_CAT) {
446                 LASSERT(list_empty(&handle->u.chd.chd_head));
447                 INIT_LIST_HEAD(&handle->u.chd.chd_head);
448                 llh->llh_size = sizeof(struct llog_logid_rec);
449                 llh->llh_flags |= LLOG_F_IS_FIXSIZE;
450         } else if (!(flags & LLOG_F_IS_PLAIN)) {
451                 CERROR("%s: unknown flags: %#x (expected %#x or %#x)\n",
452                        loghandle2name(handle), flags, LLOG_F_IS_CAT,
453                        LLOG_F_IS_PLAIN);
454                 rc = -EINVAL;
455         }
456         llh->llh_flags |= fmt;
457 out:
458         if (rc) {
459                 OBD_FREE_LARGE(llh, chunk_size);
460                 handle->lgh_hdr = NULL;
461         }
462         RETURN(rc);
463 }
464 EXPORT_SYMBOL(llog_init_handle);
465
466 int llog_verify_record(const struct llog_handle *llh, struct llog_rec_hdr *rec)
467 {
468         int chunk_size = llh->lgh_hdr->llh_hdr.lrh_len;
469
470         if (rec->lrh_len == 0 || rec->lrh_len > chunk_size) {
471                 CERROR("%s: record is too large: %d > %d\n",
472                        loghandle2name(llh), rec->lrh_len, chunk_size);
473                 return -EINVAL;
474         }
475         if (rec->lrh_index >= LLOG_HDR_BITMAP_SIZE(llh->lgh_hdr)) {
476                 CERROR("%s: index is too high: %d\n",
477                        loghandle2name(llh), rec->lrh_index);
478                 return -EINVAL;
479         }
480         if ((rec->lrh_type & LLOG_OP_MASK) != LLOG_OP_MAGIC) {
481                 CERROR("%s: magic %x is bad\n",
482                        loghandle2name(llh), rec->lrh_type);
483                 return -EINVAL;
484         }
485
486         return 0;
487 }
488 EXPORT_SYMBOL(llog_verify_record);
489
490 static int llog_process_thread(void *arg)
491 {
492         struct llog_process_info        *lpi = arg;
493         struct llog_handle              *loghandle = lpi->lpi_loghandle;
494         struct llog_log_hdr             *llh = loghandle->lgh_hdr;
495         struct llog_process_cat_data    *cd  = lpi->lpi_catdata;
496         struct llog_thread_info         *lti;
497         char                            *buf;
498         size_t                           chunk_size;
499         __u64                            cur_offset;
500         int                              rc = 0, index = 1, last_index;
501         int                              saved_index = 0;
502         int                              last_called_index = 0;
503         bool                             repeated = false;
504         bool                            refresh_idx = false;
505
506         ENTRY;
507
508         if (llh == NULL)
509                 RETURN(-EINVAL);
510
511         lti = lpi->lpi_env == NULL ? NULL : llog_info(lpi->lpi_env);
512
513         cur_offset = chunk_size = llh->llh_hdr.lrh_len;
514         /* expect chunk_size to be power of two */
515         LASSERT(is_power_of_2(chunk_size));
516
517         OBD_ALLOC_LARGE(buf, chunk_size);
518         if (buf == NULL) {
519                 lpi->lpi_rc = -ENOMEM;
520                 RETURN(0);
521         }
522
523         if (cd != NULL) {
524                 last_called_index = cd->lpcd_first_idx;
525                 index = cd->lpcd_first_idx + 1;
526         }
527         if (cd != NULL && cd->lpcd_last_idx)
528                 last_index = cd->lpcd_last_idx;
529         else
530                 last_index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
531
532         while (rc == 0) {
533                 struct llog_rec_hdr *rec;
534                 off_t chunk_offset = 0;
535                 unsigned int buf_offset = 0;
536                 int     lh_last_idx;
537                 int     synced_idx = 0;
538
539                 /* skip records not set in bitmap */
540                 while (index <= last_index &&
541                        !test_bit_le(index, LLOG_HDR_BITMAP(llh)))
542                         ++index;
543
544                 /* There are no indices prior the last_index */
545                 if (index > last_index)
546                         break;
547
548                 CDEBUG(D_OTHER, "index: %d last_index %d\n", index,
549                        last_index);
550
551 repeat:
552                 /* get the buf with our target record; avoid old garbage */
553                 memset(buf, 0, chunk_size);
554                 /* the record index for outdated chunk data */
555                 /* it is safe to process buffer until saved lgh_last_idx */
556                 lh_last_idx = LLOG_HDR_TAIL(llh)->lrt_index;
557                 rc = llog_next_block(lpi->lpi_env, loghandle, &saved_index,
558                                      index, &cur_offset, buf, chunk_size);
559                 if (repeated && rc)
560                         CDEBUG(D_OTHER, "cur_offset %llu, chunk_offset %llu,"
561                                " buf_offset %u, rc = %d\n", cur_offset,
562                                (__u64)chunk_offset, buf_offset, rc);
563                 if (rc == -ESTALE)
564                         GOTO(out, rc = 0);
565                 /* we`ve tried to reread the chunk, but there is no
566                  * new records */
567                 if (rc == -EIO && repeated && (chunk_offset + buf_offset) ==
568                     cur_offset)
569                         GOTO(out, rc = 0);
570                 if (rc != 0)
571                         GOTO(out, rc);
572
573                 /* NB: after llog_next_block() call the cur_offset is the
574                  * offset of the next block after read one.
575                  * The absolute offset of the current chunk is calculated
576                  * from cur_offset value and stored in chunk_offset variable.
577                  */
578                 if ((cur_offset & (chunk_size - 1)) != 0)
579                         chunk_offset = cur_offset & ~(chunk_size - 1);
580                 else
581                         chunk_offset = cur_offset - chunk_size;
582
583                 /* NB: when rec->lrh_len is accessed it is already swabbed
584                  * since it is used at the "end" of the loop and the rec
585                  * swabbing is done at the beginning of the loop. */
586                 for (rec = (struct llog_rec_hdr *)(buf + buf_offset);
587                      (char *)rec < buf + chunk_size;
588                      rec = llog_rec_hdr_next(rec)) {
589
590                         CDEBUG(D_OTHER, "processing rec 0x%p type %#x\n",
591                                rec, rec->lrh_type);
592
593                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
594                                 lustre_swab_llog_rec(rec);
595
596                         CDEBUG(D_OTHER, "after swabbing, type=%#x idx=%d\n",
597                                rec->lrh_type, rec->lrh_index);
598
599                         if (index == (synced_idx + 1) &&
600                             synced_idx == LLOG_HDR_TAIL(llh)->lrt_index)
601                                 GOTO(out, rc = 0);
602
603                         if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
604                                 cfs_fail_val == (unsigned int)
605                                         (loghandle->lgh_id.lgl_oi.oi.oi_id &
606                                          0xFFFFFFFF)) {
607                                 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
608                         }
609
610                         /* the bitmap could be changed during processing
611                          * records from the chunk. For wrapped catalog
612                          * it means we can read deleted record and try to
613                          * process it. Check this case and reread the chunk.
614                          * It is safe to process to lh_last_idx, including
615                          * lh_last_idx if it was synced. We can not do <=
616                          * comparison, cause for wrapped catalog lgh_last_idx
617                          * could be less than index. So we detect last index
618                          * for processing as index == lh_last_idx+1. But when
619                          * catalog is wrapped and full lgh_last_idx=llh_cat_idx,
620                          * the first processing index is llh_cat_idx+1.The
621                          * exception is !(lgh_last_idx == llh_cat_idx &&
622                          * index == llh_cat_idx + 1), and after simplification
623                          * it turns to
624                          * lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index
625                          * This exception is working for catalog only.
626                          */
627
628                         if ((index == lh_last_idx && synced_idx != index) ||
629                             (index == (lh_last_idx + 1) &&
630                              lh_last_idx != LLOG_HDR_TAIL(llh)->lrt_index) ||
631                             (rec->lrh_index == 0 && !repeated)) {
632
633                                 /* save offset inside buffer for the re-read */
634                                 buf_offset = (char *)rec - (char *)buf;
635                                 cur_offset = chunk_offset;
636                                 repeated = true;
637                                 /* We need to be sure lgh_last_idx
638                                  * record was saved to disk
639                                  */
640                                 down_read(&loghandle->lgh_last_sem);
641                                 synced_idx = LLOG_HDR_TAIL(llh)->lrt_index;
642                                 up_read(&loghandle->lgh_last_sem);
643                                 CDEBUG(D_OTHER, "synced_idx: %d\n", synced_idx);
644                                 goto repeat;
645
646                         }
647
648                         repeated = false;
649
650                         rc = llog_verify_record(loghandle, rec);
651                         if (rc) {
652                                 CERROR("%s: invalid record in llog "DFID
653                                        " record for index %d/%d: rc = %d\n",
654                                        loghandle2name(loghandle),
655                                        PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
656                                        rec->lrh_index, index, rc);
657                                 /*
658                                  * the block seem to be corrupted, let's try
659                                  * with the next one. reset rc to go to the
660                                  * next chunk.
661                                  */
662                                 refresh_idx = true;
663                                 index = 0;
664                                 GOTO(repeat, rc = 0);
665                         }
666
667                         if (rec->lrh_index < index) {
668                                 CDEBUG(D_OTHER, "skipping lrh_index %d\n",
669                                        rec->lrh_index);
670                                 continue;
671                         }
672
673                         if (rec->lrh_index != index) {
674                                 /*
675                                  * the last time we couldn't parse the block due
676                                  * to corruption, thus has no idea about the
677                                  * next index, take it from the block, once.
678                                  */
679                                 if (refresh_idx) {
680                                         refresh_idx = false;
681                                         index = rec->lrh_index;
682                                 } else {
683                                         CERROR("%s: "DFID" Invalid record: index"
684                                                " %u but expected %u\n",
685                                                loghandle2name(loghandle),
686                                                PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
687                                                rec->lrh_index, index);
688                                         GOTO(out, rc = -ERANGE);
689                                 }
690                         }
691
692                         CDEBUG(D_OTHER,
693                                "lrh_index: %d lrh_len: %d (%d remains)\n",
694                                rec->lrh_index, rec->lrh_len,
695                                (int)(buf + chunk_size - (char *)rec));
696
697                         /* lgh_cur_offset is used only at llog_test_3 */
698                         loghandle->lgh_cur_offset = (char *)rec - (char *)buf +
699                                                     chunk_offset;
700
701                         /* if set, process the callback on this record */
702                         if (test_bit_le(index, LLOG_HDR_BITMAP(llh))) {
703                                 struct llog_cookie *lgc;
704                                 __u64   tmp_off;
705                                 int     tmp_idx;
706
707                                 CDEBUG((llh->llh_flags & LLOG_F_IS_CAT ?
708                                         D_HA : D_OTHER),
709                                        "index: %d, lh_last_idx: %d "
710                                        "synced_idx: %d lgh_last_idx: %d\n",
711                                        index, lh_last_idx, synced_idx,
712                                        loghandle->lgh_last_idx);
713
714                                 if (lti != NULL) {
715                                         lgc = &lti->lgi_cookie;
716                                         /* store lu_env for recursive calls */
717                                         tmp_off = lgc->lgc_offset;
718                                         tmp_idx = lgc->lgc_index;
719
720                                         lgc->lgc_offset = (char *)rec -
721                                                 (char *)buf + chunk_offset;
722                                         lgc->lgc_index = rec->lrh_index;
723                                 }
724                                 /* using lu_env for passing record offset to
725                                  * llog_write through various callbacks */
726                                 rc = lpi->lpi_cb(lpi->lpi_env, loghandle, rec,
727                                                  lpi->lpi_cbdata);
728                                 last_called_index = index;
729
730                                 if (lti != NULL) {
731                                         lgc->lgc_offset = tmp_off;
732                                         lgc->lgc_index = tmp_idx;
733                                 }
734
735                                 if (rc == LLOG_PROC_BREAK) {
736                                         GOTO(out, rc);
737                                 } else if (rc == LLOG_DEL_RECORD) {
738                                         rc = llog_cancel_rec(lpi->lpi_env,
739                                                              loghandle,
740                                                              rec->lrh_index);
741                                         /* Allow parallel cancelling, ENOENT
742                                          * means record was canceled at another
743                                          * processing thread or callback
744                                          */
745                                         if (rc == -ENOENT)
746                                                 rc = 0;
747                                 }
748                                 if (rc)
749                                         GOTO(out, rc);
750                                 /* some stupid callbacks directly cancel records
751                                  * and delete llog. Check it and stop
752                                  * processing. */
753                                 if (loghandle->lgh_hdr == NULL ||
754                                     loghandle->lgh_hdr->llh_count == 1)
755                                         GOTO(out, rc = 0);
756                         }
757                         /* exit if the last index is reached */
758                         if (index >= last_index)
759                                 GOTO(out, rc = 0);
760                         ++index;
761                 }
762         }
763
764 out:
765         CDEBUG(D_HA, "stop processing %s "DOSTID":%x index %d count %d\n",
766                ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" : "plain"),
767                POSTID(&loghandle->lgh_id.lgl_oi), loghandle->lgh_id.lgl_ogen,
768                index, llh->llh_count);
769
770         if (cd != NULL)
771                 cd->lpcd_last_idx = last_called_index;
772
773         if (unlikely(rc == -EIO && loghandle->lgh_obj != NULL)) {
774                 if (dt_object_remote(loghandle->lgh_obj)) {
775                         /* If it is remote object, then -EIO might means
776                          * disconnection or eviction, let's return -EAGAIN,
777                          * so for update recovery log processing, it will
778                          * retry until the umount or abort recovery, see
779                          * lod_sub_recovery_thread() */
780                         CERROR("%s retry remote llog process\n",
781                                loghandle2name(loghandle));
782                         rc = -EAGAIN;
783                 } else {
784                         /* something bad happened to the processing of a local
785                          * llog file, probably I/O error or the log got
786                          * corrupted to be able to finally release the log we
787                          * discard any remaining bits in the header */
788                         CERROR("%s: Local llog found corrupted #"DOSTID":%x"
789                                " %s index %d count %d\n",
790                                loghandle2name(loghandle),
791                                POSTID(&loghandle->lgh_id.lgl_oi),
792                                loghandle->lgh_id.lgl_ogen,
793                                ((llh->llh_flags & LLOG_F_IS_CAT) ? "catalog" :
794                                 "plain"), index, llh->llh_count);
795
796                         while (index <= last_index) {
797                                 if (test_bit_le(index,
798                                                   LLOG_HDR_BITMAP(llh)) != 0)
799                                         llog_cancel_rec(lpi->lpi_env, loghandle,
800                                                         index);
801                                 index++;
802                         }
803                         rc = 0;
804                 }
805         }
806
807         OBD_FREE_LARGE(buf, chunk_size);
808         lpi->lpi_rc = rc;
809         return 0;
810 }
811
812 static int llog_process_thread_daemonize(void *arg)
813 {
814         struct llog_process_info        *lpi = arg;
815         struct lu_env                    env;
816         int                              rc;
817         struct nsproxy                  *new_ns, *curr_ns = current->nsproxy;
818
819         task_lock(lpi->lpi_reftask);
820         new_ns = lpi->lpi_reftask->nsproxy;
821         if (curr_ns != new_ns) {
822                 get_nsproxy(new_ns);
823
824                 current->nsproxy = new_ns;
825                 /* XXX: we should call put_nsproxy() instead of
826                  * atomic_dec(&ns->count) directly. But put_nsproxy() cannot be
827                  * used outside of the kernel itself, because it calls
828                  * free_nsproxy() which is not exported by the kernel
829                  * (defined in kernel/nsproxy.c) */
830                 if (curr_ns)
831                         atomic_dec(&curr_ns->count);
832         }
833         task_unlock(lpi->lpi_reftask);
834
835         /* client env has no keys, tags is just 0 */
836         rc = lu_env_init(&env, LCT_LOCAL | LCT_MG_THREAD);
837         if (rc)
838                 goto out;
839         lpi->lpi_env = &env;
840
841         rc = llog_process_thread(arg);
842
843         lu_env_fini(&env);
844 out:
845         complete(&lpi->lpi_completion);
846         return rc;
847 }
848
849 int llog_process_or_fork(const struct lu_env *env,
850                          struct llog_handle *loghandle,
851                          llog_cb_t cb, void *data, void *catdata, bool fork)
852 {
853         struct llog_process_info *lpi;
854         struct llog_process_data *d = data;
855         struct llog_process_cat_data *cd = catdata;
856         __u32 flags = loghandle->lgh_hdr->llh_flags;
857         int rc;
858
859         ENTRY;
860
861         OBD_ALLOC_PTR(lpi);
862         if (lpi == NULL) {
863                 CERROR("cannot alloc pointer\n");
864                 RETURN(-ENOMEM);
865         }
866         lpi->lpi_loghandle = loghandle;
867         lpi->lpi_cb        = cb;
868         lpi->lpi_cbdata    = data;
869         lpi->lpi_catdata   = catdata;
870
871         CDEBUG(D_OTHER, "Processing "DFID" flags 0x%03x startcat %d startidx %d first_idx %d last_idx %d\n",
872                PFID(&loghandle->lgh_id.lgl_oi.oi_fid), flags,
873                (flags & LLOG_F_IS_CAT) && d ? d->lpd_startcat : -1,
874                (flags & LLOG_F_IS_CAT) && d ? d->lpd_startidx : -1,
875                cd ? cd->lpcd_first_idx : -1, cd ? cd->lpcd_last_idx : -1);
876         if (fork) {
877                 struct task_struct *task;
878
879                 /* The new thread can't use parent env,
880                  * init the new one in llog_process_thread_daemonize. */
881                 lpi->lpi_env = NULL;
882                 init_completion(&lpi->lpi_completion);
883                 /* take reference to current, so that
884                  * llog_process_thread_daemonize() can use it to switch to
885                  * namespace associated with current  */
886                 lpi->lpi_reftask = current;
887                 task = kthread_run(llog_process_thread_daemonize, lpi,
888                                    "llog_process_thread");
889                 if (IS_ERR(task)) {
890                         rc = PTR_ERR(task);
891                         CERROR("%s: cannot start thread: rc = %d\n",
892                                loghandle2name(loghandle), rc);
893                         GOTO(out_lpi, rc);
894                 }
895                 wait_for_completion(&lpi->lpi_completion);
896         } else {
897                 lpi->lpi_env = env;
898                 llog_process_thread(lpi);
899         }
900         rc = lpi->lpi_rc;
901
902 out_lpi:
903         OBD_FREE_PTR(lpi);
904         RETURN(rc);
905 }
906 EXPORT_SYMBOL(llog_process_or_fork);
907
908 int llog_process(const struct lu_env *env, struct llog_handle *loghandle,
909                  llog_cb_t cb, void *data, void *catdata)
910 {
911         int rc;
912         rc = llog_process_or_fork(env, loghandle, cb, data, catdata, true);
913         return rc == LLOG_DEL_PLAIN ? 0 : rc;
914 }
915 EXPORT_SYMBOL(llog_process);
916
917 static inline const struct cred *llog_raise_resource(void)
918 {
919         struct cred *cred = NULL;
920
921         if (cap_raised(current_cap(), CAP_SYS_RESOURCE))
922                 return cred;
923
924         cred = prepare_creds();
925         if (!cred)
926                 return cred;
927
928         cap_raise(cred->cap_effective, CAP_SYS_RESOURCE);
929         return override_creds(cred);
930 }
931
932 static inline void llog_restore_resource(const struct cred *old_cred)
933 {
934         if (old_cred)
935                 revert_creds(old_cred);
936 }
937
938 int llog_reverse_process(const struct lu_env *env,
939                          struct llog_handle *loghandle, llog_cb_t cb,
940                          void *data, void *catdata)
941 {
942         struct llog_log_hdr *llh = loghandle->lgh_hdr;
943         struct llog_process_cat_data *cd = catdata;
944         void *buf;
945         int rc = 0, first_index = 1, index, idx;
946         __u32   chunk_size = llh->llh_hdr.lrh_len;
947         ENTRY;
948
949         OBD_ALLOC_LARGE(buf, chunk_size);
950         if (buf == NULL)
951                 RETURN(-ENOMEM);
952
953         if (cd != NULL)
954                 first_index = cd->lpcd_first_idx + 1;
955         if (cd != NULL && cd->lpcd_last_idx)
956                 index = cd->lpcd_last_idx;
957         else
958                 index = LLOG_HDR_BITMAP_SIZE(llh) - 1;
959
960         while (rc == 0) {
961                 struct llog_rec_hdr *rec;
962                 struct llog_rec_tail *tail;
963
964                 /* skip records not set in bitmap */
965                 while (index >= first_index &&
966                        !test_bit_le(index, LLOG_HDR_BITMAP(llh)))
967                         --index;
968
969                 LASSERT(index >= first_index - 1);
970                 if (index == first_index - 1)
971                         break;
972
973                 /* get the buf with our target record; avoid old garbage */
974                 memset(buf, 0, chunk_size);
975                 rc = llog_prev_block(env, loghandle, index, buf, chunk_size);
976                 if (rc)
977                         GOTO(out, rc);
978
979                 rec = buf;
980                 idx = rec->lrh_index;
981                 CDEBUG(D_RPCTRACE, "index %u : idx %u\n", index, idx);
982                 while (idx < index) {
983                         rec = (void *)rec + rec->lrh_len;
984                         if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
985                                 lustre_swab_llog_rec(rec);
986                         idx ++;
987                 }
988                 LASSERT(idx == index);
989                 tail = (void *)rec + rec->lrh_len - sizeof(*tail);
990
991                 /* process records in buffer, starting where we found one */
992                 while ((void *)tail > buf) {
993                         if (tail->lrt_index == 0)
994                                 GOTO(out, rc = 0); /* no more records */
995
996                         /* if set, process the callback on this record */
997                         if (test_bit_le(index, LLOG_HDR_BITMAP(llh))) {
998                                 rec = (void *)tail - tail->lrt_len +
999                                       sizeof(*tail);
1000
1001                                 rc = cb(env, loghandle, rec, data);
1002                                 if (rc == LLOG_PROC_BREAK) {
1003                                         GOTO(out, rc);
1004                                 } else if (rc == LLOG_DEL_RECORD) {
1005                                         rc = llog_cancel_rec(env, loghandle,
1006                                                              tail->lrt_index);
1007                                 }
1008                                 if (rc)
1009                                         GOTO(out, rc);
1010                         }
1011
1012                         /* previous record, still in buffer? */
1013                         --index;
1014                         if (index < first_index)
1015                                 GOTO(out, rc = 0);
1016                         tail = (void *)tail - tail->lrt_len;
1017                 }
1018         }
1019
1020 out:
1021         if (buf != NULL)
1022                 OBD_FREE_LARGE(buf, chunk_size);
1023         RETURN(rc);
1024 }
1025 EXPORT_SYMBOL(llog_reverse_process);
1026
1027 /**
1028  * new llog API
1029  *
1030  * API functions:
1031  *      llog_open - open llog, may not exist
1032  *      llog_exist - check if llog exists
1033  *      llog_close - close opened llog, pair for open, frees llog_handle
1034  *      llog_declare_create - declare llog creation
1035  *      llog_create - create new llog on disk, need transaction handle
1036  *      llog_declare_write_rec - declaration of llog write
1037  *      llog_write_rec - write llog record on disk, need transaction handle
1038  *      llog_declare_add - declare llog catalog record addition
1039  *      llog_add - add llog record in catalog, need transaction handle
1040  */
1041 int llog_exist(struct llog_handle *loghandle)
1042 {
1043         const struct llog_operations *lop;
1044         int rc;
1045
1046         ENTRY;
1047
1048         rc = llog_handle2ops(loghandle, &lop);
1049         if (rc)
1050                 RETURN(rc);
1051         if (lop->lop_exist == NULL)
1052                 RETURN(-EOPNOTSUPP);
1053
1054         rc = lop->lop_exist(loghandle);
1055         RETURN(rc);
1056 }
1057 EXPORT_SYMBOL(llog_exist);
1058
1059 int llog_declare_create(const struct lu_env *env,
1060                         struct llog_handle *loghandle, struct thandle *th)
1061 {
1062         const struct cred *old_cred;
1063         const struct llog_operations *lop;
1064         int rc;
1065
1066         ENTRY;
1067
1068         rc = llog_handle2ops(loghandle, &lop);
1069         if (rc)
1070                 RETURN(rc);
1071         if (lop->lop_declare_create == NULL)
1072                 RETURN(-EOPNOTSUPP);
1073
1074         old_cred = llog_raise_resource();
1075         rc = lop->lop_declare_create(env, loghandle, th);
1076         llog_restore_resource(old_cred);
1077         RETURN(rc);
1078 }
1079
1080 int llog_create(const struct lu_env *env, struct llog_handle *handle,
1081                 struct thandle *th)
1082 {
1083         const struct cred *old_cred;
1084         const struct llog_operations *lop;
1085         int rc;
1086
1087         ENTRY;
1088
1089         rc = llog_handle2ops(handle, &lop);
1090         if (rc)
1091                 RETURN(rc);
1092         if (lop->lop_create == NULL)
1093                 RETURN(-EOPNOTSUPP);
1094
1095         old_cred = llog_raise_resource();
1096         rc = lop->lop_create(env, handle, th);
1097         llog_restore_resource(old_cred);
1098         RETURN(rc);
1099 }
1100
1101 int llog_declare_write_rec(const struct lu_env *env,
1102                            struct llog_handle *handle,
1103                            struct llog_rec_hdr *rec, int idx,
1104                            struct thandle *th)
1105 {
1106         const struct cred *old_cred;
1107         const struct llog_operations *lop;
1108         int rc;
1109
1110         ENTRY;
1111
1112         rc = llog_handle2ops(handle, &lop);
1113         if (rc)
1114                 RETURN(rc);
1115         LASSERT(lop);
1116         if (lop->lop_declare_write_rec == NULL)
1117                 RETURN(-EOPNOTSUPP);
1118
1119         old_cred = llog_raise_resource();
1120         rc = lop->lop_declare_write_rec(env, handle, rec, idx, th);
1121         llog_restore_resource(old_cred);
1122         RETURN(rc);
1123 }
1124
1125 int llog_write_rec(const struct lu_env *env, struct llog_handle *handle,
1126                    struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1127                    int idx, struct thandle *th)
1128 {
1129         const struct cred *old_cred;
1130         const struct llog_operations *lop;
1131         int rc, buflen;
1132
1133         ENTRY;
1134
1135         /* API sanity checks */
1136         if (handle == NULL) {
1137                 CERROR("loghandle is missed\n");
1138                 RETURN(-EPROTO);
1139         } else if (handle->lgh_obj == NULL) {
1140                 CERROR("loghandle %p with NULL object\n",
1141                         handle);
1142                 RETURN(-EPROTO);
1143         } else if (th == NULL) {
1144                 CERROR("%s: missed transaction handle\n",
1145                        loghandle2name(handle));
1146                 RETURN(-EPROTO);
1147         } else if (handle->lgh_hdr == NULL) {
1148                 CERROR("%s: loghandle %p with no header\n",
1149                        loghandle2name(handle), handle);
1150                 RETURN(-EPROTO);
1151         }
1152
1153         rc = llog_handle2ops(handle, &lop);
1154         if (rc)
1155                 RETURN(rc);
1156
1157         if (lop->lop_write_rec == NULL)
1158                 RETURN(-EOPNOTSUPP);
1159
1160         buflen = rec->lrh_len;
1161         LASSERT(cfs_size_round(buflen) == buflen);
1162
1163         old_cred = llog_raise_resource();
1164         rc = lop->lop_write_rec(env, handle, rec, logcookies, idx, th);
1165         llog_restore_resource(old_cred);
1166         RETURN(rc);
1167 }
1168
1169 int llog_add(const struct lu_env *env, struct llog_handle *lgh,
1170              struct llog_rec_hdr *rec, struct llog_cookie *logcookies,
1171              struct thandle *th)
1172 {
1173         const struct cred *old_cred;
1174         int rc;
1175
1176         ENTRY;
1177
1178         if (lgh->lgh_logops->lop_add == NULL)
1179                 RETURN(-EOPNOTSUPP);
1180
1181         old_cred = llog_raise_resource();
1182         rc = lgh->lgh_logops->lop_add(env, lgh, rec, logcookies, th);
1183         llog_restore_resource(old_cred);
1184         RETURN(rc);
1185 }
1186 EXPORT_SYMBOL(llog_add);
1187
1188 int llog_declare_add(const struct lu_env *env, struct llog_handle *lgh,
1189                      struct llog_rec_hdr *rec, struct thandle *th)
1190 {
1191         const struct cred *old_cred;
1192         int rc;
1193
1194         ENTRY;
1195
1196         if (lgh->lgh_logops->lop_declare_add == NULL)
1197                 RETURN(-EOPNOTSUPP);
1198
1199         old_cred = llog_raise_resource();
1200         rc = lgh->lgh_logops->lop_declare_add(env, lgh, rec, th);
1201         llog_restore_resource(old_cred);
1202         RETURN(rc);
1203 }
1204 EXPORT_SYMBOL(llog_declare_add);
1205
1206 /**
1207  * Helper function to open llog or create it if doesn't exist.
1208  * It hides all transaction handling from caller.
1209  */
1210 int llog_open_create(const struct lu_env *env, struct llog_ctxt *ctxt,
1211                      struct llog_handle **res, struct llog_logid *logid,
1212                      char *name)
1213 {
1214         struct dt_device        *d;
1215         struct thandle          *th;
1216         int                      rc;
1217
1218         ENTRY;
1219
1220         rc = llog_open(env, ctxt, res, logid, name, LLOG_OPEN_NEW);
1221         if (rc)
1222                 RETURN(rc);
1223
1224         if (llog_exist(*res))
1225                 RETURN(0);
1226
1227         LASSERT((*res)->lgh_obj != NULL);
1228
1229         d = lu2dt_dev((*res)->lgh_obj->do_lu.lo_dev);
1230
1231         if (unlikely(unlikely(d->dd_rdonly)))
1232                 RETURN(-EROFS);
1233
1234         th = dt_trans_create(env, d);
1235         if (IS_ERR(th))
1236                 GOTO(out, rc = PTR_ERR(th));
1237
1238         /* Create update llog object synchronously, which
1239          * happens during inialization process see
1240          * lod_sub_prep_llog(), to make sure the update
1241          * llog object is created before corss-MDT writing
1242          * updates into the llog object */
1243         if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID)
1244                 th->th_sync = 1;
1245
1246         th->th_wait_submit = 1;
1247         rc = llog_declare_create(env, *res, th);
1248         if (rc == 0) {
1249                 rc = dt_trans_start_local(env, d, th);
1250                 if (rc == 0)
1251                         rc = llog_create(env, *res, th);
1252         }
1253         dt_trans_stop(env, d, th);
1254 out:
1255         if (rc)
1256                 llog_close(env, *res);
1257         RETURN(rc);
1258 }
1259 EXPORT_SYMBOL(llog_open_create);
1260
1261 /**
1262  * Helper function to delete existent llog.
1263  */
1264 int llog_erase(const struct lu_env *env, struct llog_ctxt *ctxt,
1265                struct llog_logid *logid, char *name)
1266 {
1267         struct llog_handle      *handle;
1268         int                      rc = 0, rc2;
1269
1270         ENTRY;
1271
1272         /* nothing to erase */
1273         if (name == NULL && logid == NULL)
1274                 RETURN(0);
1275
1276         rc = llog_open(env, ctxt, &handle, logid, name, LLOG_OPEN_EXISTS);
1277         if (rc < 0)
1278                 RETURN(rc);
1279
1280         rc = llog_init_handle(env, handle, LLOG_F_IS_PLAIN, NULL);
1281         if (rc == 0)
1282                 rc = llog_destroy(env, handle);
1283
1284         rc2 = llog_close(env, handle);
1285         if (rc == 0)
1286                 rc = rc2;
1287         RETURN(rc);
1288 }
1289 EXPORT_SYMBOL(llog_erase);
1290
1291 /*
1292  * Helper function for write record in llog.
1293  * It hides all transaction handling from caller.
1294  * Valid only with local llog.
1295  */
1296 int llog_write(const struct lu_env *env, struct llog_handle *loghandle,
1297                struct llog_rec_hdr *rec, int idx)
1298 {
1299         struct dt_device        *dt;
1300         struct thandle          *th;
1301         bool                    need_cookie;
1302         int                     rc;
1303
1304         ENTRY;
1305
1306         LASSERT(loghandle);
1307         LASSERT(loghandle->lgh_ctxt);
1308         LASSERT(loghandle->lgh_obj != NULL);
1309
1310         dt = lu2dt_dev(loghandle->lgh_obj->do_lu.lo_dev);
1311
1312         if (unlikely(unlikely(dt->dd_rdonly)))
1313                 RETURN(-EROFS);
1314
1315         th = dt_trans_create(env, dt);
1316         if (IS_ERR(th))
1317                 RETURN(PTR_ERR(th));
1318
1319         rc = llog_declare_write_rec(env, loghandle, rec, idx, th);
1320         if (rc)
1321                 GOTO(out_trans, rc);
1322
1323         th->th_wait_submit = 1;
1324         rc = dt_trans_start_local(env, dt, th);
1325         if (rc)
1326                 GOTO(out_trans, rc);
1327
1328         need_cookie = !(idx == LLOG_HEADER_IDX || idx == LLOG_NEXT_IDX);
1329
1330         down_write(&loghandle->lgh_lock);
1331         if (need_cookie) {
1332                 struct llog_thread_info *lti = llog_info(env);
1333
1334                 /* cookie comes from llog_process_thread */
1335                 rc = llog_write_rec(env, loghandle, rec, &lti->lgi_cookie,
1336                                     rec->lrh_index, th);
1337                 /* upper layer didn`t pass cookie so change rc */
1338                 rc = (rc == 1 ? 0 : rc);
1339         } else {
1340                 rc = llog_write_rec(env, loghandle, rec, NULL, idx, th);
1341         }
1342
1343         up_write(&loghandle->lgh_lock);
1344 out_trans:
1345         dt_trans_stop(env, dt, th);
1346         RETURN(rc);
1347 }
1348 EXPORT_SYMBOL(llog_write);
1349
1350 int llog_open(const struct lu_env *env, struct llog_ctxt *ctxt,
1351               struct llog_handle **lgh, struct llog_logid *logid,
1352               char *name, enum llog_open_param open_param)
1353 {
1354         const struct cred *old_cred;
1355         int      rc;
1356
1357         ENTRY;
1358
1359         LASSERT(ctxt);
1360         LASSERT(ctxt->loc_logops);
1361
1362         if (ctxt->loc_logops->lop_open == NULL) {
1363                 *lgh = NULL;
1364                 RETURN(-EOPNOTSUPP);
1365         }
1366
1367         *lgh = llog_alloc_handle();
1368         if (*lgh == NULL)
1369                 RETURN(-ENOMEM);
1370         (*lgh)->lgh_ctxt = ctxt;
1371         (*lgh)->lgh_logops = ctxt->loc_logops;
1372
1373         old_cred = llog_raise_resource();
1374         rc = ctxt->loc_logops->lop_open(env, *lgh, logid, name, open_param);
1375         llog_restore_resource(old_cred);
1376         if (rc) {
1377                 llog_free_handle(*lgh);
1378                 *lgh = NULL;
1379         }
1380         RETURN(rc);
1381 }
1382 EXPORT_SYMBOL(llog_open);
1383
1384 int llog_close(const struct lu_env *env, struct llog_handle *loghandle)
1385 {
1386         return llog_handle_put(env, loghandle);
1387 }
1388 EXPORT_SYMBOL(llog_close);
1389
1390 /**
1391  * Helper function to get the llog size in records. It is used by MGS
1392  * mostly to check that config llog exists and contains data.
1393  *
1394  * \param[in] env       execution environment
1395  * \param[in] ctxt      llog context
1396  * \param[in] name      llog name
1397  *
1398  * \retval              true if there are records in llog besides a header
1399  * \retval              false on error or llog without records
1400  */
1401 int llog_is_empty(const struct lu_env *env, struct llog_ctxt *ctxt,
1402                   char *name)
1403 {
1404         struct llog_handle      *llh;
1405         int                      rc = 0;
1406
1407         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1408         if (rc < 0) {
1409                 if (likely(rc == -ENOENT))
1410                         rc = 0;
1411                 GOTO(out, rc);
1412         }
1413
1414         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1415         if (rc)
1416                 GOTO(out_close, rc);
1417         rc = llog_get_size(llh);
1418
1419 out_close:
1420         llog_close(env, llh);
1421 out:
1422         /* The header is record 1, the llog is still considered as empty
1423          * if there is only header */
1424         return (rc <= 1);
1425 }
1426 EXPORT_SYMBOL(llog_is_empty);
1427
1428 int llog_copy_handler(const struct lu_env *env, struct llog_handle *llh,
1429                       struct llog_rec_hdr *rec, void *data)
1430 {
1431         struct llog_handle      *copy_llh = data;
1432
1433         /* Append all records */
1434         return llog_write(env, copy_llh, rec, LLOG_NEXT_IDX);
1435 }
1436
1437 /* backup plain llog */
1438 int llog_backup(const struct lu_env *env, struct obd_device *obd,
1439                 struct llog_ctxt *ctxt, struct llog_ctxt *bctxt,
1440                 char *name, char *backup)
1441 {
1442         struct llog_handle      *llh, *bllh;
1443         int                      rc;
1444
1445         ENTRY;
1446
1447         /* open original log */
1448         rc = llog_open(env, ctxt, &llh, NULL, name, LLOG_OPEN_EXISTS);
1449         if (rc < 0) {
1450                 /* the -ENOENT case is also reported to the caller
1451                  * but silently so it should handle that if needed.
1452                  */
1453                 if (rc != -ENOENT)
1454                         CERROR("%s: failed to open log %s: rc = %d\n",
1455                                obd->obd_name, name, rc);
1456                 RETURN(rc);
1457         }
1458
1459         rc = llog_init_handle(env, llh, LLOG_F_IS_PLAIN, NULL);
1460         if (rc)
1461                 GOTO(out_close, rc);
1462
1463         /* Make sure there's no old backup log */
1464         rc = llog_erase(env, bctxt, NULL, backup);
1465         if (rc < 0 && rc != -ENOENT)
1466                 GOTO(out_close, rc);
1467
1468         /* open backup log */
1469         rc = llog_open_create(env, bctxt, &bllh, NULL, backup);
1470         if (rc) {
1471                 CERROR("%s: failed to open backup logfile %s: rc = %d\n",
1472                        obd->obd_name, backup, rc);
1473                 GOTO(out_close, rc);
1474         }
1475
1476         /* check that backup llog is not the same object as original one */
1477         if (llh->lgh_obj == bllh->lgh_obj) {
1478                 CERROR("%s: backup llog %s to itself (%s), objects %p/%p\n",
1479                        obd->obd_name, name, backup, llh->lgh_obj,
1480                        bllh->lgh_obj);
1481                 GOTO(out_backup, rc = -EEXIST);
1482         }
1483
1484         rc = llog_init_handle(env, bllh, LLOG_F_IS_PLAIN, NULL);
1485         if (rc)
1486                 GOTO(out_backup, rc);
1487
1488         /* Copy log record by record */
1489         rc = llog_process_or_fork(env, llh, llog_copy_handler, (void *)bllh,
1490                                   NULL, false);
1491         if (rc)
1492                 CERROR("%s: failed to backup log %s: rc = %d\n",
1493                        obd->obd_name, name, rc);
1494 out_backup:
1495         llog_close(env, bllh);
1496 out_close:
1497         llog_close(env, llh);
1498         RETURN(rc);
1499 }
1500 EXPORT_SYMBOL(llog_backup);
1501
1502 /* Get size of llog */
1503 __u64 llog_size(const struct lu_env *env, struct llog_handle *llh)
1504 {
1505         int rc;
1506         struct lu_attr la;
1507
1508         rc = llh->lgh_obj->do_ops->do_attr_get(env, llh->lgh_obj, &la);
1509         if (rc) {
1510                 CERROR("%s: attr_get failed for "DFID": rc = %d\n",
1511                        loghandle2name(llh), PFID(&llh->lgh_id.lgl_oi.oi_fid),
1512                        rc);
1513                 return 0;
1514         }
1515
1516         return la.la_size;
1517 }
1518 EXPORT_SYMBOL(llog_size);
1519