4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2014 Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * lustre/obdclass/llog_osd.c
35 * Low level llog routines on top of OSD API
37 * This file provides set of methods for llog operations on top of
38 * dt_device. It contains all supported llog_operations interfaces and
39 * supplimental functions.
41 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42 * Author: Mikhail Pershin <mike.pershin@intel.com>
45 #define DEBUG_SUBSYSTEM S_LOG
48 #include <obd_class.h>
49 #include <lustre_fid.h>
50 #include <dt_object.h>
52 #include "llog_internal.h"
53 #include "local_storage.h"
56 * Implementation of the llog_operations::lop_declare_create
58 * This function is a wrapper over local_storage API function
59 * local_object_declare_create().
61 * \param[in] env execution environment
62 * \param[in] los local_storage for bottom storage device
63 * \param[in] o dt_object to create
64 * \param[in] th current transaction handle
66 * \retval 0 on successful declaration of the new object
67 * \retval negative error if declaration was failed
69 static int llog_osd_declare_new_object(const struct lu_env *env,
70 struct local_oid_storage *los,
74 struct llog_thread_info *lgi = llog_info(env);
76 lgi->lgi_attr.la_valid = LA_MODE;
77 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
78 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
80 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
85 * Implementation of the llog_operations::lop_create
87 * This function is a wrapper over local_storage API function
88 * local_object_create().
90 * \param[in] env execution environment
91 * \param[in] los local_storage for bottom storage device
92 * \param[in] o dt_object to create
93 * \param[in] th current transaction handle
95 * \retval 0 on successful creation of the new object
96 * \retval negative error if creation was failed
98 static int llog_osd_create_new_object(const struct lu_env *env,
99 struct local_oid_storage *los,
103 struct llog_thread_info *lgi = llog_info(env);
105 lgi->lgi_attr.la_valid = LA_MODE;
106 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
107 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
109 return local_object_create(env, los, o, &lgi->lgi_attr,
114 * Write a padding record to the llog
116 * This function writes a padding record to the end of llog. That may
117 * be needed if llog contains records of variable size, e.g. config logs
119 * The padding record just aligns llog to the LLOG_CHUNK_SIZE boundary if
120 * the current record doesn't fit in the remaining space.
122 * It allocates full length to avoid two separate writes for header and tail.
123 * Such 2-steps scheme needs extra protection and complex error handling.
125 * \param[in] env execution environment
126 * \param[in] o dt_object to create
127 * \param[in,out] off pointer to the padding start offset
128 * \param[in] len padding length
129 * \param[in] index index of the padding record in a llog
130 * \param[in] th current transaction handle
132 * \retval 0 on successful padding write
133 * \retval negative error if write failed
135 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
136 loff_t *off, int len, int index, struct thandle *th)
138 struct llog_thread_info *lgi = llog_info(env);
139 struct llog_rec_hdr *rec;
140 struct llog_rec_tail *tail;
147 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
154 rec->lrh_index = index;
155 rec->lrh_type = LLOG_PAD_MAGIC;
157 tail = rec_tail(rec);
159 tail->lrt_index = index;
161 lgi->lgi_buf.lb_buf = rec;
162 lgi->lgi_buf.lb_len = len;
163 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
165 CERROR("%s: error writing padding record: rc = %d\n",
166 o->do_lu.lo_dev->ld_obd->obd_name, rc);
173 * Implementation of the llog_operations::lop_read_header
175 * This function reads the current llog header from the bottom storage
178 * \param[in] env execution environment
179 * \param[in] handle llog handle of the current llog
181 * \retval 0 on successful header read
182 * \retval negative error if read failed
184 static int llog_osd_read_header(const struct lu_env *env,
185 struct llog_handle *handle)
187 struct llog_rec_hdr *llh_hdr;
189 struct llog_thread_info *lgi;
194 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
199 lgi = llog_info(env);
201 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
205 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
207 if (lgi->lgi_attr.la_size == 0) {
208 CDEBUG(D_HA, "not reading header from 0-byte log\n");
213 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
214 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
216 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
218 CERROR("%s: error reading log header from "DFID": rc = %d\n",
219 o->do_lu.lo_dev->ld_obd->obd_name,
220 PFID(lu_object_fid(&o->do_lu)), rc);
224 llh_hdr = &handle->lgh_hdr->llh_hdr;
225 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
226 lustre_swab_llog_hdr(handle->lgh_hdr);
228 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
229 CERROR("%s: bad log %s "DFID" header magic: %#x "
230 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
231 handle->lgh_name ? handle->lgh_name : "",
232 PFID(lu_object_fid(&o->do_lu)),
233 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
235 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
236 CERROR("%s: incorrectly sized log %s "DFID" header: "
237 "%#x (expected %#x)\n"
238 "you may need to re-run lconf --write_conf.\n",
239 o->do_lu.lo_dev->ld_obd->obd_name,
240 handle->lgh_name ? handle->lgh_name : "",
241 PFID(lu_object_fid(&o->do_lu)),
242 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
246 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
252 * Implementation of the llog_operations::lop_declare_write
254 * This function declares the new record write.
256 * \param[in] env execution environment
257 * \param[in] loghandle llog handle of the current llog
258 * \param[in] rec llog record header. This is a real header of the full
259 * llog record to write. This is the beginning of buffer
260 * to write, the length of buffer is stored in
262 * \param[in] idx index of the llog record. If \a idx == -1 then this is
263 * append case, otherwise \a idx is the index of record
265 * \param[in] th current transaction handle
267 * \retval 0 on successful declaration
268 * \retval negative error if declaration failed
270 static int llog_osd_declare_write_rec(const struct lu_env *env,
271 struct llog_handle *loghandle,
272 struct llog_rec_hdr *rec,
273 int idx, struct thandle *th)
275 struct llog_thread_info *lgi = llog_info(env);
285 LASSERT(rec->lrh_len <= LLOG_CHUNK_SIZE);
287 o = loghandle->lgh_obj;
290 lgi->lgi_buf.lb_len = sizeof(struct llog_log_hdr);
291 lgi->lgi_buf.lb_buf = NULL;
292 /* each time we update header */
293 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
295 if (rc || idx == 0) /* if error or just header */
299 * the pad record can be inserted so take into account double
302 lgi->lgi_buf.lb_len = rec->lrh_len * 2;
303 lgi->lgi_buf.lb_buf = NULL;
304 /* XXX: implement declared window or multi-chunks approach */
305 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
311 * Implementation of the llog_operations::lop_write
313 * This function writes the new record in the llog or modify the existed one.
315 * \param[in] env execution environment
316 * \param[in] loghandle llog handle of the current llog
317 * \param[in] rec llog record header. This is a real header of
318 * the full llog record to write. This is
319 * the beginning of buffer to write, the length
320 * of buffer is stored in \a rec::lrh_len
321 * \param[out] reccookie pointer to the cookie to return back if needed.
322 * It is used for further cancel of this llog
324 * \param[in] idx index of the llog record. If \a idx == -1 then
325 * this is append case, otherwise \a idx is
326 * the index of record to modify
327 * \param[in] th current transaction handle
329 * \retval 0 on successful write && \a reccookie == NULL
330 * 1 on successful write && \a reccookie != NULL
331 * \retval negative error if write failed
333 static int llog_osd_write_rec(const struct lu_env *env,
334 struct llog_handle *loghandle,
335 struct llog_rec_hdr *rec,
336 struct llog_cookie *reccookie,
337 int idx, struct thandle *th)
339 struct llog_thread_info *lgi = llog_info(env);
340 struct llog_log_hdr *llh;
341 int reclen = rec->lrh_len;
343 struct llog_rec_tail *lrt;
346 bool header_is_updated = false;
351 llh = loghandle->lgh_hdr;
353 o = loghandle->lgh_obj;
357 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
358 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
360 /* record length should not bigger than LLOG_CHUNK_SIZE */
361 if (reclen > LLOG_CHUNK_SIZE)
364 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
369 * The modification case.
370 * If idx set then the record with that index must be modified.
371 * There are three cases possible:
372 * 1) the common case is the llog header update (idx == 0)
373 * 2) the llog record modification during llog process.
374 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
375 * In that case the \a loghandle::lgh_cur_offset
376 * 3) otherwise this is assumed that llog consist of records of
377 * fixed size, i.e. catalog. The llog header must has llh_size
378 * field equal to record size. The record offset is calculated
379 * just by /a idx value
381 * During modification we don't need extra header update because
382 * the bitmap and record count are not changed. The record header
383 * and tail remains the same too.
385 if (idx != LLOG_NEXT_IDX) {
386 /* llog can be empty only when first record is being written */
387 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
389 if (!ext2_test_bit(idx, llh->llh_bitmap)) {
390 CERROR("%s: modify unset record %u\n",
391 o->do_lu.lo_dev->ld_obd->obd_name, idx);
395 if (idx != rec->lrh_index) {
396 CERROR("%s: modify index mismatch %d %u\n",
397 o->do_lu.lo_dev->ld_obd->obd_name, idx,
402 if (idx == LLOG_HEADER_IDX) {
403 /* llog header update */
404 LASSERT(reclen == sizeof(struct llog_log_hdr));
405 LASSERT(rec == &llh->llh_hdr);
408 lgi->lgi_buf.lb_len = reclen;
409 lgi->lgi_buf.lb_buf = rec;
410 rc = dt_record_write(env, o, &lgi->lgi_buf,
413 } else if (loghandle->lgh_cur_idx > 0) {
415 * The lgh_cur_offset can be used only if index is
418 if (idx != loghandle->lgh_cur_idx) {
419 CERROR("%s: modify index mismatch %d %d\n",
420 o->do_lu.lo_dev->ld_obd->obd_name, idx,
421 loghandle->lgh_cur_idx);
425 lgi->lgi_off = loghandle->lgh_cur_offset;
426 CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
427 "len:%u offset %llu\n",
428 POSTID(&loghandle->lgh_id.lgl_oi), idx,
429 rec->lrh_len, (long long)lgi->lgi_off);
430 } else if (llh->llh_size > 0) {
431 if (llh->llh_size != rec->lrh_len) {
432 CERROR("%s: wrong record size, llh_size is %u"
433 " but record size is %u\n",
434 o->do_lu.lo_dev->ld_obd->obd_name,
435 llh->llh_size, rec->lrh_len);
438 lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
440 /* This can be result of lgh_cur_idx is not set during
441 * llog processing or llh_size is not set to proper
442 * record size for fixed records llog. Therefore it is
443 * impossible to get record offset. */
444 CERROR("%s: can't get record offset, idx:%d, "
445 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
450 /* update only data, header and tail remain the same */
451 lgi->lgi_off += sizeof(struct llog_rec_hdr);
452 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
453 lgi->lgi_buf.lb_buf = REC_DATA(rec);
454 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
455 if (rc == 0 && reccookie) {
456 reccookie->lgc_lgl = loghandle->lgh_id;
457 reccookie->lgc_index = idx;
465 * The most common case of using llog. The new index is assigned to
466 * the new record, new bit is set in llog bitmap and llog count is
469 * Make sure that records don't cross a chunk boundary, so we can
470 * process them page-at-a-time if needed. If it will cross a chunk
471 * boundary, write in a fake (but referenced) entry to pad the chunk.
473 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
474 lgi->lgi_off = lgi->lgi_attr.la_size;
475 left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
476 /* NOTE: padding is a record, but no bit is set */
477 if (left != 0 && left != reclen &&
478 left < (reclen + LLOG_MIN_REC_SIZE)) {
479 index = loghandle->lgh_last_idx + 1;
480 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
483 loghandle->lgh_last_idx++; /* for pad rec */
485 /* if it's the last idx in log file, then return -ENOSPC */
486 if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
489 /* increment the last_idx along with llh_tail index, they should
490 * be equal for a llog lifetime */
491 loghandle->lgh_last_idx++;
492 index = loghandle->lgh_last_idx;
493 llh->llh_tail.lrt_index = index;
495 * NB: the caller should make sure only 1 process access
496 * the lgh_last_idx, e.g. append should be exclusive.
497 * Otherwise it might hit the assert.
499 LASSERT(index < LLOG_BITMAP_SIZE(llh));
500 rec->lrh_index = index;
502 lrt->lrt_len = rec->lrh_len;
503 lrt->lrt_index = rec->lrh_index;
505 /* the lgh_hdr_lock protects llog header data from concurrent
506 * update/cancel, the llh_count and llh_bitmap are protected */
507 spin_lock(&loghandle->lgh_hdr_lock);
508 if (ext2_set_bit(index, llh->llh_bitmap)) {
509 CERROR("%s: index %u already set in log bitmap\n",
510 o->do_lu.lo_dev->ld_obd->obd_name, index);
511 spin_unlock(&loghandle->lgh_hdr_lock);
512 LBUG(); /* should never happen */
515 spin_unlock(&loghandle->lgh_hdr_lock);
518 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
519 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
520 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
524 header_is_updated = true;
525 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
529 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
530 lgi->lgi_off = lgi->lgi_attr.la_size;
531 lgi->lgi_buf.lb_len = reclen;
532 lgi->lgi_buf.lb_buf = rec;
533 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
537 CDEBUG(D_OTHER, "added record "DOSTID": idx: %u, %u\n",
538 POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
539 if (reccookie != NULL) {
540 reccookie->lgc_lgl = loghandle->lgh_id;
541 reccookie->lgc_index = index;
542 if ((rec->lrh_type == MDS_UNLINK_REC) ||
543 (rec->lrh_type == MDS_SETATTR64_REC))
544 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
545 else if (rec->lrh_type == OST_SZ_REC)
546 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
548 reccookie->lgc_subsys = -1;
553 /* cleanup llog for error case */
554 spin_lock(&loghandle->lgh_hdr_lock);
555 ext2_clear_bit(index, llh->llh_bitmap);
557 spin_unlock(&loghandle->lgh_hdr_lock);
559 /* restore llog last_idx */
560 loghandle->lgh_last_idx--;
561 llh->llh_tail.lrt_index = loghandle->lgh_last_idx;
563 /* restore the header on disk if it was written */
564 if (header_is_updated) {
566 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
567 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
568 dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
575 * We can skip reading at least as many log blocks as the number of
576 * minimum sized log records we are skipping. If it turns out
577 * that we are not far enough along the log (because the
578 * actual records are larger than minimum size) we just skip
581 static inline void llog_skip_over(__u64 *off, int curr, int goal)
585 *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
586 ~(LLOG_CHUNK_SIZE - 1);
590 * Implementation of the llog_operations::lop_next_block
592 * This function finds the the next llog block to return which contains
593 * record with required index. It is main part of llog processing.
595 * \param[in] env execution environment
596 * \param[in] loghandle llog handle of the current llog
597 * \param[in,out] cur_idx index preceeding cur_offset
598 * \param[in] next_idx target index to find
599 * \param[in,out] cur_offset furtherst point read in the file
600 * \param[in] buf pointer to data buffer to fill
601 * \param[in] len required len to read, it is
602 * LLOG_CHUNK_SIZE usually.
604 * \retval 0 on successful buffer read
605 * \retval negative value on error
607 static int llog_osd_next_block(const struct lu_env *env,
608 struct llog_handle *loghandle, int *cur_idx,
609 int next_idx, __u64 *cur_offset, void *buf,
612 struct llog_thread_info *lgi = llog_info(env);
614 struct dt_device *dt;
622 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
625 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
626 next_idx, *cur_idx, *cur_offset);
629 LASSERT(loghandle->lgh_ctxt);
631 o = loghandle->lgh_obj;
633 LASSERT(dt_object_exists(o));
634 dt = lu2dt_dev(o->do_lu.lo_dev);
637 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
641 while (*cur_offset < lgi->lgi_attr.la_size) {
642 struct llog_rec_hdr *rec, *last_rec;
643 struct llog_rec_tail *tail;
645 llog_skip_over(cur_offset, *cur_idx, next_idx);
647 /* read up to next LLOG_CHUNK_SIZE block */
648 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
649 (*cur_offset & (LLOG_CHUNK_SIZE - 1));
650 lgi->lgi_buf.lb_buf = buf;
652 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
654 CERROR("%s: can't read llog block from log "DFID
655 " offset "LPU64": rc = %d\n",
656 o->do_lu.lo_dev->ld_obd->obd_name,
657 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
663 /* signal the end of the valid buffer to
665 memset(buf + rc, 0, len - rc);
668 if (rc == 0) /* end of file, nothing to do */
671 if (rc < sizeof(*tail)) {
672 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
674 o->do_lu.lo_dev->ld_obd->obd_name,
675 POSTID(&loghandle->lgh_id.lgl_oi),
676 loghandle->lgh_id.lgl_ogen, *cur_offset);
677 GOTO(out, rc = -EINVAL);
681 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
682 lustre_swab_llog_rec(rec);
684 tail = (struct llog_rec_tail *)((char *)buf + rc -
685 sizeof(struct llog_rec_tail));
686 /* get the last record in block */
687 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
688 le32_to_cpu(tail->lrt_len));
690 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
691 lustre_swab_llog_rec(last_rec);
692 LASSERT(last_rec->lrh_index == tail->lrt_index);
694 *cur_idx = tail->lrt_index;
696 /* this shouldn't happen */
697 if (tail->lrt_index == 0) {
698 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
700 o->do_lu.lo_dev->ld_obd->obd_name,
701 POSTID(&loghandle->lgh_id.lgl_oi),
702 loghandle->lgh_id.lgl_ogen, *cur_offset);
703 GOTO(out, rc = -EINVAL);
705 if (tail->lrt_index < next_idx)
708 /* sanity check that the start of the new buffer is no farther
709 * than the record that we wanted. This shouldn't happen. */
710 if (rec->lrh_index > next_idx) {
711 CERROR("%s: missed desired record? %u > %u\n",
712 o->do_lu.lo_dev->ld_obd->obd_name,
713 rec->lrh_index, next_idx);
714 GOTO(out, rc = -ENOENT);
718 GOTO(out, rc = -EIO);
724 * Implementation of the llog_operations::lop_prev_block
726 * This function finds the llog block to return which contains
727 * record with required index but in reverse order - from end of llog
729 * It is main part of reverse llog processing.
731 * \param[in] env execution environment
732 * \param[in] loghandle llog handle of the current llog
733 * \param[in] prev_idx target index to find
734 * \param[in] buf pointer to data buffer to fill
735 * \param[in] len required len to read, it is LLOG_CHUNK_SIZE usually.
737 * \retval 0 on successful buffer read
738 * \retval negative value on error
740 static int llog_osd_prev_block(const struct lu_env *env,
741 struct llog_handle *loghandle,
742 int prev_idx, void *buf, int len)
744 struct llog_thread_info *lgi = llog_info(env);
746 struct dt_device *dt;
752 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
755 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
758 LASSERT(loghandle->lgh_ctxt);
760 o = loghandle->lgh_obj;
762 LASSERT(dt_object_exists(o));
763 dt = lu2dt_dev(o->do_lu.lo_dev);
766 cur_offset = LLOG_CHUNK_SIZE;
767 llog_skip_over(&cur_offset, 0, prev_idx);
769 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
773 while (cur_offset < lgi->lgi_attr.la_size) {
774 struct llog_rec_hdr *rec, *last_rec;
775 struct llog_rec_tail *tail;
777 lgi->lgi_buf.lb_len = len;
778 lgi->lgi_buf.lb_buf = buf;
779 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
781 CERROR("%s: can't read llog block from log "DFID
782 " offset "LPU64": rc = %d\n",
783 o->do_lu.lo_dev->ld_obd->obd_name,
784 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
788 if (rc == 0) /* end of file, nothing to do */
791 if (rc < sizeof(*tail)) {
792 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
794 o->do_lu.lo_dev->ld_obd->obd_name,
795 POSTID(&loghandle->lgh_id.lgl_oi),
796 loghandle->lgh_id.lgl_ogen, cur_offset);
797 GOTO(out, rc = -EINVAL);
801 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
802 lustre_swab_llog_rec(rec);
804 tail = (struct llog_rec_tail *)((char *)buf + rc -
805 sizeof(struct llog_rec_tail));
806 /* get the last record in block */
807 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
808 le32_to_cpu(tail->lrt_len));
810 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
811 lustre_swab_llog_rec(last_rec);
812 LASSERT(last_rec->lrh_index == tail->lrt_index);
814 /* this shouldn't happen */
815 if (tail->lrt_index == 0) {
816 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
818 o->do_lu.lo_dev->ld_obd->obd_name,
819 POSTID(&loghandle->lgh_id.lgl_oi),
820 loghandle->lgh_id.lgl_ogen, cur_offset);
821 GOTO(out, rc = -EINVAL);
823 if (tail->lrt_index < prev_idx)
826 /* sanity check that the start of the new buffer is no farther
827 * than the record that we wanted. This shouldn't happen. */
828 if (rec->lrh_index > prev_idx) {
829 CERROR("%s: missed desired record? %u > %u\n",
830 o->do_lu.lo_dev->ld_obd->obd_name,
831 rec->lrh_index, prev_idx);
832 GOTO(out, rc = -ENOENT);
836 GOTO(out, rc = -EIO);
842 * This is helper function to get llog directory object. It is used by named
843 * llog operations to find/insert/delete llog entry from llog directory.
845 * \param[in] env execution environment
846 * \param[in] ctxt llog context
848 * \retval dt_object of llog directory
849 * \retval ERR_PTR of negative value on error
851 struct dt_object *llog_osd_dir_get(const struct lu_env *env,
852 struct llog_ctxt *ctxt)
854 struct dt_device *dt;
855 struct dt_thread_info *dti = dt_info(env);
856 struct dt_object *dir;
859 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
860 if (ctxt->loc_dir == NULL) {
861 rc = dt_root_get(env, dt, &dti->dti_fid);
864 dir = dt_locate(env, dt, &dti->dti_fid);
866 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
867 lu_object_put(env, &dir->do_lu);
868 return ERR_PTR(-ENOTDIR);
871 lu_object_get(&ctxt->loc_dir->do_lu);
879 * Implementation of the llog_operations::lop_open
881 * This function opens the llog by its logid or by name, it may open also
882 * non existent llog and assing then new id to it.
883 * The llog_open/llog_close pair works similar to lu_object_find/put,
884 * the object may not exist prior open. The result of open is just dt_object
885 * in the llog header.
887 * \param[in] env execution environment
888 * \param[in] handle llog handle of the current llog
889 * \param[in] logid logid of llog to open (nameless llog)
890 * \param[in] name name of llog to open (named llog)
891 * \param[in] open_param
892 * LLOG_OPEN_NEW - new llog, may not exist
893 * LLOG_OPEN_EXIST - old llog, must exist
895 * \retval 0 on successful open, llog_handle::lgh_obj
896 * contains the dt_object of the llog.
897 * \retval negative value on error
899 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
900 struct llog_logid *logid, char *name,
901 enum llog_open_param open_param)
903 struct llog_thread_info *lgi = llog_info(env);
904 struct llog_ctxt *ctxt = handle->lgh_ctxt;
906 struct dt_device *dt;
907 struct ls_device *ls;
908 struct local_oid_storage *los;
915 LASSERT(ctxt->loc_exp);
916 LASSERT(ctxt->loc_exp->exp_obd);
917 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
920 ls = ls_device_get(dt);
924 mutex_lock(&ls->ls_los_mutex);
925 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
926 mutex_unlock(&ls->ls_los_mutex);
928 ls_device_put(env, ls);
933 logid_to_fid(logid, &lgi->lgi_fid);
935 struct dt_object *llog_dir;
937 llog_dir = llog_osd_dir_get(env, ctxt);
938 if (IS_ERR(llog_dir))
939 GOTO(out, rc = PTR_ERR(llog_dir));
940 dt_read_lock(env, llog_dir, 0);
941 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
942 dt_read_unlock(env, llog_dir);
943 lu_object_put(env, &llog_dir->do_lu);
944 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
945 /* generate fid for new llog */
946 rc = local_object_fid_generate(env, los,
951 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
952 if (handle->lgh_name)
953 strcpy(handle->lgh_name, name);
955 GOTO(out, rc = -ENOMEM);
957 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
958 /* generate fid for new llog */
959 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
964 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
966 GOTO(out_name, rc = PTR_ERR(o));
968 /* No new llog is expected but doesn't exist */
969 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
970 GOTO(out_put, rc = -ENOENT);
972 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
974 handle->private_data = los;
975 LASSERT(handle->lgh_ctxt);
980 lu_object_put(env, &o->do_lu);
982 if (handle->lgh_name != NULL)
983 OBD_FREE(handle->lgh_name, strlen(name) + 1);
990 * Implementation of the llog_operations::lop_exist
992 * This function checks that llog exists on storage.
994 * \param[in] handle llog handle of the current llog
996 * \retval true if llog object exists and is not just destroyed
997 * \retval false if llog doesn't exist or just destroyed
999 static int llog_osd_exist(struct llog_handle *handle)
1001 LASSERT(handle->lgh_obj);
1002 return (dt_object_exists(handle->lgh_obj) &&
1003 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
1007 * Implementation of the llog_operations::lop_declare_create
1009 * This function declares the llog create. It declares also name insert
1010 * into llog directory in case of named llog.
1012 * \param[in] env execution environment
1013 * \param[in] res llog handle of the current llog
1014 * \param[in] th current transaction handle
1016 * \retval 0 on successful create declaration
1017 * \retval negative value on error
1019 static int llog_osd_declare_create(const struct lu_env *env,
1020 struct llog_handle *res, struct thandle *th)
1022 struct llog_thread_info *lgi = llog_info(env);
1023 struct local_oid_storage *los;
1024 struct dt_object *o;
1029 LASSERT(res->lgh_obj);
1032 /* object can be created by another thread */
1034 if (dt_object_exists(o))
1037 los = res->private_data;
1040 rc = llog_osd_declare_new_object(env, los, o, th);
1044 /* do not declare header initialization here as it's declared
1045 * in llog_osd_declare_write_rec() which is always called */
1047 if (res->lgh_name) {
1048 struct dt_object *llog_dir;
1050 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1051 if (IS_ERR(llog_dir))
1052 RETURN(PTR_ERR(llog_dir));
1053 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1054 rc = dt_declare_insert(env, llog_dir,
1055 (struct dt_rec *)&lgi->lgi_fid,
1056 (struct dt_key *)res->lgh_name, th);
1057 lu_object_put(env, &llog_dir->do_lu);
1059 CERROR("%s: can't declare named llog %s: rc = %d\n",
1060 o->do_lu.lo_dev->ld_obd->obd_name,
1067 * Implementation of the llog_operations::lop_create
1069 * This function creates the llog according with llog_handle::lgh_obj
1070 * and llog_handle::lgh_name.
1072 * \param[in] env execution environment
1073 * \param[in] res llog handle of the current llog
1074 * \param[in] th current transaction handle
1076 * \retval 0 on successful create
1077 * \retval negative value on error
1079 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1082 struct llog_thread_info *lgi = llog_info(env);
1083 struct local_oid_storage *los;
1084 struct dt_object *o;
1093 /* llog can be already created */
1094 if (dt_object_exists(o))
1097 los = res->private_data;
1100 dt_write_lock(env, o, 0);
1101 if (!dt_object_exists(o))
1102 rc = llog_osd_create_new_object(env, los, o, th);
1106 dt_write_unlock(env, o);
1110 if (res->lgh_name) {
1111 struct dt_object *llog_dir;
1113 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1114 if (IS_ERR(llog_dir))
1115 RETURN(PTR_ERR(llog_dir));
1117 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1118 dt_read_lock(env, llog_dir, 0);
1119 rc = dt_insert(env, llog_dir,
1120 (struct dt_rec *)&lgi->lgi_fid,
1121 (struct dt_key *)res->lgh_name,
1122 th, BYPASS_CAPA, 1);
1123 dt_read_unlock(env, llog_dir);
1124 lu_object_put(env, &llog_dir->do_lu);
1126 CERROR("%s: can't create named llog %s: rc = %d\n",
1127 o->do_lu.lo_dev->ld_obd->obd_name,
1134 * Implementation of the llog_operations::lop_close
1136 * This function closes the llog. It just put llog object and referenced
1139 * \param[in] env execution environment
1140 * \param[in] handle llog handle of the current llog
1142 * \retval 0 on successful llog close
1143 * \retval negative value on error
1145 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1147 struct local_oid_storage *los;
1152 LASSERT(handle->lgh_obj);
1154 lu_object_put(env, &handle->lgh_obj->do_lu);
1156 los = handle->private_data;
1160 if (handle->lgh_name)
1161 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1167 * Implementation of the llog_operations::lop_destroy
1169 * This function destroys the llog and deletes also entry in the
1170 * llog directory in case of named llog. Llog should be opened prior that.
1171 * Destroy method is not part of external transaction and does everything
1174 * \param[in] env execution environment
1175 * \param[in] loghandle llog handle of the current llog
1177 * \retval 0 on successful destroy
1178 * \retval negative value on error
1180 static int llog_osd_destroy(const struct lu_env *env,
1181 struct llog_handle *loghandle)
1183 struct llog_ctxt *ctxt;
1184 struct dt_object *o, *llog_dir = NULL;
1185 struct dt_device *d;
1192 ctxt = loghandle->lgh_ctxt;
1195 o = loghandle->lgh_obj;
1198 d = lu2dt_dev(o->do_lu.lo_dev);
1200 LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1202 th = dt_trans_create(env, d);
1204 RETURN(PTR_ERR(th));
1206 if (loghandle->lgh_name) {
1207 llog_dir = llog_osd_dir_get(env, ctxt);
1208 if (IS_ERR(llog_dir))
1209 GOTO(out_trans, rc = PTR_ERR(llog_dir));
1211 name = loghandle->lgh_name;
1212 rc = dt_declare_delete(env, llog_dir,
1213 (struct dt_key *)name, th);
1215 GOTO(out_trans, rc);
1218 dt_declare_ref_del(env, o, th);
1220 rc = dt_declare_destroy(env, o, th);
1222 GOTO(out_trans, rc);
1224 rc = dt_trans_start_local(env, d, th);
1226 GOTO(out_trans, rc);
1228 dt_write_lock(env, o, 0);
1229 if (dt_object_exists(o)) {
1231 dt_read_lock(env, llog_dir, 0);
1232 rc = dt_delete(env, llog_dir,
1233 (struct dt_key *) name,
1235 dt_read_unlock(env, llog_dir);
1237 CERROR("%s: can't remove llog %s: rc = %d\n",
1238 o->do_lu.lo_dev->ld_obd->obd_name,
1240 GOTO(out_unlock, rc);
1243 dt_ref_del(env, o, th);
1244 rc = dt_destroy(env, o, th);
1246 GOTO(out_unlock, rc);
1249 dt_write_unlock(env, o);
1251 dt_trans_stop(env, d, th);
1252 if (llog_dir != NULL)
1253 lu_object_put(env, &llog_dir->do_lu);
1258 * Implementation of the llog_operations::lop_setup
1260 * This function setup the llog on local storage.
1262 * \param[in] env execution environment
1263 * \param[in] obd obd device the llog belongs to
1264 * \param[in] olg the llog group, it is always zero group now.
1265 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1266 * Every new llog type have to use own index.
1267 * \param[in] disk_obd the storage obd, where llog is stored.
1269 * \retval 0 on successful llog setup
1270 * \retval negative value on error
1272 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1273 struct obd_llog_group *olg, int ctxt_idx,
1274 struct obd_device *disk_obd)
1276 struct llog_thread_info *lgi = llog_info(env);
1277 struct llog_ctxt *ctxt;
1282 LASSERT(olg->olg_ctxts[ctxt_idx]);
1284 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1287 /* initialize data allowing to generate new fids,
1288 * literally we need a sequece */
1289 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1290 lgi->lgi_fid.f_oid = 1;
1291 lgi->lgi_fid.f_ver = 0;
1292 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1294 &ctxt->loc_los_nameless);
1298 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1299 lgi->lgi_fid.f_oid = 1;
1300 lgi->lgi_fid.f_ver = 0;
1301 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1303 &ctxt->loc_los_named);
1305 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1306 ctxt->loc_los_nameless = NULL;
1312 llog_ctxt_put(ctxt);
1317 * Implementation of the llog_operations::lop_cleanup
1319 * This function cleanups the llog on local storage.
1321 * \param[in] env execution environment
1322 * \param[in] ctxt the llog context
1326 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1328 if (ctxt->loc_los_nameless != NULL) {
1329 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1330 ctxt->loc_los_nameless = NULL;
1333 if (ctxt->loc_los_named != NULL) {
1334 local_oid_storage_fini(env, ctxt->loc_los_named);
1335 ctxt->loc_los_named = NULL;
1341 struct llog_operations llog_osd_ops = {
1342 .lop_next_block = llog_osd_next_block,
1343 .lop_prev_block = llog_osd_prev_block,
1344 .lop_read_header = llog_osd_read_header,
1345 .lop_destroy = llog_osd_destroy,
1346 .lop_setup = llog_osd_setup,
1347 .lop_cleanup = llog_osd_cleanup,
1348 .lop_open = llog_osd_open,
1349 .lop_exist = llog_osd_exist,
1350 .lop_declare_create = llog_osd_declare_create,
1351 .lop_create = llog_osd_create,
1352 .lop_declare_write_rec = llog_osd_declare_write_rec,
1353 .lop_write_rec = llog_osd_write_rec,
1354 .lop_close = llog_osd_close,
1356 EXPORT_SYMBOL(llog_osd_ops);
1359 * Read the special file which contains the list of llog catalogs IDs
1361 * This function reads the CATALOGS file which contains the array of llog
1362 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1363 * by OST/MDT number.
1365 * \param[in] env execution environment
1366 * \param[in] d corresponding storage device
1367 * \param[in] idx position to start from, usually OST/MDT index
1368 * \param[in] count how many catalog IDs to read
1369 * \param[out] idarray the buffer for the data. If it is NULL then
1370 * function returns just number of catalog IDs
1372 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1374 * \retval 0 on successful read of catalog IDs
1375 * \retval negative value on error
1376 * \retval positive value which is number of records in
1377 * the file if \a idarray is NULL
1379 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1380 int idx, int count, struct llog_catid *idarray,
1381 const struct lu_fid *fid)
1383 struct llog_thread_info *lgi = llog_info(env);
1384 struct dt_object *o = NULL;
1392 size = sizeof(*idarray) * count;
1393 lgi->lgi_off = idx * sizeof(*idarray);
1395 lgi->lgi_fid = *fid;
1396 o = dt_locate(env, d, &lgi->lgi_fid);
1400 if (!dt_object_exists(o)) {
1401 th = dt_trans_create(env, d);
1403 GOTO(out, rc = PTR_ERR(th));
1405 lgi->lgi_attr.la_valid = LA_MODE;
1406 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1407 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1409 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1412 GOTO(out_trans, rc);
1414 rc = dt_trans_start_local(env, d, th);
1416 GOTO(out_trans, rc);
1418 dt_write_lock(env, o, 0);
1419 if (!dt_object_exists(o))
1420 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1422 dt_write_unlock(env, o);
1424 dt_trans_stop(env, d, th);
1429 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1433 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1434 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1435 o->do_lu.lo_dev->ld_obd->obd_name,
1436 lgi->lgi_attr.la_mode);
1437 GOTO(out, rc = -ENOENT);
1440 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1441 (int)lgi->lgi_attr.la_size, size);
1443 /* return just number of llogs */
1444 if (idarray == NULL) {
1445 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1449 /* read for new ost index or for empty file */
1450 memset(idarray, 0, size);
1451 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1453 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1454 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1456 lgi->lgi_buf.lb_buf = idarray;
1457 lgi->lgi_buf.lb_len = size;
1458 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1460 CERROR("%s: error reading CATALOGS: rc = %d\n",
1461 o->do_lu.lo_dev->ld_obd->obd_name, rc);
1467 lu_object_put(env, &o->do_lu);
1470 EXPORT_SYMBOL(llog_osd_get_cat_list);
1473 * Write the special file which contains the list of llog catalogs IDs
1475 * This function writes the CATALOG file which contains the array of llog
1476 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1479 * \param[in] env execution environment
1480 * \param[in] d corresponding storage device
1481 * \param[in] idx position to start from, usually OST/MDT index
1482 * \param[in] count how many catalog IDs to write
1483 * \param[out] idarray the buffer with the data to write.
1484 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1486 * \retval 0 on successful write of catalog IDs
1487 * \retval negative value on error
1489 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1490 int idx, int count, struct llog_catid *idarray,
1491 const struct lu_fid *fid)
1493 struct llog_thread_info *lgi = llog_info(env);
1494 struct dt_object *o = NULL;
1503 size = sizeof(*idarray) * count;
1504 lgi->lgi_off = idx * sizeof(*idarray);
1505 lgi->lgi_fid = *fid;
1507 o = dt_locate(env, d, &lgi->lgi_fid);
1511 if (!dt_object_exists(o))
1512 GOTO(out, rc = -ENOENT);
1514 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1518 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1519 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1520 o->do_lu.lo_dev->ld_obd->obd_name,
1521 lgi->lgi_attr.la_mode);
1522 GOTO(out, rc = -ENOENT);
1525 th = dt_trans_create(env, d);
1527 GOTO(out, rc = PTR_ERR(th));
1529 lgi->lgi_buf.lb_len = size;
1530 lgi->lgi_buf.lb_buf = idarray;
1531 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
1535 rc = dt_trans_start_local(env, d, th);
1537 GOTO(out_trans, rc);
1539 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1541 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
1544 dt_trans_stop(env, d, th);
1546 lu_object_put(env, &o->do_lu);
1549 EXPORT_SYMBOL(llog_osd_put_cat_list);