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