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