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