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