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