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