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, 2015, 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 * Implementation of the llog_operations::lop_exist
116 * This function checks that llog exists on storage.
118 * \param[in] handle llog handle of the current llog
120 * \retval true if llog object exists and is not just destroyed
121 * \retval false if llog doesn't exist or just destroyed
123 static int llog_osd_exist(struct llog_handle *handle)
125 LASSERT(handle->lgh_obj);
126 return dt_object_exists(handle->lgh_obj) &&
127 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header);
131 * Write a padding record to the llog
133 * This function writes a padding record to the end of llog. That may
134 * be needed if llog contains records of variable size, e.g. config logs
136 * The padding record just aligns llog to the llog chunk_size boundary if
137 * the current record doesn't fit in the remaining space.
139 * It allocates full length to avoid two separate writes for header and tail.
140 * Such 2-steps scheme needs extra protection and complex error handling.
142 * \param[in] env execution environment
143 * \param[in] o dt_object to create
144 * \param[in,out] off pointer to the padding start offset
145 * \param[in] len padding length
146 * \param[in] index index of the padding record in a llog
147 * \param[in] th current transaction handle
149 * \retval 0 on successful padding write
150 * \retval negative error if write failed
152 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
153 loff_t *off, int len, int index, struct thandle *th)
155 struct llog_thread_info *lgi = llog_info(env);
156 struct llog_rec_hdr *rec;
157 struct llog_rec_tail *tail;
164 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
171 rec->lrh_index = index;
172 rec->lrh_type = LLOG_PAD_MAGIC;
174 tail = rec_tail(rec);
176 tail->lrt_index = index;
178 lgi->lgi_buf.lb_buf = rec;
179 lgi->lgi_buf.lb_len = len;
180 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
182 CERROR("%s: error writing padding record: rc = %d\n",
183 o->do_lu.lo_dev->ld_obd->obd_name, rc);
190 * Implementation of the llog_operations::lop_read_header
192 * This function reads the current llog header from the bottom storage
195 * \param[in] env execution environment
196 * \param[in] handle llog handle of the current llog
198 * \retval 0 on successful header read
199 * \retval negative error if read failed
201 static int llog_osd_read_header(const struct lu_env *env,
202 struct llog_handle *handle)
204 struct llog_rec_hdr *llh_hdr;
206 struct llog_thread_info *lgi;
207 enum llog_flag flags;
215 lgi = llog_info(env);
217 rc = dt_attr_get(env, o, &lgi->lgi_attr);
221 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
223 if (lgi->lgi_attr.la_size == 0) {
224 CDEBUG(D_HA, "not reading header from 0-byte log\n");
228 flags = handle->lgh_hdr->llh_flags;
231 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
232 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
233 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
234 llh_hdr = &handle->lgh_hdr->llh_hdr;
235 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
236 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
237 o->do_lu.lo_dev->ld_obd->obd_name,
238 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
247 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
248 lustre_swab_llog_hdr(handle->lgh_hdr);
250 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
251 CERROR("%s: bad log %s "DFID" header magic: %#x "
252 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
253 handle->lgh_name ? handle->lgh_name : "",
254 PFID(lu_object_fid(&o->do_lu)),
255 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
257 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
258 llh_hdr->lrh_len > handle->lgh_hdr_size) {
259 CERROR("%s: incorrectly sized log %s "DFID" header: "
260 "%#x (expected at least %#x)\n"
261 "you may need to re-run lconf --write_conf.\n",
262 o->do_lu.lo_dev->ld_obd->obd_name,
263 handle->lgh_name ? handle->lgh_name : "",
264 PFID(lu_object_fid(&o->do_lu)),
265 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
267 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
268 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
269 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
271 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
273 o->do_lu.lo_dev->ld_obd->obd_name,
274 handle->lgh_name ? handle->lgh_name : "",
275 PFID(lu_object_fid(&o->do_lu)),
276 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
280 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
281 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
287 * Implementation of the llog_operations::lop_declare_write
289 * This function declares the new record write.
291 * \param[in] env execution environment
292 * \param[in] loghandle llog handle of the current llog
293 * \param[in] rec llog record header. This is a real header of the full
294 * llog record to write. This is the beginning of buffer
295 * to write, the length of buffer is stored in
297 * \param[in] idx index of the llog record. If \a idx == -1 then this is
298 * append case, otherwise \a idx is the index of record
300 * \param[in] th current transaction handle
302 * \retval 0 on successful declaration
303 * \retval negative error if declaration failed
305 static int llog_osd_declare_write_rec(const struct lu_env *env,
306 struct llog_handle *loghandle,
307 struct llog_rec_hdr *rec,
308 int idx, struct thandle *th)
310 struct llog_thread_info *lgi = llog_info(env);
321 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
323 o = loghandle->lgh_obj;
326 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
327 lgi->lgi_buf.lb_len = chunk_size;
328 lgi->lgi_buf.lb_buf = NULL;
329 /* each time we update header */
330 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
332 if (rc || idx == 0) /* if error or just header */
336 * the pad record can be inserted so take into account double
339 lgi->lgi_buf.lb_len = chunk_size * 2;
340 lgi->lgi_buf.lb_buf = NULL;
341 /* XXX: implement declared window or multi-chunks approach */
342 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
348 * Implementation of the llog_operations::lop_write
350 * This function writes the new record in the llog or modify the existed one.
352 * \param[in] env execution environment
353 * \param[in] loghandle llog handle of the current llog
354 * \param[in] rec llog record header. This is a real header of
355 * the full llog record to write. This is
356 * the beginning of buffer to write, the length
357 * of buffer is stored in \a rec::lrh_len
358 * \param[out] reccookie pointer to the cookie to return back if needed.
359 * It is used for further cancel of this llog
361 * \param[in] idx index of the llog record. If \a idx == -1 then
362 * this is append case, otherwise \a idx is
363 * the index of record to modify
364 * \param[in] th current transaction handle
366 * \retval 0 on successful write && \a reccookie == NULL
367 * 1 on successful write && \a reccookie != NULL
368 * \retval negative error if write failed
370 static int llog_osd_write_rec(const struct lu_env *env,
371 struct llog_handle *loghandle,
372 struct llog_rec_hdr *rec,
373 struct llog_cookie *reccookie,
374 int idx, struct thandle *th)
376 struct llog_thread_info *lgi = llog_info(env);
377 struct llog_log_hdr *llh;
378 int reclen = rec->lrh_len;
380 struct llog_rec_tail *lrt;
388 llh = loghandle->lgh_hdr;
390 o = loghandle->lgh_obj;
394 chunk_size = llh->llh_hdr.lrh_len;
395 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
396 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
398 if (!llog_osd_exist(loghandle))
401 /* record length should not bigger than */
402 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
405 /* sanity check for fixed-records llog */
406 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
407 LASSERT(llh->llh_size != 0);
408 LASSERT(llh->llh_size == reclen);
411 rc = dt_attr_get(env, o, &lgi->lgi_attr);
416 * The modification case.
417 * If idx set then the record with that index must be modified.
418 * There are three cases possible:
419 * 1) the common case is the llog header update (idx == 0)
420 * 2) the llog record modification during llog process.
421 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
422 * In that case the \a loghandle::lgh_cur_offset
423 * 3) otherwise this is assumed that llog consist of records of
424 * fixed size, i.e. catalog. The llog header must has llh_size
425 * field equal to record size. The record offset is calculated
426 * just by /a idx value
428 * During modification we don't need extra header update because
429 * the bitmap and record count are not changed. The record header
430 * and tail remains the same too.
432 if (idx != LLOG_NEXT_IDX) {
433 /* llog can be empty only when first record is being written */
434 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
436 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
437 CERROR("%s: modify unset record %u\n",
438 o->do_lu.lo_dev->ld_obd->obd_name, idx);
442 if (idx != rec->lrh_index) {
443 CERROR("%s: modify index mismatch %d %u\n",
444 o->do_lu.lo_dev->ld_obd->obd_name, idx,
449 if (idx == LLOG_HEADER_IDX) {
450 /* llog header update */
451 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
455 /* If it does not indicate the bitmap index
456 * (reccookie == NULL), then it means update
457 * the whole update header. Otherwise only
458 * update header and bits needs to be updated,
459 * and in DNE cases, it will signaficantly
460 * shrink the RPC size.
461 * see distribute_txn_cancel_records()*/
462 if (reccookie == NULL) {
463 lgi->lgi_buf.lb_len = reclen;
464 lgi->lgi_buf.lb_buf = rec;
465 rc = dt_record_write(env, o, &lgi->lgi_buf,
470 /* update the header */
471 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
472 lgi->lgi_buf.lb_buf = llh;
473 rc = dt_record_write(env, o, &lgi->lgi_buf,
478 /* update the bitmap */
479 index = reccookie->lgc_index;
480 lgi->lgi_off = llh->llh_bitmap_offset +
481 (index / (sizeof(*bitmap) * 8)) *
483 lgi->lgi_buf.lb_len = sizeof(*bitmap);
484 lgi->lgi_buf.lb_buf =
485 &bitmap[index/(sizeof(*bitmap)*8)];
486 rc = dt_record_write(env, o, &lgi->lgi_buf,
490 } else if (loghandle->lgh_cur_idx > 0) {
492 * The lgh_cur_offset can be used only if index is
495 if (idx != loghandle->lgh_cur_idx) {
496 CERROR("%s: modify index mismatch %d %d\n",
497 o->do_lu.lo_dev->ld_obd->obd_name, idx,
498 loghandle->lgh_cur_idx);
502 lgi->lgi_off = loghandle->lgh_cur_offset;
503 CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
504 "len:%u offset %llu\n",
505 POSTID(&loghandle->lgh_id.lgl_oi), idx,
506 rec->lrh_len, (long long)lgi->lgi_off);
507 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
508 lgi->lgi_off = llh->llh_hdr.lrh_len +
511 /* This can be result of lgh_cur_idx is not set during
512 * llog processing or llh_size is not set to proper
513 * record size for fixed records llog. Therefore it is
514 * impossible to get record offset. */
515 CERROR("%s: can't get record offset, idx:%d, "
516 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
521 /* update only data, header and tail remain the same */
522 lgi->lgi_off += sizeof(struct llog_rec_hdr);
523 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
524 lgi->lgi_buf.lb_buf = REC_DATA(rec);
525 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
526 if (rc == 0 && reccookie) {
527 reccookie->lgc_lgl = loghandle->lgh_id;
528 reccookie->lgc_index = idx;
536 * The most common case of using llog. The new index is assigned to
537 * the new record, new bit is set in llog bitmap and llog count is
540 * Make sure that records don't cross a chunk boundary, so we can
541 * process them page-at-a-time if needed. If it will cross a chunk
542 * boundary, write in a fake (but referenced) entry to pad the chunk.
546 /* simulate ENOSPC when new plain llog is being added to the
548 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
549 llh->llh_flags & LLOG_F_IS_CAT)
552 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
553 lgi->lgi_off = lgi->lgi_attr.la_size;
554 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
555 /* NOTE: padding is a record, but no bit is set */
556 if (left != 0 && left != reclen &&
557 left < (reclen + LLOG_MIN_REC_SIZE)) {
558 index = loghandle->lgh_last_idx + 1;
559 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
562 loghandle->lgh_last_idx++; /* for pad rec */
564 /* if it's the last idx in log file, then return -ENOSPC
565 * or wrap around if a catalog */
566 if (llog_is_full(loghandle) ||
567 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
568 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
569 loghandle->lgh_last_idx >= cfs_fail_val)) {
570 if (llh->llh_flags & LLOG_F_IS_CAT)
571 loghandle->lgh_last_idx = 0;
576 /* increment the last_idx along with llh_tail index, they should
577 * be equal for a llog lifetime */
578 loghandle->lgh_last_idx++;
579 index = loghandle->lgh_last_idx;
580 LLOG_HDR_TAIL(llh)->lrt_index = index;
582 * NB: the caller should make sure only 1 process access
583 * the lgh_last_idx, e.g. append should be exclusive.
584 * Otherwise it might hit the assert.
586 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
587 rec->lrh_index = index;
589 lrt->lrt_len = rec->lrh_len;
590 lrt->lrt_index = rec->lrh_index;
592 /* the lgh_hdr_mutex protects llog header data from concurrent
593 * update/cancel, the llh_count and llh_bitmap are protected */
594 mutex_lock(&loghandle->lgh_hdr_mutex);
595 if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
596 CERROR("%s: index %u already set in log bitmap\n",
597 o->do_lu.lo_dev->ld_obd->obd_name, index);
598 mutex_unlock(&loghandle->lgh_hdr_mutex);
599 LBUG(); /* should never happen */
603 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
604 /* Update the minimum size of the llog record */
605 if (llh->llh_size == 0)
606 llh->llh_size = reclen;
607 else if (reclen < llh->llh_size)
608 llh->llh_size = reclen;
611 if (lgi->lgi_attr.la_size == 0) {
613 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
614 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
615 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
617 GOTO(out_unlock, rc);
619 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
621 /* Note: If this is not initialization (size == 0), then do not
622 * write the whole header (8k bytes), only update header/tail
623 * and bits needs to be updated. Because this update might be
624 * part of cross-MDT operation, which needs to write these
625 * updates into the update log(32KB limit) and also pack inside
626 * the RPC (1MB limit), if we write 8K for each operation, which
627 * will cost a lot space, and keep us adding more updates to one
630 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
631 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
632 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
634 GOTO(out_unlock, rc);
636 lgi->lgi_off = llh->llh_bitmap_offset +
637 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
638 lgi->lgi_buf.lb_len = sizeof(*bitmap);
639 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
640 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
642 GOTO(out_unlock, rc);
644 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
646 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
647 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
648 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
650 GOTO(out_unlock, rc);
654 /* unlock here for remote object */
655 mutex_unlock(&loghandle->lgh_hdr_mutex);
659 /* computed index can be used to determine offset for fixed-size
660 * records. This also allows to handle Catalog wrap around case */
661 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
662 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
664 rc = dt_attr_get(env, o, &lgi->lgi_attr);
668 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
669 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
673 lgi->lgi_buf.lb_len = reclen;
674 lgi->lgi_buf.lb_buf = rec;
675 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
679 CDEBUG(D_OTHER, "added record "DOSTID": idx: %u, %u off"LPU64"\n",
680 POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len,
682 if (reccookie != NULL) {
683 reccookie->lgc_lgl = loghandle->lgh_id;
684 reccookie->lgc_index = index;
685 if ((rec->lrh_type == MDS_UNLINK_REC) ||
686 (rec->lrh_type == MDS_SETATTR64_REC))
687 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
688 else if (rec->lrh_type == OST_SZ_REC)
689 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
691 reccookie->lgc_subsys = -1;
696 /* cleanup llog for error case */
697 mutex_lock(&loghandle->lgh_hdr_mutex);
698 ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
700 mutex_unlock(&loghandle->lgh_hdr_mutex);
702 /* restore llog last_idx */
703 if (--loghandle->lgh_last_idx == 0 &&
704 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
705 /* catalog had just wrap-around case */
706 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
708 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
714 * We can skip reading at least as many log blocks as the number of
715 * minimum sized log records we are skipping. If it turns out
716 * that we are not far enough along the log (because the
717 * actual records are larger than minimum size) we just skip
720 * Note: in llog_process_thread, it will use bitmap offset as
721 * the index to locate the record, which also includs some pad
722 * records, whose record size is very small, and it also does not
723 * consider pad record when recording minimum record size (otherwise
724 * min_record size might be too small), so in some rare cases,
725 * it might skip too much record for @goal, see llog_osd_next_block().
727 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
728 * as the min record size to skip over, usually because in the previous
729 * try, it skip too much record, see loog_osd_next(prev)_block().
731 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
732 int curr, int goal, __u32 chunk_size,
735 struct llog_log_hdr *llh = lgh->lgh_hdr;
737 /* Goal should not bigger than the record count */
738 if (goal > lgh->lgh_last_idx)
739 goal = lgh->lgh_last_idx;
742 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
743 *off = chunk_size + (goal - 1) * llh->llh_size;
745 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
747 if (llh->llh_size > 0 && !force_mini_rec)
748 min_rec_size = llh->llh_size;
750 *off = *off + (goal - curr - 1) * min_rec_size;
753 /* always align with lower chunk boundary*/
754 *off &= ~(chunk_size - 1);
758 * Remove optional fields that the client doesn't expect.
759 * This is typically in order to ensure compatibility with older clients.
760 * It is assumed that since we exclusively remove fields, the block will be
761 * big enough to handle the remapped records. It is also assumed that records
762 * of a block have the same format (i.e.: the same features enabled).
764 * \param[in,out] hdr Header of the block of records to remap.
765 * \param[in,out] last_hdr Last header, don't read past this point.
766 * \param[in] flags Flags describing the fields to keep.
768 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
769 struct llog_rec_hdr *last_hdr,
770 enum changelog_rec_flags flags)
772 if (hdr->lrh_type != CHANGELOG_REC)
776 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
778 changelog_remap_rec(rec, rec->cr_flags & flags);
779 hdr = llog_rec_hdr_next(hdr);
780 } while ((char *)hdr <= (char *)last_hdr);
784 * Implementation of the llog_operations::lop_next_block
786 * This function finds the the next llog block to return which contains
787 * record with required index. It is main part of llog processing.
789 * \param[in] env execution environment
790 * \param[in] loghandle llog handle of the current llog
791 * \param[in,out] cur_idx index preceeding cur_offset
792 * \param[in] next_idx target index to find
793 * \param[in,out] cur_offset furtherst point read in the file
794 * \param[in] buf pointer to data buffer to fill
795 * \param[in] len required len to read, it is
796 * usually llog chunk_size.
798 * \retval 0 on successful buffer read
799 * \retval negative value on error
801 static int llog_osd_next_block(const struct lu_env *env,
802 struct llog_handle *loghandle, int *cur_idx,
803 int next_idx, __u64 *cur_offset, void *buf,
806 struct llog_thread_info *lgi = llog_info(env);
808 struct dt_device *dt;
811 int last_idx = *cur_idx;
812 __u64 last_offset = *cur_offset;
813 bool force_mini_rec = false;
820 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
821 if (len == 0 || len & (chunk_size - 1))
824 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
825 next_idx, *cur_idx, *cur_offset);
828 LASSERT(loghandle->lgh_ctxt);
830 o = loghandle->lgh_obj;
832 LASSERT(dt_object_exists(o));
833 dt = lu2dt_dev(o->do_lu.lo_dev);
836 rc = dt_attr_get(env, o, &lgi->lgi_attr);
840 while (*cur_offset < lgi->lgi_attr.la_size) {
841 struct llog_rec_hdr *rec, *last_rec;
842 struct llog_rec_tail *tail;
844 llog_skip_over(loghandle, cur_offset, *cur_idx,
845 next_idx, chunk_size, force_mini_rec);
847 /* read up to next llog chunk_size block */
848 lgi->lgi_buf.lb_len = chunk_size -
849 (*cur_offset & (chunk_size - 1));
850 lgi->lgi_buf.lb_buf = buf;
852 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
854 if (rc == -EBADR && !force_mini_rec)
857 CERROR("%s: can't read llog block from log "DFID
858 " offset "LPU64": rc = %d\n",
859 o->do_lu.lo_dev->ld_obd->obd_name,
860 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
866 /* signal the end of the valid buffer to
868 memset(buf + rc, 0, len - rc);
871 if (rc == 0) { /* end of file, nothing to do */
877 if (rc < sizeof(*tail)) {
881 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
883 o->do_lu.lo_dev->ld_obd->obd_name,
884 POSTID(&loghandle->lgh_id.lgl_oi),
885 loghandle->lgh_id.lgl_ogen, *cur_offset);
886 GOTO(out, rc = -EINVAL);
890 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
891 lustre_swab_llog_rec(rec);
893 tail = (struct llog_rec_tail *)((char *)buf + rc -
894 sizeof(struct llog_rec_tail));
895 /* get the last record in block */
896 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
899 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
900 lustre_swab_llog_rec(last_rec);
901 LASSERT(last_rec->lrh_index == tail->lrt_index);
903 *cur_idx = tail->lrt_index;
905 /* this shouldn't happen */
906 if (tail->lrt_index == 0) {
907 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
908 "offset "LPU64" bytes %d\n",
909 o->do_lu.lo_dev->ld_obd->obd_name,
910 POSTID(&loghandle->lgh_id.lgl_oi),
911 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
912 GOTO(out, rc = -EINVAL);
914 if (tail->lrt_index < next_idx) {
916 last_offset = *cur_offset;
920 /* sanity check that the start of the new buffer is no farther
921 * than the record that we wanted. This shouldn't happen. */
922 if (rec->lrh_index > next_idx) {
923 if (!force_mini_rec && next_idx > last_idx)
926 CERROR("%s: missed desired record? %u > %u\n",
927 o->do_lu.lo_dev->ld_obd->obd_name,
928 rec->lrh_index, next_idx);
929 GOTO(out, rc = -ENOENT);
932 /* Trim unsupported extensions for compat w/ older clients */
933 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
934 changelog_block_trim_ext(rec, last_rec,
935 CLF_VERSION | CLF_RENAME);
940 /* Note: because there are some pad records in the
941 * llog, so llog_skip_over() might skip too much
942 * records, let's try skip again with minimum record */
943 force_mini_rec = true;
944 *cur_offset = last_offset;
947 GOTO(out, rc = -EIO);
953 * Implementation of the llog_operations::lop_prev_block
955 * This function finds the llog block to return which contains
956 * record with required index but in reverse order - from end of llog
958 * It is main part of reverse llog processing.
960 * \param[in] env execution environment
961 * \param[in] loghandle llog handle of the current llog
962 * \param[in] prev_idx target index to find
963 * \param[in] buf pointer to data buffer to fill
964 * \param[in] len required len to read, it is llog_chunk_size usually.
966 * \retval 0 on successful buffer read
967 * \retval negative value on error
969 static int llog_osd_prev_block(const struct lu_env *env,
970 struct llog_handle *loghandle,
971 int prev_idx, void *buf, int len)
973 struct llog_thread_info *lgi = llog_info(env);
975 struct dt_device *dt;
982 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
983 if (len == 0 || len & (chunk_size - 1))
986 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
989 LASSERT(loghandle->lgh_ctxt);
991 o = loghandle->lgh_obj;
993 LASSERT(dt_object_exists(o));
994 dt = lu2dt_dev(o->do_lu.lo_dev);
997 /* Let's only use mini record size for previous block read
999 cur_offset = chunk_size;
1000 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1003 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1007 while (cur_offset < lgi->lgi_attr.la_size) {
1008 struct llog_rec_hdr *rec, *last_rec;
1009 struct llog_rec_tail *tail;
1011 lgi->lgi_buf.lb_len = len;
1012 lgi->lgi_buf.lb_buf = buf;
1013 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1015 CERROR("%s: can't read llog block from log "DFID
1016 " offset "LPU64": rc = %d\n",
1017 o->do_lu.lo_dev->ld_obd->obd_name,
1018 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1022 if (rc == 0) /* end of file, nothing to do */
1025 if (rc < sizeof(*tail)) {
1026 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
1028 o->do_lu.lo_dev->ld_obd->obd_name,
1029 POSTID(&loghandle->lgh_id.lgl_oi),
1030 loghandle->lgh_id.lgl_ogen, cur_offset);
1031 GOTO(out, rc = -EINVAL);
1035 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1036 lustre_swab_llog_rec(rec);
1038 tail = (struct llog_rec_tail *)((char *)buf + rc -
1039 sizeof(struct llog_rec_tail));
1040 /* get the last record in block */
1041 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1042 le32_to_cpu(tail->lrt_len));
1044 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1045 lustre_swab_llog_rec(last_rec);
1046 LASSERT(last_rec->lrh_index == tail->lrt_index);
1048 /* this shouldn't happen */
1049 if (tail->lrt_index == 0) {
1050 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
1052 o->do_lu.lo_dev->ld_obd->obd_name,
1053 POSTID(&loghandle->lgh_id.lgl_oi),
1054 loghandle->lgh_id.lgl_ogen, cur_offset);
1055 GOTO(out, rc = -EINVAL);
1057 if (tail->lrt_index < prev_idx)
1060 /* sanity check that the start of the new buffer is no farther
1061 * than the record that we wanted. This shouldn't happen. */
1062 if (rec->lrh_index > prev_idx) {
1063 CERROR("%s: missed desired record? %u > %u\n",
1064 o->do_lu.lo_dev->ld_obd->obd_name,
1065 rec->lrh_index, prev_idx);
1066 GOTO(out, rc = -ENOENT);
1069 /* Trim unsupported extensions for compat w/ older clients */
1070 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
1071 changelog_block_trim_ext(rec, last_rec,
1072 CLF_VERSION | CLF_RENAME);
1076 GOTO(out, rc = -EIO);
1082 * This is helper function to get llog directory object. It is used by named
1083 * llog operations to find/insert/delete llog entry from llog directory.
1085 * \param[in] env execution environment
1086 * \param[in] ctxt llog context
1088 * \retval dt_object of llog directory
1089 * \retval ERR_PTR of negative value on error
1091 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1092 struct llog_ctxt *ctxt)
1094 struct dt_device *dt;
1095 struct dt_thread_info *dti = dt_info(env);
1096 struct dt_object *dir;
1099 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1100 if (ctxt->loc_dir == NULL) {
1101 rc = dt_root_get(env, dt, &dti->dti_fid);
1104 dir = dt_locate(env, dt, &dti->dti_fid);
1106 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1107 lu_object_put(env, &dir->do_lu);
1108 return ERR_PTR(-ENOTDIR);
1111 lu_object_get(&ctxt->loc_dir->do_lu);
1112 dir = ctxt->loc_dir;
1119 * Implementation of the llog_operations::lop_open
1121 * This function opens the llog by its logid or by name, it may open also
1122 * non existent llog and assing then new id to it.
1123 * The llog_open/llog_close pair works similar to lu_object_find/put,
1124 * the object may not exist prior open. The result of open is just dt_object
1125 * in the llog header.
1127 * \param[in] env execution environment
1128 * \param[in] handle llog handle of the current llog
1129 * \param[in] logid logid of llog to open (nameless llog)
1130 * \param[in] name name of llog to open (named llog)
1131 * \param[in] open_param
1132 * LLOG_OPEN_NEW - new llog, may not exist
1133 * LLOG_OPEN_EXIST - old llog, must exist
1135 * \retval 0 on successful open, llog_handle::lgh_obj
1136 * contains the dt_object of the llog.
1137 * \retval negative value on error
1139 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1140 struct llog_logid *logid, char *name,
1141 enum llog_open_param open_param)
1143 struct llog_thread_info *lgi = llog_info(env);
1144 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1145 struct dt_object *o;
1146 struct dt_device *dt;
1147 struct ls_device *ls;
1148 struct local_oid_storage *los = NULL;
1155 LASSERT(ctxt->loc_exp);
1156 LASSERT(ctxt->loc_exp->exp_obd);
1157 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1159 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1160 struct lu_object_conf conf = { 0 };
1161 if (logid != NULL) {
1162 logid_to_fid(logid, &lgi->lgi_fid);
1164 /* If logid == NULL, then it means the caller needs
1165 * to allocate new FID (llog_cat_declare_add_rec()). */
1166 rc = obd_fid_alloc(env, ctxt->loc_exp,
1167 &lgi->lgi_fid, NULL);
1171 conf.loc_flags = LOC_F_NEW;
1174 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1175 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1182 ls = ls_device_get(dt);
1184 RETURN(PTR_ERR(ls));
1186 mutex_lock(&ls->ls_los_mutex);
1187 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1188 mutex_unlock(&ls->ls_los_mutex);
1190 ls_device_put(env, ls);
1194 if (logid != NULL) {
1195 logid_to_fid(logid, &lgi->lgi_fid);
1197 struct dt_object *llog_dir;
1199 llog_dir = llog_osd_dir_get(env, ctxt);
1200 if (IS_ERR(llog_dir))
1201 GOTO(out, rc = PTR_ERR(llog_dir));
1202 dt_read_lock(env, llog_dir, 0);
1203 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1204 dt_read_unlock(env, llog_dir);
1205 lu_object_put(env, &llog_dir->do_lu);
1206 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1207 /* generate fid for new llog */
1208 rc = local_object_fid_generate(env, los,
1213 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1214 if (handle->lgh_name)
1215 strcpy(handle->lgh_name, name);
1217 GOTO(out, rc = -ENOMEM);
1219 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1220 /* generate fid for new llog */
1221 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1226 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1228 GOTO(out_name, rc = PTR_ERR(o));
1231 /* No new llog is expected but doesn't exist */
1232 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
1233 GOTO(out_put, rc = -ENOENT);
1235 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1236 handle->lgh_obj = o;
1237 handle->private_data = los;
1238 LASSERT(handle->lgh_ctxt);
1243 lu_object_put(env, &o->do_lu);
1245 if (handle->lgh_name != NULL)
1246 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1254 * Get dir for regular fid log object
1256 * Get directory for regular fid log object, and these regular fid log
1257 * object will be inserted under this directory, to satisfy the FS
1258 * consistency check, e2fsck etc.
1260 * \param [in] env execution environment
1261 * \param [in] dto llog object
1263 * \retval pointer to the directory if it is found.
1264 * \retval ERR_PTR(negative errno) if it fails.
1266 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1267 struct dt_object *dto)
1269 struct llog_thread_info *lgi = llog_info(env);
1270 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1271 struct lu_seq_range *range = &lgi->lgi_range;
1272 struct lu_fid *dir_fid = &lgi->lgi_fid;
1273 struct dt_object *dir;
1277 fld_range_set_any(range);
1278 LASSERT(ss != NULL);
1279 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1280 fid_seq(lu_object_fid(&dto->do_lu)), range);
1282 RETURN(ERR_PTR(rc));
1284 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1285 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1289 if (!dt_try_as_dir(env, dir)) {
1290 lu_object_put(env, &dir->do_lu);
1291 RETURN(ERR_PTR(-ENOTDIR));
1298 * Add llog object with regular FID to name entry
1300 * Add llog object with regular FID to name space, and each llog
1301 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1302 * so to satisfy the namespace consistency check, e2fsck etc.
1304 * \param [in] env execution environment
1305 * \param [in] dto llog object
1306 * \param [in] th thandle
1307 * \param [in] declare if it is declare or execution
1309 * \retval 0 if insertion succeeds.
1310 * \retval negative errno if insertion fails.
1313 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1314 struct dt_object *dto,
1315 struct thandle *th, bool declare)
1317 struct llog_thread_info *lgi = llog_info(env);
1318 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1319 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1320 struct dt_object *dir;
1321 char *name = lgi->lgi_name;
1325 if (!fid_is_norm(fid))
1328 dir = llog_osd_get_regular_fid_dir(env, dto);
1330 RETURN(PTR_ERR(dir));
1333 rec->rec_type = S_IFREG;
1334 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1335 dt_write_lock(env, dir, 0);
1337 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1338 (struct dt_key *)name, th);
1340 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1341 (struct dt_key *)name, th, 1);
1343 dt_write_unlock(env, dir);
1345 lu_object_put(env, &dir->do_lu);
1351 * Implementation of the llog_operations::lop_declare_create
1353 * This function declares the llog create. It declares also name insert
1354 * into llog directory in case of named llog.
1356 * \param[in] env execution environment
1357 * \param[in] res llog handle of the current llog
1358 * \param[in] th current transaction handle
1360 * \retval 0 on successful create declaration
1361 * \retval negative value on error
1363 static int llog_osd_declare_create(const struct lu_env *env,
1364 struct llog_handle *res, struct thandle *th)
1366 struct llog_thread_info *lgi = llog_info(env);
1367 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1368 struct local_oid_storage *los;
1369 struct dt_object *o;
1374 LASSERT(res->lgh_obj);
1377 /* object can be created by another thread */
1379 if (dt_object_exists(o))
1382 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1383 struct llog_thread_info *lgi = llog_info(env);
1385 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1386 lgi->lgi_attr.la_size = 0;
1387 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1388 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1390 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1396 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1400 los = res->private_data;
1403 rc = llog_osd_declare_new_object(env, los, o, th);
1407 /* do not declare header initialization here as it's declared
1408 * in llog_osd_declare_write_rec() which is always called */
1410 if (res->lgh_name) {
1411 struct dt_object *llog_dir;
1413 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1414 if (IS_ERR(llog_dir))
1415 RETURN(PTR_ERR(llog_dir));
1416 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1417 rec->rec_fid = &lgi->lgi_fid;
1418 rec->rec_type = S_IFREG;
1419 rc = dt_declare_insert(env, llog_dir,
1420 (struct dt_rec *)rec,
1421 (struct dt_key *)res->lgh_name, th);
1422 lu_object_put(env, &llog_dir->do_lu);
1424 CERROR("%s: can't declare named llog %s: rc = %d\n",
1425 o->do_lu.lo_dev->ld_obd->obd_name,
1432 * Implementation of the llog_operations::lop_create
1434 * This function creates the llog according with llog_handle::lgh_obj
1435 * and llog_handle::lgh_name.
1437 * \param[in] env execution environment
1438 * \param[in] res llog handle of the current llog
1439 * \param[in] th current transaction handle
1441 * \retval 0 on successful create
1442 * \retval negative value on error
1444 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1447 struct llog_thread_info *lgi = llog_info(env);
1448 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1449 struct local_oid_storage *los;
1450 struct dt_object *o;
1459 /* llog can be already created */
1460 if (dt_object_exists(o))
1463 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1464 struct llog_thread_info *lgi = llog_info(env);
1466 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1467 lgi->lgi_attr.la_size = 0;
1468 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1469 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1471 dt_write_lock(env, o, 0);
1472 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1474 dt_write_unlock(env, o);
1478 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1483 los = res->private_data;
1486 dt_write_lock(env, o, 0);
1487 if (!dt_object_exists(o))
1488 rc = llog_osd_create_new_object(env, los, o, th);
1492 dt_write_unlock(env, o);
1496 if (res->lgh_name) {
1497 struct dt_object *llog_dir;
1499 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1500 if (IS_ERR(llog_dir))
1501 RETURN(PTR_ERR(llog_dir));
1503 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1504 rec->rec_fid = &lgi->lgi_fid;
1505 rec->rec_type = S_IFREG;
1506 dt_read_lock(env, llog_dir, 0);
1507 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1508 (struct dt_key *)res->lgh_name,
1510 dt_read_unlock(env, llog_dir);
1511 lu_object_put(env, &llog_dir->do_lu);
1513 CERROR("%s: can't create named llog %s: rc = %d\n",
1514 o->do_lu.lo_dev->ld_obd->obd_name,
1521 * Implementation of the llog_operations::lop_close
1523 * This function closes the llog. It just put llog object and referenced
1526 * \param[in] env execution environment
1527 * \param[in] handle llog handle of the current llog
1529 * \retval 0 on successful llog close
1530 * \retval negative value on error
1532 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1534 struct local_oid_storage *los;
1539 LASSERT(handle->lgh_obj);
1541 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1542 /* Remove the object from the cache, otherwise it may
1543 * hold LOD being released during cleanup process */
1544 lu_object_put_nocache(env, &handle->lgh_obj->do_lu);
1545 LASSERT(handle->private_data == NULL);
1548 lu_object_put(env, &handle->lgh_obj->do_lu);
1550 los = handle->private_data;
1554 if (handle->lgh_name)
1555 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1561 * delete llog object name entry
1563 * Delete llog object (with regular FID) from name space (under
1566 * \param [in] env execution environment
1567 * \param [in] dto llog object
1568 * \param [in] th thandle
1569 * \param [in] declare if it is declare or execution
1571 * \retval 0 if deletion succeeds.
1572 * \retval negative errno if deletion fails.
1575 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1576 struct dt_object *dto,
1577 struct thandle *th, bool declare)
1579 struct llog_thread_info *lgi = llog_info(env);
1580 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1581 struct dt_object *dir;
1582 char *name = lgi->lgi_name;
1586 if (!fid_is_norm(fid))
1589 dir = llog_osd_get_regular_fid_dir(env, dto);
1591 RETURN(PTR_ERR(dir));
1593 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1594 dt_write_lock(env, dir, 0);
1596 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1599 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1601 dt_write_unlock(env, dir);
1603 lu_object_put(env, &dir->do_lu);
1608 * Implementation of the llog_operations::lop_declare_destroy
1610 * This function declare destroys the llog and deletes also entry in the
1611 * llog directory in case of named llog. Llog should be opened prior that.
1613 * \param[in] env execution environment
1614 * \param[in] loghandle llog handle of the current llog
1616 * \retval 0 on successful destroy
1617 * \retval negative value on error
1619 static int llog_osd_declare_destroy(const struct lu_env *env,
1620 struct llog_handle *loghandle,
1623 struct llog_ctxt *ctxt;
1624 struct dt_object *o, *llog_dir = NULL;
1629 ctxt = loghandle->lgh_ctxt;
1632 o = loghandle->lgh_obj;
1635 if (loghandle->lgh_name) {
1636 llog_dir = llog_osd_dir_get(env, ctxt);
1637 if (IS_ERR(llog_dir))
1638 RETURN(PTR_ERR(llog_dir));
1640 rc = dt_declare_delete(env, llog_dir,
1641 (struct dt_key *)loghandle->lgh_name,
1647 rc = dt_declare_ref_del(env, o, th);
1651 rc = dt_declare_destroy(env, o, th);
1655 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1656 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1662 if (!(IS_ERR_OR_NULL(llog_dir)))
1663 lu_object_put(env, &llog_dir->do_lu);
1670 * Implementation of the llog_operations::lop_destroy
1672 * This function destroys the llog and deletes also entry in the
1673 * llog directory in case of named llog. Llog should be opened prior that.
1674 * Destroy method is not part of external transaction and does everything
1677 * \param[in] env execution environment
1678 * \param[in] loghandle llog handle of the current llog
1680 * \retval 0 on successful destroy
1681 * \retval negative value on error
1683 static int llog_osd_destroy(const struct lu_env *env,
1684 struct llog_handle *loghandle, struct thandle *th)
1686 struct llog_ctxt *ctxt;
1687 struct dt_object *o, *llog_dir = NULL;
1692 ctxt = loghandle->lgh_ctxt;
1693 LASSERT(ctxt != NULL);
1695 o = loghandle->lgh_obj;
1698 dt_write_lock(env, o, 0);
1699 if (!dt_object_exists(o))
1700 GOTO(out_unlock, rc = 0);
1702 if (loghandle->lgh_name) {
1703 llog_dir = llog_osd_dir_get(env, ctxt);
1704 if (IS_ERR(llog_dir))
1705 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1707 dt_read_lock(env, llog_dir, 0);
1708 rc = dt_delete(env, llog_dir,
1709 (struct dt_key *)loghandle->lgh_name,
1711 dt_read_unlock(env, llog_dir);
1713 CERROR("%s: can't remove llog %s: rc = %d\n",
1714 o->do_lu.lo_dev->ld_obd->obd_name,
1715 loghandle->lgh_name, rc);
1716 GOTO(out_unlock, rc);
1720 dt_ref_del(env, o, th);
1721 rc = dt_destroy(env, o, th);
1723 GOTO(out_unlock, rc);
1725 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1726 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1728 GOTO(out_unlock, rc);
1732 dt_write_unlock(env, o);
1733 if (!(IS_ERR_OR_NULL(llog_dir)))
1734 lu_object_put(env, &llog_dir->do_lu);
1739 * Implementation of the llog_operations::lop_setup
1741 * This function setup the llog on local storage.
1743 * \param[in] env execution environment
1744 * \param[in] obd obd device the llog belongs to
1745 * \param[in] olg the llog group, it is always zero group now.
1746 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1747 * Every new llog type have to use own index.
1748 * \param[in] disk_obd the storage obd, where llog is stored.
1750 * \retval 0 on successful llog setup
1751 * \retval negative value on error
1753 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1754 struct obd_llog_group *olg, int ctxt_idx,
1755 struct obd_device *disk_obd)
1757 struct llog_thread_info *lgi = llog_info(env);
1758 struct llog_ctxt *ctxt;
1763 LASSERT(olg->olg_ctxts[ctxt_idx]);
1765 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1768 if (disk_obd == NULL)
1771 /* initialize data allowing to generate new fids,
1772 * literally we need a sequece */
1773 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1774 lgi->lgi_fid.f_oid = 1;
1775 lgi->lgi_fid.f_ver = 0;
1776 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1778 &ctxt->loc_los_nameless);
1782 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1783 lgi->lgi_fid.f_oid = 1;
1784 lgi->lgi_fid.f_ver = 0;
1785 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1787 &ctxt->loc_los_named);
1789 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1790 ctxt->loc_los_nameless = NULL;
1796 llog_ctxt_put(ctxt);
1801 * Implementation of the llog_operations::lop_cleanup
1803 * This function cleanups the llog on local storage.
1805 * \param[in] env execution environment
1806 * \param[in] ctxt the llog context
1810 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1812 if (ctxt->loc_los_nameless != NULL) {
1813 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1814 ctxt->loc_los_nameless = NULL;
1817 if (ctxt->loc_los_named != NULL) {
1818 local_oid_storage_fini(env, ctxt->loc_los_named);
1819 ctxt->loc_los_named = NULL;
1825 struct llog_operations llog_osd_ops = {
1826 .lop_next_block = llog_osd_next_block,
1827 .lop_prev_block = llog_osd_prev_block,
1828 .lop_read_header = llog_osd_read_header,
1829 .lop_declare_destroy = llog_osd_declare_destroy,
1830 .lop_destroy = llog_osd_destroy,
1831 .lop_setup = llog_osd_setup,
1832 .lop_cleanup = llog_osd_cleanup,
1833 .lop_open = llog_osd_open,
1834 .lop_exist = llog_osd_exist,
1835 .lop_declare_create = llog_osd_declare_create,
1836 .lop_create = llog_osd_create,
1837 .lop_declare_write_rec = llog_osd_declare_write_rec,
1838 .lop_write_rec = llog_osd_write_rec,
1839 .lop_close = llog_osd_close,
1841 EXPORT_SYMBOL(llog_osd_ops);
1843 struct llog_operations llog_common_cat_ops = {
1844 .lop_next_block = llog_osd_next_block,
1845 .lop_prev_block = llog_osd_prev_block,
1846 .lop_read_header = llog_osd_read_header,
1847 .lop_declare_destroy = llog_osd_declare_destroy,
1848 .lop_destroy = llog_osd_destroy,
1849 .lop_setup = llog_osd_setup,
1850 .lop_cleanup = llog_osd_cleanup,
1851 .lop_open = llog_osd_open,
1852 .lop_exist = llog_osd_exist,
1853 .lop_declare_create = llog_osd_declare_create,
1854 .lop_create = llog_osd_create,
1855 .lop_declare_write_rec = llog_osd_declare_write_rec,
1856 .lop_write_rec = llog_osd_write_rec,
1857 .lop_close = llog_osd_close,
1858 .lop_add = llog_cat_add_rec,
1859 .lop_declare_add = llog_cat_declare_add_rec,
1861 EXPORT_SYMBOL(llog_common_cat_ops);
1864 * Read the special file which contains the list of llog catalogs IDs
1866 * This function reads the CATALOGS file which contains the array of llog
1867 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1868 * by OST/MDT number.
1870 * \param[in] env execution environment
1871 * \param[in] d corresponding storage device
1872 * \param[in] idx position to start from, usually OST/MDT index
1873 * \param[in] count how many catalog IDs to read
1874 * \param[out] idarray the buffer for the data. If it is NULL then
1875 * function returns just number of catalog IDs
1877 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1879 * \retval 0 on successful read of catalog IDs
1880 * \retval negative value on error
1881 * \retval positive value which is number of records in
1882 * the file if \a idarray is NULL
1884 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1885 int idx, int count, struct llog_catid *idarray,
1886 const struct lu_fid *fid)
1888 struct llog_thread_info *lgi = llog_info(env);
1889 struct dt_object *o = NULL;
1897 size = sizeof(*idarray) * count;
1898 lgi->lgi_off = idx * sizeof(*idarray);
1900 lgi->lgi_fid = *fid;
1901 o = dt_locate(env, d, &lgi->lgi_fid);
1905 if (!dt_object_exists(o)) {
1906 th = dt_trans_create(env, d);
1908 GOTO(out, rc = PTR_ERR(th));
1910 lgi->lgi_attr.la_valid = LA_MODE;
1911 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1912 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1914 th->th_wait_submit = 1;
1915 /* Make the llog object creation synchronization, so
1916 * it will be reliable to the reference, especially
1917 * for remote reference */
1920 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1923 GOTO(out_trans, rc);
1925 rc = dt_trans_start_local(env, d, th);
1927 GOTO(out_trans, rc);
1929 dt_write_lock(env, o, 0);
1930 if (!dt_object_exists(o))
1931 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1933 dt_write_unlock(env, o);
1935 dt_trans_stop(env, d, th);
1940 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1944 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1945 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1946 o->do_lu.lo_dev->ld_obd->obd_name,
1947 lgi->lgi_attr.la_mode);
1948 GOTO(out, rc = -ENOENT);
1951 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1952 (int)lgi->lgi_attr.la_size, size);
1954 /* return just number of llogs */
1955 if (idarray == NULL) {
1956 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1960 /* read for new ost index or for empty file */
1961 memset(idarray, 0, size);
1962 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1964 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1965 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1967 lgi->lgi_buf.lb_buf = idarray;
1968 lgi->lgi_buf.lb_len = size;
1969 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1970 /* -EFAULT means the llog is a sparse file. This is not an error
1971 * after arbitrary OST index is supported. */
1972 if (rc < 0 && rc != -EFAULT) {
1973 CERROR("%s: error reading CATALOGS: rc = %d\n",
1974 o->do_lu.lo_dev->ld_obd->obd_name, rc);
1980 lu_object_put(env, &o->do_lu);
1983 EXPORT_SYMBOL(llog_osd_get_cat_list);
1986 * Write the special file which contains the list of llog catalogs IDs
1988 * This function writes the CATALOG file which contains the array of llog
1989 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1992 * \param[in] env execution environment
1993 * \param[in] d corresponding storage device
1994 * \param[in] idx position to start from, usually OST/MDT index
1995 * \param[in] count how many catalog IDs to write
1996 * \param[out] idarray the buffer with the data to write.
1997 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1999 * \retval 0 on successful write of catalog IDs
2000 * \retval negative value on error
2002 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2003 int idx, int count, struct llog_catid *idarray,
2004 const struct lu_fid *fid)
2006 struct llog_thread_info *lgi = llog_info(env);
2007 struct dt_object *o = NULL;
2016 size = sizeof(*idarray) * count;
2017 lgi->lgi_off = idx * sizeof(*idarray);
2018 lgi->lgi_fid = *fid;
2020 o = dt_locate(env, d, &lgi->lgi_fid);
2024 if (!dt_object_exists(o))
2025 GOTO(out, rc = -ENOENT);
2027 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2031 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2032 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2033 o->do_lu.lo_dev->ld_obd->obd_name,
2034 lgi->lgi_attr.la_mode);
2035 GOTO(out, rc = -ENOENT);
2038 th = dt_trans_create(env, d);
2040 GOTO(out, rc = PTR_ERR(th));
2042 lgi->lgi_buf.lb_len = size;
2043 lgi->lgi_buf.lb_buf = idarray;
2044 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2046 GOTO(out_trans, rc);
2048 /* For update log, this happens during initialization,
2049 * see lod_sub_prep_llog(), and we need make sure catlog
2050 * file ID is written to catlist file(committed) before
2051 * cross-MDT operation write update records to catlog FILE,
2052 * otherwise, during failover these update records might
2054 if (fid_is_update_log(fid))
2057 rc = dt_trans_start_local(env, d, th);
2059 GOTO(out_trans, rc);
2061 th->th_wait_submit = 1;
2063 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2065 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2068 dt_trans_stop(env, d, th);
2070 lu_object_put(env, &o->do_lu);
2073 EXPORT_SYMBOL(llog_osd_put_cat_list);