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
47 #include <dt_object.h>
48 #include <llog_swab.h>
49 #include <lustre_fid.h>
51 #include <obd_class.h>
53 #include "llog_internal.h"
54 #include "local_storage.h"
57 * Implementation of the llog_operations::lop_declare_create
59 * This function is a wrapper over local_storage API function
60 * local_object_declare_create().
62 * \param[in] env execution environment
63 * \param[in] los local_storage for bottom storage device
64 * \param[in] o dt_object to create
65 * \param[in] th current transaction handle
67 * \retval 0 on successful declaration of the new object
68 * \retval negative error if declaration was failed
70 static int llog_osd_declare_new_object(const struct lu_env *env,
71 struct local_oid_storage *los,
75 struct llog_thread_info *lgi = llog_info(env);
77 lgi->lgi_attr.la_valid = LA_MODE;
78 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
79 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
81 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
86 * Implementation of the llog_operations::lop_create
88 * This function is a wrapper over local_storage API function
89 * local_object_create().
91 * \param[in] env execution environment
92 * \param[in] los local_storage for bottom storage device
93 * \param[in] o dt_object to create
94 * \param[in] th current transaction handle
96 * \retval 0 on successful creation of the new object
97 * \retval negative error if creation was failed
99 static int llog_osd_create_new_object(const struct lu_env *env,
100 struct local_oid_storage *los,
104 struct llog_thread_info *lgi = llog_info(env);
106 lgi->lgi_attr.la_valid = LA_MODE;
107 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
108 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
110 return local_object_create(env, los, o, &lgi->lgi_attr,
115 * Implementation of the llog_operations::lop_exist
117 * This function checks that llog exists on storage.
119 * \param[in] handle llog handle of the current llog
121 * \retval true if llog object exists and is not just destroyed
122 * \retval false if llog doesn't exist or just destroyed
124 static int llog_osd_exist(struct llog_handle *handle)
126 LASSERT(handle->lgh_obj);
127 return dt_object_exists(handle->lgh_obj) &&
128 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header);
132 * Write a padding record to the llog
134 * This function writes a padding record to the end of llog. That may
135 * be needed if llog contains records of variable size, e.g. config logs
137 * The padding record just aligns llog to the llog chunk_size boundary if
138 * the current record doesn't fit in the remaining space.
140 * It allocates full length to avoid two separate writes for header and tail.
141 * Such 2-steps scheme needs extra protection and complex error handling.
143 * \param[in] env execution environment
144 * \param[in] o dt_object to create
145 * \param[in,out] off pointer to the padding start offset
146 * \param[in] len padding length
147 * \param[in] index index of the padding record in a llog
148 * \param[in] th current transaction handle
150 * \retval 0 on successful padding write
151 * \retval negative error if write failed
153 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
154 loff_t *off, int len, int index, struct thandle *th)
156 struct llog_thread_info *lgi = llog_info(env);
157 struct llog_rec_hdr *rec;
158 struct llog_rec_tail *tail;
165 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
172 rec->lrh_index = index;
173 rec->lrh_type = LLOG_PAD_MAGIC;
175 tail = rec_tail(rec);
177 tail->lrt_index = index;
179 lgi->lgi_buf.lb_buf = rec;
180 lgi->lgi_buf.lb_len = len;
181 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
183 CERROR("%s: error writing padding record: rc = %d\n",
184 o->do_lu.lo_dev->ld_obd->obd_name, rc);
191 * Implementation of the llog_operations::lop_read_header
193 * This function reads the current llog header from the bottom storage
196 * \param[in] env execution environment
197 * \param[in] handle llog handle of the current llog
199 * \retval 0 on successful header read
200 * \retval negative error if read failed
202 static int llog_osd_read_header(const struct lu_env *env,
203 struct llog_handle *handle)
205 struct llog_rec_hdr *llh_hdr;
207 struct llog_thread_info *lgi;
208 enum llog_flag flags;
216 lgi = llog_info(env);
218 rc = dt_attr_get(env, o, &lgi->lgi_attr);
222 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
224 if (lgi->lgi_attr.la_size == 0) {
225 CDEBUG(D_HA, "not reading header from 0-byte log\n");
229 flags = handle->lgh_hdr->llh_flags;
232 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
233 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
234 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
235 llh_hdr = &handle->lgh_hdr->llh_hdr;
236 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
237 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
238 o->do_lu.lo_dev->ld_obd->obd_name,
239 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
248 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
249 lustre_swab_llog_hdr(handle->lgh_hdr);
251 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
252 CERROR("%s: bad log %s "DFID" header magic: %#x "
253 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
254 handle->lgh_name ? handle->lgh_name : "",
255 PFID(lu_object_fid(&o->do_lu)),
256 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
258 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
259 llh_hdr->lrh_len > handle->lgh_hdr_size) {
260 CERROR("%s: incorrectly sized log %s "DFID" header: "
261 "%#x (expected at least %#x)\n"
262 "you may need to re-run lconf --write_conf.\n",
263 o->do_lu.lo_dev->ld_obd->obd_name,
264 handle->lgh_name ? handle->lgh_name : "",
265 PFID(lu_object_fid(&o->do_lu)),
266 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
268 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
269 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
270 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
272 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
274 o->do_lu.lo_dev->ld_obd->obd_name,
275 handle->lgh_name ? handle->lgh_name : "",
276 PFID(lu_object_fid(&o->do_lu)),
277 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
281 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
282 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
288 * Implementation of the llog_operations::lop_declare_write
290 * This function declares the new record write.
292 * \param[in] env execution environment
293 * \param[in] loghandle llog handle of the current llog
294 * \param[in] rec llog record header. This is a real header of the full
295 * llog record to write. This is the beginning of buffer
296 * to write, the length of buffer is stored in
298 * \param[in] idx index of the llog record. If \a idx == -1 then this is
299 * append case, otherwise \a idx is the index of record
301 * \param[in] th current transaction handle
303 * \retval 0 on successful declaration
304 * \retval negative error if declaration failed
306 static int llog_osd_declare_write_rec(const struct lu_env *env,
307 struct llog_handle *loghandle,
308 struct llog_rec_hdr *rec,
309 int idx, struct thandle *th)
311 struct llog_thread_info *lgi = llog_info(env);
322 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
324 o = loghandle->lgh_obj;
327 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
328 lgi->lgi_buf.lb_len = chunk_size;
329 lgi->lgi_buf.lb_buf = NULL;
330 /* each time we update header */
331 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
333 if (rc || idx == 0) /* if error or just header */
337 * the pad record can be inserted so take into account double
340 lgi->lgi_buf.lb_len = chunk_size * 2;
341 lgi->lgi_buf.lb_buf = NULL;
342 /* XXX: implement declared window or multi-chunks approach */
343 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
349 * Implementation of the llog_operations::lop_write
351 * This function writes the new record in the llog or modify the existed one.
353 * \param[in] env execution environment
354 * \param[in] loghandle llog handle of the current llog
355 * \param[in] rec llog record header. This is a real header of
356 * the full llog record to write. This is
357 * the beginning of buffer to write, the length
358 * of buffer is stored in \a rec::lrh_len
359 * \param[out] reccookie pointer to the cookie to return back if needed.
360 * It is used for further cancel of this llog
362 * \param[in] idx index of the llog record. If \a idx == -1 then
363 * this is append case, otherwise \a idx is
364 * the index of record to modify
365 * \param[in] th current transaction handle
367 * \retval 0 on successful write && \a reccookie == NULL
368 * 1 on successful write && \a reccookie != NULL
369 * \retval negative error if write failed
371 static int llog_osd_write_rec(const struct lu_env *env,
372 struct llog_handle *loghandle,
373 struct llog_rec_hdr *rec,
374 struct llog_cookie *reccookie,
375 int idx, struct thandle *th)
377 struct llog_thread_info *lgi = llog_info(env);
378 struct llog_log_hdr *llh;
379 int reclen = rec->lrh_len;
381 struct llog_rec_tail *lrt;
389 llh = loghandle->lgh_hdr;
391 o = loghandle->lgh_obj;
395 chunk_size = llh->llh_hdr.lrh_len;
396 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
397 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
399 if (!llog_osd_exist(loghandle))
402 /* record length should not bigger than */
403 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
406 /* sanity check for fixed-records llog */
407 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
408 LASSERT(llh->llh_size != 0);
409 LASSERT(llh->llh_size == reclen);
412 rc = dt_attr_get(env, o, &lgi->lgi_attr);
417 * The modification case.
418 * If idx set then the record with that index must be modified.
419 * There are three cases possible:
420 * 1) the common case is the llog header update (idx == 0)
421 * 2) the llog record modification during llog process.
422 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
423 * In that case the \a loghandle::lgh_cur_offset
424 * 3) otherwise this is assumed that llog consist of records of
425 * fixed size, i.e. catalog. The llog header must has llh_size
426 * field equal to record size. The record offset is calculated
427 * just by /a idx value
429 * During modification we don't need extra header update because
430 * the bitmap and record count are not changed. The record header
431 * and tail remains the same too.
433 if (idx != LLOG_NEXT_IDX) {
434 /* llog can be empty only when first record is being written */
435 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
437 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
438 CERROR("%s: modify unset record %u\n",
439 o->do_lu.lo_dev->ld_obd->obd_name, idx);
443 if (idx != rec->lrh_index) {
444 CERROR("%s: modify index mismatch %d %u\n",
445 o->do_lu.lo_dev->ld_obd->obd_name, idx,
450 if (idx == LLOG_HEADER_IDX) {
451 /* llog header update */
452 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
456 /* If it does not indicate the bitmap index
457 * (reccookie == NULL), then it means update
458 * the whole update header. Otherwise only
459 * update header and bits needs to be updated,
460 * and in DNE cases, it will signaficantly
461 * shrink the RPC size.
462 * see distribute_txn_cancel_records()*/
463 if (reccookie == NULL) {
464 lgi->lgi_buf.lb_len = reclen;
465 lgi->lgi_buf.lb_buf = rec;
466 rc = dt_record_write(env, o, &lgi->lgi_buf,
471 /* update the header */
472 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
473 lgi->lgi_buf.lb_buf = llh;
474 rc = dt_record_write(env, o, &lgi->lgi_buf,
479 /* update the bitmap */
480 index = reccookie->lgc_index;
481 lgi->lgi_off = llh->llh_bitmap_offset +
482 (index / (sizeof(*bitmap) * 8)) *
484 lgi->lgi_buf.lb_len = sizeof(*bitmap);
485 lgi->lgi_buf.lb_buf =
486 &bitmap[index/(sizeof(*bitmap)*8)];
487 rc = dt_record_write(env, o, &lgi->lgi_buf,
491 } else if (loghandle->lgh_cur_idx > 0) {
493 * The lgh_cur_offset can be used only if index is
496 if (idx != loghandle->lgh_cur_idx) {
497 CERROR("%s: modify index mismatch %d %d\n",
498 o->do_lu.lo_dev->ld_obd->obd_name, idx,
499 loghandle->lgh_cur_idx);
503 lgi->lgi_off = loghandle->lgh_cur_offset;
504 CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
505 "len:%u offset %llu\n",
506 POSTID(&loghandle->lgh_id.lgl_oi), idx,
507 rec->lrh_len, (long long)lgi->lgi_off);
508 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
509 lgi->lgi_off = llh->llh_hdr.lrh_len +
512 /* This can be result of lgh_cur_idx is not set during
513 * llog processing or llh_size is not set to proper
514 * record size for fixed records llog. Therefore it is
515 * impossible to get record offset. */
516 CERROR("%s: can't get record offset, idx:%d, "
517 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
522 /* update only data, header and tail remain the same */
523 lgi->lgi_off += sizeof(struct llog_rec_hdr);
524 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
525 lgi->lgi_buf.lb_buf = REC_DATA(rec);
526 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
527 if (rc == 0 && reccookie) {
528 reccookie->lgc_lgl = loghandle->lgh_id;
529 reccookie->lgc_index = idx;
537 * The most common case of using llog. The new index is assigned to
538 * the new record, new bit is set in llog bitmap and llog count is
541 * Make sure that records don't cross a chunk boundary, so we can
542 * process them page-at-a-time if needed. If it will cross a chunk
543 * boundary, write in a fake (but referenced) entry to pad the chunk.
547 /* simulate ENOSPC when new plain llog is being added to the
549 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
550 llh->llh_flags & LLOG_F_IS_CAT)
553 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
554 lgi->lgi_off = lgi->lgi_attr.la_size;
555 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
556 /* NOTE: padding is a record, but no bit is set */
557 if (left != 0 && left != reclen &&
558 left < (reclen + LLOG_MIN_REC_SIZE)) {
559 index = loghandle->lgh_last_idx + 1;
560 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
563 loghandle->lgh_last_idx++; /* for pad rec */
565 /* if it's the last idx in log file, then return -ENOSPC
566 * or wrap around if a catalog */
567 if (llog_is_full(loghandle) ||
568 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
569 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
570 loghandle->lgh_last_idx >= cfs_fail_val)) {
571 if (llh->llh_flags & LLOG_F_IS_CAT)
572 loghandle->lgh_last_idx = 0;
577 /* increment the last_idx along with llh_tail index, they should
578 * be equal for a llog lifetime */
579 loghandle->lgh_last_idx++;
580 index = loghandle->lgh_last_idx;
581 LLOG_HDR_TAIL(llh)->lrt_index = index;
583 * NB: the caller should make sure only 1 process access
584 * the lgh_last_idx, e.g. append should be exclusive.
585 * Otherwise it might hit the assert.
587 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
588 rec->lrh_index = index;
590 lrt->lrt_len = rec->lrh_len;
591 lrt->lrt_index = rec->lrh_index;
593 /* the lgh_hdr_mutex protects llog header data from concurrent
594 * update/cancel, the llh_count and llh_bitmap are protected */
595 mutex_lock(&loghandle->lgh_hdr_mutex);
596 if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
597 CERROR("%s: index %u already set in log bitmap\n",
598 o->do_lu.lo_dev->ld_obd->obd_name, index);
599 mutex_unlock(&loghandle->lgh_hdr_mutex);
600 LBUG(); /* should never happen */
604 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
605 /* Update the minimum size of the llog record */
606 if (llh->llh_size == 0)
607 llh->llh_size = reclen;
608 else if (reclen < llh->llh_size)
609 llh->llh_size = reclen;
612 if (lgi->lgi_attr.la_size == 0) {
614 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
615 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
616 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
618 GOTO(out_unlock, rc);
620 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
622 /* Note: If this is not initialization (size == 0), then do not
623 * write the whole header (8k bytes), only update header/tail
624 * and bits needs to be updated. Because this update might be
625 * part of cross-MDT operation, which needs to write these
626 * updates into the update log(32KB limit) and also pack inside
627 * the RPC (1MB limit), if we write 8K for each operation, which
628 * will cost a lot space, and keep us adding more updates to one
631 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
632 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
633 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
635 GOTO(out_unlock, rc);
637 lgi->lgi_off = llh->llh_bitmap_offset +
638 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
639 lgi->lgi_buf.lb_len = sizeof(*bitmap);
640 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
641 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
643 GOTO(out_unlock, rc);
645 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
647 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
648 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
649 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
651 GOTO(out_unlock, rc);
655 /* unlock here for remote object */
656 mutex_unlock(&loghandle->lgh_hdr_mutex);
660 /* computed index can be used to determine offset for fixed-size
661 * records. This also allows to handle Catalog wrap around case */
662 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
663 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
665 rc = dt_attr_get(env, o, &lgi->lgi_attr);
669 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
670 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
674 lgi->lgi_buf.lb_len = reclen;
675 lgi->lgi_buf.lb_buf = rec;
676 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
680 CDEBUG(D_OTHER, "added record "DOSTID": idx: %u, %u off"LPU64"\n",
681 POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len,
683 if (reccookie != NULL) {
684 reccookie->lgc_lgl = loghandle->lgh_id;
685 reccookie->lgc_index = index;
686 if ((rec->lrh_type == MDS_UNLINK_REC) ||
687 (rec->lrh_type == MDS_SETATTR64_REC))
688 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
689 else if (rec->lrh_type == OST_SZ_REC)
690 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
692 reccookie->lgc_subsys = -1;
697 /* cleanup llog for error case */
698 mutex_lock(&loghandle->lgh_hdr_mutex);
699 ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
701 mutex_unlock(&loghandle->lgh_hdr_mutex);
703 /* restore llog last_idx */
704 if (--loghandle->lgh_last_idx == 0 &&
705 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
706 /* catalog had just wrap-around case */
707 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
709 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
715 * We can skip reading at least as many log blocks as the number of
716 * minimum sized log records we are skipping. If it turns out
717 * that we are not far enough along the log (because the
718 * actual records are larger than minimum size) we just skip
721 * Note: in llog_process_thread, it will use bitmap offset as
722 * the index to locate the record, which also includs some pad
723 * records, whose record size is very small, and it also does not
724 * consider pad record when recording minimum record size (otherwise
725 * min_record size might be too small), so in some rare cases,
726 * it might skip too much record for @goal, see llog_osd_next_block().
728 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
729 * as the min record size to skip over, usually because in the previous
730 * try, it skip too much record, see loog_osd_next(prev)_block().
732 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
733 int curr, int goal, __u32 chunk_size,
736 struct llog_log_hdr *llh = lgh->lgh_hdr;
738 /* Goal should not bigger than the record count */
739 if (goal > lgh->lgh_last_idx)
740 goal = lgh->lgh_last_idx;
743 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
744 *off = chunk_size + (goal - 1) * llh->llh_size;
746 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
748 if (llh->llh_size > 0 && !force_mini_rec)
749 min_rec_size = llh->llh_size;
751 *off = *off + (goal - curr - 1) * min_rec_size;
754 /* always align with lower chunk boundary*/
755 *off &= ~(chunk_size - 1);
759 * Remove optional fields that the client doesn't expect.
760 * This is typically in order to ensure compatibility with older clients.
761 * It is assumed that since we exclusively remove fields, the block will be
762 * big enough to handle the remapped records. It is also assumed that records
763 * of a block have the same format (i.e.: the same features enabled).
765 * \param[in,out] hdr Header of the block of records to remap.
766 * \param[in,out] last_hdr Last header, don't read past this point.
767 * \param[in] flags Flags describing the fields to keep.
769 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
770 struct llog_rec_hdr *last_hdr,
771 enum changelog_rec_flags flags)
773 if (hdr->lrh_type != CHANGELOG_REC)
777 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
779 changelog_remap_rec(rec, rec->cr_flags & flags);
780 hdr = llog_rec_hdr_next(hdr);
781 } while ((char *)hdr <= (char *)last_hdr);
785 * Implementation of the llog_operations::lop_next_block
787 * This function finds the the next llog block to return which contains
788 * record with required index. It is main part of llog processing.
790 * \param[in] env execution environment
791 * \param[in] loghandle llog handle of the current llog
792 * \param[in,out] cur_idx index preceeding cur_offset
793 * \param[in] next_idx target index to find
794 * \param[in,out] cur_offset furtherst point read in the file
795 * \param[in] buf pointer to data buffer to fill
796 * \param[in] len required len to read, it is
797 * usually llog chunk_size.
799 * \retval 0 on successful buffer read
800 * \retval negative value on error
802 static int llog_osd_next_block(const struct lu_env *env,
803 struct llog_handle *loghandle, int *cur_idx,
804 int next_idx, __u64 *cur_offset, void *buf,
807 struct llog_thread_info *lgi = llog_info(env);
809 struct dt_device *dt;
812 int last_idx = *cur_idx;
813 __u64 last_offset = *cur_offset;
814 bool force_mini_rec = false;
821 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
822 if (len == 0 || len & (chunk_size - 1))
825 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
826 next_idx, *cur_idx, *cur_offset);
829 LASSERT(loghandle->lgh_ctxt);
831 o = loghandle->lgh_obj;
833 LASSERT(dt_object_exists(o));
834 dt = lu2dt_dev(o->do_lu.lo_dev);
837 rc = dt_attr_get(env, o, &lgi->lgi_attr);
841 while (*cur_offset < lgi->lgi_attr.la_size) {
842 struct llog_rec_hdr *rec, *last_rec;
843 struct llog_rec_tail *tail;
845 llog_skip_over(loghandle, cur_offset, *cur_idx,
846 next_idx, chunk_size, force_mini_rec);
848 /* read up to next llog chunk_size block */
849 lgi->lgi_buf.lb_len = chunk_size -
850 (*cur_offset & (chunk_size - 1));
851 lgi->lgi_buf.lb_buf = buf;
853 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
855 if (rc == -EBADR && !force_mini_rec)
858 CERROR("%s: can't read llog block from log "DFID
859 " offset "LPU64": rc = %d\n",
860 o->do_lu.lo_dev->ld_obd->obd_name,
861 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
867 /* signal the end of the valid buffer to
869 memset(buf + rc, 0, len - rc);
872 if (rc == 0) { /* end of file, nothing to do */
878 if (rc < sizeof(*tail)) {
882 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
884 o->do_lu.lo_dev->ld_obd->obd_name,
885 POSTID(&loghandle->lgh_id.lgl_oi),
886 loghandle->lgh_id.lgl_ogen, *cur_offset);
887 GOTO(out, rc = -EINVAL);
891 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
892 lustre_swab_llog_rec(rec);
894 tail = (struct llog_rec_tail *)((char *)buf + rc -
895 sizeof(struct llog_rec_tail));
896 /* get the last record in block */
897 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
900 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
901 lustre_swab_llog_rec(last_rec);
902 LASSERT(last_rec->lrh_index == tail->lrt_index);
904 *cur_idx = tail->lrt_index;
906 /* this shouldn't happen */
907 if (tail->lrt_index == 0) {
908 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
909 "offset "LPU64" bytes %d\n",
910 o->do_lu.lo_dev->ld_obd->obd_name,
911 POSTID(&loghandle->lgh_id.lgl_oi),
912 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
913 GOTO(out, rc = -EINVAL);
915 if (tail->lrt_index < next_idx) {
917 last_offset = *cur_offset;
921 /* sanity check that the start of the new buffer is no farther
922 * than the record that we wanted. This shouldn't happen. */
923 if (rec->lrh_index > next_idx) {
924 if (!force_mini_rec && next_idx > last_idx)
927 CERROR("%s: missed desired record? %u > %u\n",
928 o->do_lu.lo_dev->ld_obd->obd_name,
929 rec->lrh_index, next_idx);
930 GOTO(out, rc = -ENOENT);
933 /* Trim unsupported extensions for compat w/ older clients */
934 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
935 changelog_block_trim_ext(rec, last_rec,
936 CLF_VERSION | CLF_RENAME);
941 /* Note: because there are some pad records in the
942 * llog, so llog_skip_over() might skip too much
943 * records, let's try skip again with minimum record */
944 force_mini_rec = true;
945 *cur_offset = last_offset;
948 GOTO(out, rc = -EIO);
954 * Implementation of the llog_operations::lop_prev_block
956 * This function finds the llog block to return which contains
957 * record with required index but in reverse order - from end of llog
959 * It is main part of reverse llog processing.
961 * \param[in] env execution environment
962 * \param[in] loghandle llog handle of the current llog
963 * \param[in] prev_idx target index to find
964 * \param[in] buf pointer to data buffer to fill
965 * \param[in] len required len to read, it is llog_chunk_size usually.
967 * \retval 0 on successful buffer read
968 * \retval negative value on error
970 static int llog_osd_prev_block(const struct lu_env *env,
971 struct llog_handle *loghandle,
972 int prev_idx, void *buf, int len)
974 struct llog_thread_info *lgi = llog_info(env);
976 struct dt_device *dt;
983 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
984 if (len == 0 || len & (chunk_size - 1))
987 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
990 LASSERT(loghandle->lgh_ctxt);
992 o = loghandle->lgh_obj;
994 LASSERT(dt_object_exists(o));
995 dt = lu2dt_dev(o->do_lu.lo_dev);
998 /* Let's only use mini record size for previous block read
1000 cur_offset = chunk_size;
1001 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1004 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1008 while (cur_offset < lgi->lgi_attr.la_size) {
1009 struct llog_rec_hdr *rec, *last_rec;
1010 struct llog_rec_tail *tail;
1012 lgi->lgi_buf.lb_len = len;
1013 lgi->lgi_buf.lb_buf = buf;
1014 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1016 CERROR("%s: can't read llog block from log "DFID
1017 " offset "LPU64": rc = %d\n",
1018 o->do_lu.lo_dev->ld_obd->obd_name,
1019 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1023 if (rc == 0) /* end of file, nothing to do */
1026 if (rc < sizeof(*tail)) {
1027 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
1029 o->do_lu.lo_dev->ld_obd->obd_name,
1030 POSTID(&loghandle->lgh_id.lgl_oi),
1031 loghandle->lgh_id.lgl_ogen, cur_offset);
1032 GOTO(out, rc = -EINVAL);
1036 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1037 lustre_swab_llog_rec(rec);
1039 tail = (struct llog_rec_tail *)((char *)buf + rc -
1040 sizeof(struct llog_rec_tail));
1041 /* get the last record in block */
1042 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1043 le32_to_cpu(tail->lrt_len));
1045 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1046 lustre_swab_llog_rec(last_rec);
1047 LASSERT(last_rec->lrh_index == tail->lrt_index);
1049 /* this shouldn't happen */
1050 if (tail->lrt_index == 0) {
1051 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
1053 o->do_lu.lo_dev->ld_obd->obd_name,
1054 POSTID(&loghandle->lgh_id.lgl_oi),
1055 loghandle->lgh_id.lgl_ogen, cur_offset);
1056 GOTO(out, rc = -EINVAL);
1058 if (tail->lrt_index < prev_idx)
1061 /* sanity check that the start of the new buffer is no farther
1062 * than the record that we wanted. This shouldn't happen. */
1063 if (rec->lrh_index > prev_idx) {
1064 CERROR("%s: missed desired record? %u > %u\n",
1065 o->do_lu.lo_dev->ld_obd->obd_name,
1066 rec->lrh_index, prev_idx);
1067 GOTO(out, rc = -ENOENT);
1070 /* Trim unsupported extensions for compat w/ older clients */
1071 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
1072 changelog_block_trim_ext(rec, last_rec,
1073 CLF_VERSION | CLF_RENAME);
1077 GOTO(out, rc = -EIO);
1083 * This is helper function to get llog directory object. It is used by named
1084 * llog operations to find/insert/delete llog entry from llog directory.
1086 * \param[in] env execution environment
1087 * \param[in] ctxt llog context
1089 * \retval dt_object of llog directory
1090 * \retval ERR_PTR of negative value on error
1092 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1093 struct llog_ctxt *ctxt)
1095 struct dt_device *dt;
1096 struct dt_thread_info *dti = dt_info(env);
1097 struct dt_object *dir;
1100 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1101 if (ctxt->loc_dir == NULL) {
1102 rc = dt_root_get(env, dt, &dti->dti_fid);
1105 dir = dt_locate(env, dt, &dti->dti_fid);
1107 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1108 lu_object_put(env, &dir->do_lu);
1109 return ERR_PTR(-ENOTDIR);
1112 lu_object_get(&ctxt->loc_dir->do_lu);
1113 dir = ctxt->loc_dir;
1120 * Implementation of the llog_operations::lop_open
1122 * This function opens the llog by its logid or by name, it may open also
1123 * non existent llog and assing then new id to it.
1124 * The llog_open/llog_close pair works similar to lu_object_find/put,
1125 * the object may not exist prior open. The result of open is just dt_object
1126 * in the llog header.
1128 * \param[in] env execution environment
1129 * \param[in] handle llog handle of the current llog
1130 * \param[in] logid logid of llog to open (nameless llog)
1131 * \param[in] name name of llog to open (named llog)
1132 * \param[in] open_param
1133 * LLOG_OPEN_NEW - new llog, may not exist
1134 * LLOG_OPEN_EXIST - old llog, must exist
1136 * \retval 0 on successful open, llog_handle::lgh_obj
1137 * contains the dt_object of the llog.
1138 * \retval negative value on error
1140 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1141 struct llog_logid *logid, char *name,
1142 enum llog_open_param open_param)
1144 struct llog_thread_info *lgi = llog_info(env);
1145 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1146 struct dt_object *o;
1147 struct dt_device *dt;
1148 struct ls_device *ls;
1149 struct local_oid_storage *los = NULL;
1156 LASSERT(ctxt->loc_exp);
1157 LASSERT(ctxt->loc_exp->exp_obd);
1158 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1160 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1161 struct lu_object_conf conf = { 0 };
1162 if (logid != NULL) {
1163 logid_to_fid(logid, &lgi->lgi_fid);
1165 /* If logid == NULL, then it means the caller needs
1166 * to allocate new FID (llog_cat_declare_add_rec()). */
1167 rc = obd_fid_alloc(env, ctxt->loc_exp,
1168 &lgi->lgi_fid, NULL);
1172 conf.loc_flags = LOC_F_NEW;
1175 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1176 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1183 ls = ls_device_get(dt);
1185 RETURN(PTR_ERR(ls));
1187 mutex_lock(&ls->ls_los_mutex);
1188 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1189 mutex_unlock(&ls->ls_los_mutex);
1191 ls_device_put(env, ls);
1195 if (logid != NULL) {
1196 logid_to_fid(logid, &lgi->lgi_fid);
1198 struct dt_object *llog_dir;
1200 llog_dir = llog_osd_dir_get(env, ctxt);
1201 if (IS_ERR(llog_dir))
1202 GOTO(out, rc = PTR_ERR(llog_dir));
1203 dt_read_lock(env, llog_dir, 0);
1204 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1205 dt_read_unlock(env, llog_dir);
1206 lu_object_put(env, &llog_dir->do_lu);
1207 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1208 /* generate fid for new llog */
1209 rc = local_object_fid_generate(env, los,
1214 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1215 if (handle->lgh_name)
1216 strcpy(handle->lgh_name, name);
1218 GOTO(out, rc = -ENOMEM);
1220 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1221 /* generate fid for new llog */
1222 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1227 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1229 GOTO(out_name, rc = PTR_ERR(o));
1232 /* No new llog is expected but doesn't exist */
1233 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
1234 GOTO(out_put, rc = -ENOENT);
1236 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1237 handle->lgh_obj = o;
1238 handle->private_data = los;
1239 LASSERT(handle->lgh_ctxt);
1244 lu_object_put(env, &o->do_lu);
1246 if (handle->lgh_name != NULL)
1247 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1255 * Get dir for regular fid log object
1257 * Get directory for regular fid log object, and these regular fid log
1258 * object will be inserted under this directory, to satisfy the FS
1259 * consistency check, e2fsck etc.
1261 * \param [in] env execution environment
1262 * \param [in] dto llog object
1264 * \retval pointer to the directory if it is found.
1265 * \retval ERR_PTR(negative errno) if it fails.
1267 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1268 struct dt_object *dto)
1270 struct llog_thread_info *lgi = llog_info(env);
1271 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1272 struct lu_seq_range *range = &lgi->lgi_range;
1273 struct lu_fid *dir_fid = &lgi->lgi_fid;
1274 struct dt_object *dir;
1278 fld_range_set_any(range);
1279 LASSERT(ss != NULL);
1280 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1281 fid_seq(lu_object_fid(&dto->do_lu)), range);
1283 RETURN(ERR_PTR(rc));
1285 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1286 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1290 if (!dt_try_as_dir(env, dir)) {
1291 lu_object_put(env, &dir->do_lu);
1292 RETURN(ERR_PTR(-ENOTDIR));
1299 * Add llog object with regular FID to name entry
1301 * Add llog object with regular FID to name space, and each llog
1302 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1303 * so to satisfy the namespace consistency check, e2fsck etc.
1305 * \param [in] env execution environment
1306 * \param [in] dto llog object
1307 * \param [in] th thandle
1308 * \param [in] declare if it is declare or execution
1310 * \retval 0 if insertion succeeds.
1311 * \retval negative errno if insertion fails.
1314 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1315 struct dt_object *dto,
1316 struct thandle *th, bool declare)
1318 struct llog_thread_info *lgi = llog_info(env);
1319 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1320 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1321 struct dt_object *dir;
1322 char *name = lgi->lgi_name;
1326 if (!fid_is_norm(fid))
1329 dir = llog_osd_get_regular_fid_dir(env, dto);
1331 RETURN(PTR_ERR(dir));
1334 rec->rec_type = S_IFREG;
1335 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1336 dt_write_lock(env, dir, 0);
1338 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1339 (struct dt_key *)name, th);
1341 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1342 (struct dt_key *)name, th, 1);
1344 dt_write_unlock(env, dir);
1346 lu_object_put(env, &dir->do_lu);
1352 * Implementation of the llog_operations::lop_declare_create
1354 * This function declares the llog create. It declares also name insert
1355 * into llog directory in case of named llog.
1357 * \param[in] env execution environment
1358 * \param[in] res llog handle of the current llog
1359 * \param[in] th current transaction handle
1361 * \retval 0 on successful create declaration
1362 * \retval negative value on error
1364 static int llog_osd_declare_create(const struct lu_env *env,
1365 struct llog_handle *res, struct thandle *th)
1367 struct llog_thread_info *lgi = llog_info(env);
1368 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1369 struct local_oid_storage *los;
1370 struct dt_object *o;
1375 LASSERT(res->lgh_obj);
1378 /* object can be created by another thread */
1380 if (dt_object_exists(o))
1383 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1384 struct llog_thread_info *lgi = llog_info(env);
1386 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1387 lgi->lgi_attr.la_size = 0;
1388 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1389 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1391 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1397 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1401 los = res->private_data;
1404 rc = llog_osd_declare_new_object(env, los, o, th);
1408 /* do not declare header initialization here as it's declared
1409 * in llog_osd_declare_write_rec() which is always called */
1411 if (res->lgh_name) {
1412 struct dt_object *llog_dir;
1414 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1415 if (IS_ERR(llog_dir))
1416 RETURN(PTR_ERR(llog_dir));
1417 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1418 rec->rec_fid = &lgi->lgi_fid;
1419 rec->rec_type = S_IFREG;
1420 rc = dt_declare_insert(env, llog_dir,
1421 (struct dt_rec *)rec,
1422 (struct dt_key *)res->lgh_name, th);
1423 lu_object_put(env, &llog_dir->do_lu);
1425 CERROR("%s: can't declare named llog %s: rc = %d\n",
1426 o->do_lu.lo_dev->ld_obd->obd_name,
1433 * Implementation of the llog_operations::lop_create
1435 * This function creates the llog according with llog_handle::lgh_obj
1436 * and llog_handle::lgh_name.
1438 * \param[in] env execution environment
1439 * \param[in] res llog handle of the current llog
1440 * \param[in] th current transaction handle
1442 * \retval 0 on successful create
1443 * \retval negative value on error
1445 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1448 struct llog_thread_info *lgi = llog_info(env);
1449 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1450 struct local_oid_storage *los;
1451 struct dt_object *o;
1460 /* llog can be already created */
1461 if (dt_object_exists(o))
1464 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1465 struct llog_thread_info *lgi = llog_info(env);
1467 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1468 lgi->lgi_attr.la_size = 0;
1469 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1470 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1472 dt_write_lock(env, o, 0);
1473 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1475 dt_write_unlock(env, o);
1479 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1484 los = res->private_data;
1487 dt_write_lock(env, o, 0);
1488 if (!dt_object_exists(o))
1489 rc = llog_osd_create_new_object(env, los, o, th);
1493 dt_write_unlock(env, o);
1497 if (res->lgh_name) {
1498 struct dt_object *llog_dir;
1500 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1501 if (IS_ERR(llog_dir))
1502 RETURN(PTR_ERR(llog_dir));
1504 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1505 rec->rec_fid = &lgi->lgi_fid;
1506 rec->rec_type = S_IFREG;
1507 dt_read_lock(env, llog_dir, 0);
1508 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1509 (struct dt_key *)res->lgh_name,
1511 dt_read_unlock(env, llog_dir);
1512 lu_object_put(env, &llog_dir->do_lu);
1514 CERROR("%s: can't create named llog %s: rc = %d\n",
1515 o->do_lu.lo_dev->ld_obd->obd_name,
1522 * Implementation of the llog_operations::lop_close
1524 * This function closes the llog. It just put llog object and referenced
1527 * \param[in] env execution environment
1528 * \param[in] handle llog handle of the current llog
1530 * \retval 0 on successful llog close
1531 * \retval negative value on error
1533 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1535 struct local_oid_storage *los;
1540 LASSERT(handle->lgh_obj);
1542 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1543 /* Remove the object from the cache, otherwise it may
1544 * hold LOD being released during cleanup process */
1545 lu_object_put_nocache(env, &handle->lgh_obj->do_lu);
1546 LASSERT(handle->private_data == NULL);
1549 lu_object_put(env, &handle->lgh_obj->do_lu);
1551 los = handle->private_data;
1555 if (handle->lgh_name)
1556 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1562 * delete llog object name entry
1564 * Delete llog object (with regular FID) from name space (under
1567 * \param [in] env execution environment
1568 * \param [in] dto llog object
1569 * \param [in] th thandle
1570 * \param [in] declare if it is declare or execution
1572 * \retval 0 if deletion succeeds.
1573 * \retval negative errno if deletion fails.
1576 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1577 struct dt_object *dto,
1578 struct thandle *th, bool declare)
1580 struct llog_thread_info *lgi = llog_info(env);
1581 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1582 struct dt_object *dir;
1583 char *name = lgi->lgi_name;
1587 if (!fid_is_norm(fid))
1590 dir = llog_osd_get_regular_fid_dir(env, dto);
1592 RETURN(PTR_ERR(dir));
1594 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1595 dt_write_lock(env, dir, 0);
1597 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1600 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1602 dt_write_unlock(env, dir);
1604 lu_object_put(env, &dir->do_lu);
1609 * Implementation of the llog_operations::lop_declare_destroy
1611 * This function declare destroys the llog and deletes also entry in the
1612 * llog directory in case of named llog. Llog should be opened prior that.
1614 * \param[in] env execution environment
1615 * \param[in] loghandle llog handle of the current llog
1617 * \retval 0 on successful destroy
1618 * \retval negative value on error
1620 static int llog_osd_declare_destroy(const struct lu_env *env,
1621 struct llog_handle *loghandle,
1624 struct llog_ctxt *ctxt;
1625 struct dt_object *o, *llog_dir = NULL;
1630 ctxt = loghandle->lgh_ctxt;
1633 o = loghandle->lgh_obj;
1636 if (loghandle->lgh_name) {
1637 llog_dir = llog_osd_dir_get(env, ctxt);
1638 if (IS_ERR(llog_dir))
1639 RETURN(PTR_ERR(llog_dir));
1641 rc = dt_declare_delete(env, llog_dir,
1642 (struct dt_key *)loghandle->lgh_name,
1648 rc = dt_declare_ref_del(env, o, th);
1652 rc = dt_declare_destroy(env, o, th);
1656 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1657 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1663 if (!(IS_ERR_OR_NULL(llog_dir)))
1664 lu_object_put(env, &llog_dir->do_lu);
1671 * Implementation of the llog_operations::lop_destroy
1673 * This function destroys the llog and deletes also entry in the
1674 * llog directory in case of named llog. Llog should be opened prior that.
1675 * Destroy method is not part of external transaction and does everything
1678 * \param[in] env execution environment
1679 * \param[in] loghandle llog handle of the current llog
1681 * \retval 0 on successful destroy
1682 * \retval negative value on error
1684 static int llog_osd_destroy(const struct lu_env *env,
1685 struct llog_handle *loghandle, struct thandle *th)
1687 struct llog_ctxt *ctxt;
1688 struct dt_object *o, *llog_dir = NULL;
1693 ctxt = loghandle->lgh_ctxt;
1694 LASSERT(ctxt != NULL);
1696 o = loghandle->lgh_obj;
1699 dt_write_lock(env, o, 0);
1700 if (!dt_object_exists(o))
1701 GOTO(out_unlock, rc = 0);
1703 if (loghandle->lgh_name) {
1704 llog_dir = llog_osd_dir_get(env, ctxt);
1705 if (IS_ERR(llog_dir))
1706 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1708 dt_read_lock(env, llog_dir, 0);
1709 rc = dt_delete(env, llog_dir,
1710 (struct dt_key *)loghandle->lgh_name,
1712 dt_read_unlock(env, llog_dir);
1714 CERROR("%s: can't remove llog %s: rc = %d\n",
1715 o->do_lu.lo_dev->ld_obd->obd_name,
1716 loghandle->lgh_name, rc);
1717 GOTO(out_unlock, rc);
1721 dt_ref_del(env, o, th);
1722 rc = dt_destroy(env, o, th);
1724 GOTO(out_unlock, rc);
1726 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1727 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1729 GOTO(out_unlock, rc);
1733 dt_write_unlock(env, o);
1734 if (!(IS_ERR_OR_NULL(llog_dir)))
1735 lu_object_put(env, &llog_dir->do_lu);
1740 * Implementation of the llog_operations::lop_setup
1742 * This function setup the llog on local storage.
1744 * \param[in] env execution environment
1745 * \param[in] obd obd device the llog belongs to
1746 * \param[in] olg the llog group, it is always zero group now.
1747 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1748 * Every new llog type have to use own index.
1749 * \param[in] disk_obd the storage obd, where llog is stored.
1751 * \retval 0 on successful llog setup
1752 * \retval negative value on error
1754 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1755 struct obd_llog_group *olg, int ctxt_idx,
1756 struct obd_device *disk_obd)
1758 struct llog_thread_info *lgi = llog_info(env);
1759 struct llog_ctxt *ctxt;
1764 LASSERT(olg->olg_ctxts[ctxt_idx]);
1766 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1769 if (disk_obd == NULL)
1772 /* initialize data allowing to generate new fids,
1773 * literally we need a sequece */
1774 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1775 lgi->lgi_fid.f_oid = 1;
1776 lgi->lgi_fid.f_ver = 0;
1777 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1779 &ctxt->loc_los_nameless);
1783 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1784 lgi->lgi_fid.f_oid = 1;
1785 lgi->lgi_fid.f_ver = 0;
1786 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1788 &ctxt->loc_los_named);
1790 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1791 ctxt->loc_los_nameless = NULL;
1797 llog_ctxt_put(ctxt);
1802 * Implementation of the llog_operations::lop_cleanup
1804 * This function cleanups the llog on local storage.
1806 * \param[in] env execution environment
1807 * \param[in] ctxt the llog context
1811 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1813 if (ctxt->loc_los_nameless != NULL) {
1814 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1815 ctxt->loc_los_nameless = NULL;
1818 if (ctxt->loc_los_named != NULL) {
1819 local_oid_storage_fini(env, ctxt->loc_los_named);
1820 ctxt->loc_los_named = NULL;
1826 struct llog_operations llog_osd_ops = {
1827 .lop_next_block = llog_osd_next_block,
1828 .lop_prev_block = llog_osd_prev_block,
1829 .lop_read_header = llog_osd_read_header,
1830 .lop_declare_destroy = llog_osd_declare_destroy,
1831 .lop_destroy = llog_osd_destroy,
1832 .lop_setup = llog_osd_setup,
1833 .lop_cleanup = llog_osd_cleanup,
1834 .lop_open = llog_osd_open,
1835 .lop_exist = llog_osd_exist,
1836 .lop_declare_create = llog_osd_declare_create,
1837 .lop_create = llog_osd_create,
1838 .lop_declare_write_rec = llog_osd_declare_write_rec,
1839 .lop_write_rec = llog_osd_write_rec,
1840 .lop_close = llog_osd_close,
1842 EXPORT_SYMBOL(llog_osd_ops);
1844 struct llog_operations llog_common_cat_ops = {
1845 .lop_next_block = llog_osd_next_block,
1846 .lop_prev_block = llog_osd_prev_block,
1847 .lop_read_header = llog_osd_read_header,
1848 .lop_declare_destroy = llog_osd_declare_destroy,
1849 .lop_destroy = llog_osd_destroy,
1850 .lop_setup = llog_osd_setup,
1851 .lop_cleanup = llog_osd_cleanup,
1852 .lop_open = llog_osd_open,
1853 .lop_exist = llog_osd_exist,
1854 .lop_declare_create = llog_osd_declare_create,
1855 .lop_create = llog_osd_create,
1856 .lop_declare_write_rec = llog_osd_declare_write_rec,
1857 .lop_write_rec = llog_osd_write_rec,
1858 .lop_close = llog_osd_close,
1859 .lop_add = llog_cat_add_rec,
1860 .lop_declare_add = llog_cat_declare_add_rec,
1862 EXPORT_SYMBOL(llog_common_cat_ops);
1865 * Read the special file which contains the list of llog catalogs IDs
1867 * This function reads the CATALOGS file which contains the array of llog
1868 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1869 * by OST/MDT number.
1871 * \param[in] env execution environment
1872 * \param[in] d corresponding storage device
1873 * \param[in] idx position to start from, usually OST/MDT index
1874 * \param[in] count how many catalog IDs to read
1875 * \param[out] idarray the buffer for the data. If it is NULL then
1876 * function returns just number of catalog IDs
1878 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1880 * \retval 0 on successful read of catalog IDs
1881 * \retval negative value on error
1882 * \retval positive value which is number of records in
1883 * the file if \a idarray is NULL
1885 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1886 int idx, int count, struct llog_catid *idarray,
1887 const struct lu_fid *fid)
1889 struct llog_thread_info *lgi = llog_info(env);
1890 struct dt_object *o = NULL;
1898 size = sizeof(*idarray) * count;
1899 lgi->lgi_off = idx * sizeof(*idarray);
1901 lgi->lgi_fid = *fid;
1902 o = dt_locate(env, d, &lgi->lgi_fid);
1906 if (!dt_object_exists(o)) {
1907 th = dt_trans_create(env, d);
1909 GOTO(out, rc = PTR_ERR(th));
1911 lgi->lgi_attr.la_valid = LA_MODE;
1912 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1913 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1915 th->th_wait_submit = 1;
1916 /* Make the llog object creation synchronization, so
1917 * it will be reliable to the reference, especially
1918 * for remote reference */
1921 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1924 GOTO(out_trans, rc);
1926 rc = dt_trans_start_local(env, d, th);
1928 GOTO(out_trans, rc);
1930 dt_write_lock(env, o, 0);
1931 if (!dt_object_exists(o))
1932 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1934 dt_write_unlock(env, o);
1936 dt_trans_stop(env, d, th);
1941 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1945 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1946 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1947 o->do_lu.lo_dev->ld_obd->obd_name,
1948 lgi->lgi_attr.la_mode);
1949 GOTO(out, rc = -ENOENT);
1952 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1953 (int)lgi->lgi_attr.la_size, size);
1955 /* return just number of llogs */
1956 if (idarray == NULL) {
1957 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1961 /* read for new ost index or for empty file */
1962 memset(idarray, 0, size);
1963 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1965 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1966 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1968 lgi->lgi_buf.lb_buf = idarray;
1969 lgi->lgi_buf.lb_len = size;
1970 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1971 /* -EFAULT means the llog is a sparse file. This is not an error
1972 * after arbitrary OST index is supported. */
1973 if (rc < 0 && rc != -EFAULT) {
1974 CERROR("%s: error reading CATALOGS: rc = %d\n",
1975 o->do_lu.lo_dev->ld_obd->obd_name, rc);
1981 lu_object_put(env, &o->do_lu);
1984 EXPORT_SYMBOL(llog_osd_get_cat_list);
1987 * Write the special file which contains the list of llog catalogs IDs
1989 * This function writes the CATALOG file which contains the array of llog
1990 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1993 * \param[in] env execution environment
1994 * \param[in] d corresponding storage device
1995 * \param[in] idx position to start from, usually OST/MDT index
1996 * \param[in] count how many catalog IDs to write
1997 * \param[out] idarray the buffer with the data to write.
1998 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2000 * \retval 0 on successful write of catalog IDs
2001 * \retval negative value on error
2003 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2004 int idx, int count, struct llog_catid *idarray,
2005 const struct lu_fid *fid)
2007 struct llog_thread_info *lgi = llog_info(env);
2008 struct dt_object *o = NULL;
2017 size = sizeof(*idarray) * count;
2018 lgi->lgi_off = idx * sizeof(*idarray);
2019 lgi->lgi_fid = *fid;
2021 o = dt_locate(env, d, &lgi->lgi_fid);
2025 if (!dt_object_exists(o))
2026 GOTO(out, rc = -ENOENT);
2028 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2032 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2033 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2034 o->do_lu.lo_dev->ld_obd->obd_name,
2035 lgi->lgi_attr.la_mode);
2036 GOTO(out, rc = -ENOENT);
2039 th = dt_trans_create(env, d);
2041 GOTO(out, rc = PTR_ERR(th));
2043 lgi->lgi_buf.lb_len = size;
2044 lgi->lgi_buf.lb_buf = idarray;
2045 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2047 GOTO(out_trans, rc);
2049 /* For update log, this happens during initialization,
2050 * see lod_sub_prep_llog(), and we need make sure catlog
2051 * file ID is written to catlist file(committed) before
2052 * cross-MDT operation write update records to catlog FILE,
2053 * otherwise, during failover these update records might
2055 if (fid_is_update_log(fid))
2058 rc = dt_trans_start_local(env, d, th);
2060 GOTO(out_trans, rc);
2062 th->th_wait_submit = 1;
2064 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2066 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2069 dt_trans_stop(env, d, th);
2071 lu_object_put(env, &o->do_lu);
2074 EXPORT_SYMBOL(llog_osd_put_cat_list);