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, 2017, 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);
131 static void *rec_tail(struct llog_rec_hdr *rec)
133 return (void *)((char *)rec + rec->lrh_len -
134 sizeof(struct llog_rec_tail));
138 * Write a padding record to the llog
140 * This function writes a padding record to the end of llog. That may
141 * be needed if llog contains records of variable size, e.g. config logs
143 * The padding record just aligns llog to the llog chunk_size boundary if
144 * the current record doesn't fit in the remaining space.
146 * It allocates full length to avoid two separate writes for header and tail.
147 * Such 2-steps scheme needs extra protection and complex error handling.
149 * \param[in] env execution environment
150 * \param[in] o dt_object to create
151 * \param[in,out] off pointer to the padding start offset
152 * \param[in] len padding length
153 * \param[in] index index of the padding record in a llog
154 * \param[in] th current transaction handle
156 * \retval 0 on successful padding write
157 * \retval negative error if write failed
159 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
160 loff_t *off, int len, int index, struct thandle *th)
162 struct llog_thread_info *lgi = llog_info(env);
163 struct llog_rec_hdr *rec;
164 struct llog_rec_tail *tail;
171 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
178 rec->lrh_index = index;
179 rec->lrh_type = LLOG_PAD_MAGIC;
181 tail = rec_tail(rec);
183 tail->lrt_index = index;
185 lgi->lgi_buf.lb_buf = rec;
186 lgi->lgi_buf.lb_len = len;
187 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
189 CERROR("%s: error writing padding record: rc = %d\n",
190 o->do_lu.lo_dev->ld_obd->obd_name, rc);
197 * Implementation of the llog_operations::lop_read_header
199 * This function reads the current llog header from the bottom storage
202 * \param[in] env execution environment
203 * \param[in] handle llog handle of the current llog
205 * \retval 0 on successful header read
206 * \retval negative error if read failed
208 static int llog_osd_read_header(const struct lu_env *env,
209 struct llog_handle *handle)
211 struct llog_rec_hdr *llh_hdr;
213 struct llog_thread_info *lgi;
214 enum llog_flag flags;
222 lgi = llog_info(env);
224 rc = dt_attr_get(env, o, &lgi->lgi_attr);
228 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
230 if (lgi->lgi_attr.la_size == 0) {
231 CDEBUG(D_HA, "not reading header from 0-byte log\n");
235 flags = handle->lgh_hdr->llh_flags;
238 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
239 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
240 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
241 llh_hdr = &handle->lgh_hdr->llh_hdr;
242 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
243 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
244 o->do_lu.lo_dev->ld_obd->obd_name,
245 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
254 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
255 lustre_swab_llog_hdr(handle->lgh_hdr);
257 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
258 CERROR("%s: bad log %s "DFID" header magic: %#x "
259 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
260 handle->lgh_name ? handle->lgh_name : "",
261 PFID(lu_object_fid(&o->do_lu)),
262 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
264 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
265 llh_hdr->lrh_len > handle->lgh_hdr_size) {
266 CERROR("%s: incorrectly sized log %s "DFID" header: "
267 "%#x (expected at least %#x)\n"
268 "you may need to re-run lconf --write_conf.\n",
269 o->do_lu.lo_dev->ld_obd->obd_name,
270 handle->lgh_name ? handle->lgh_name : "",
271 PFID(lu_object_fid(&o->do_lu)),
272 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
274 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
275 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
276 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
278 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
280 o->do_lu.lo_dev->ld_obd->obd_name,
281 handle->lgh_name ? handle->lgh_name : "",
282 PFID(lu_object_fid(&o->do_lu)),
283 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
287 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
288 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
294 * Implementation of the llog_operations::lop_declare_write
296 * This function declares the new record write.
298 * \param[in] env execution environment
299 * \param[in] loghandle llog handle of the current llog
300 * \param[in] rec llog record header. This is a real header of the full
301 * llog record to write. This is the beginning of buffer
302 * to write, the length of buffer is stored in
304 * \param[in] idx index of the llog record. If \a idx == -1 then this is
305 * append case, otherwise \a idx is the index of record
307 * \param[in] th current transaction handle
309 * \retval 0 on successful declaration
310 * \retval negative error if declaration failed
312 static int llog_osd_declare_write_rec(const struct lu_env *env,
313 struct llog_handle *loghandle,
314 struct llog_rec_hdr *rec,
315 int idx, struct thandle *th)
317 struct llog_thread_info *lgi = llog_info(env);
328 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
330 o = loghandle->lgh_obj;
333 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
334 lgi->lgi_buf.lb_len = chunk_size;
335 lgi->lgi_buf.lb_buf = NULL;
336 /* each time we update header */
337 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
339 if (rc || idx == 0) /* if error or just header */
343 * the pad record can be inserted so take into account double
346 lgi->lgi_buf.lb_len = chunk_size * 2;
347 lgi->lgi_buf.lb_buf = NULL;
348 /* XXX: implement declared window or multi-chunks approach */
349 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
355 * Implementation of the llog_operations::lop_write
357 * This function writes the new record in the llog or modify the existed one.
359 * \param[in] env execution environment
360 * \param[in] loghandle llog handle of the current llog
361 * \param[in] rec llog record header. This is a real header of
362 * the full llog record to write. This is
363 * the beginning of buffer to write, the length
364 * of buffer is stored in \a rec::lrh_len
365 * \param[in,out] reccookie pointer to the cookie to return back if needed.
366 * It is used for further cancel of this llog
368 * \param[in] idx index of the llog record. If \a idx == -1 then
369 * this is append case, otherwise \a idx is
370 * the index of record to modify
371 * \param[in] th current transaction handle
373 * \retval 0 on successful write && \a reccookie == NULL
374 * 1 on successful write && \a reccookie != NULL
375 * \retval negative error if write failed
377 static int llog_osd_write_rec(const struct lu_env *env,
378 struct llog_handle *loghandle,
379 struct llog_rec_hdr *rec,
380 struct llog_cookie *reccookie,
381 int idx, struct thandle *th)
383 struct llog_thread_info *lgi = llog_info(env);
384 struct llog_log_hdr *llh;
385 int reclen = rec->lrh_len;
387 struct llog_rec_tail *lrt;
394 llh = loghandle->lgh_hdr;
395 o = loghandle->lgh_obj;
397 chunk_size = llh->llh_hdr.lrh_len;
398 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
399 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
401 if (!llog_osd_exist(loghandle))
404 /* record length should not bigger than */
405 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
408 /* sanity check for fixed-records llog */
409 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
410 LASSERT(llh->llh_size != 0);
411 LASSERT(llh->llh_size == reclen);
414 rc = dt_attr_get(env, o, &lgi->lgi_attr);
419 * The modification case.
420 * If idx set then the record with that index must be modified.
421 * There are three cases possible:
422 * 1) the common case is the llog header update (idx == 0)
423 * 2) the llog record modification during llog process.
424 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
425 * In that case the \a loghandle::lgh_cur_offset
426 * 3) otherwise this is assumed that llog consist of records of
427 * fixed size, i.e. catalog. The llog header must has llh_size
428 * field equal to record size. The record offset is calculated
429 * just by /a idx value
431 * During modification we don't need extra header update because
432 * the bitmap and record count are not changed. The record header
433 * and tail remains the same too.
435 if (idx != LLOG_NEXT_IDX) {
436 /* llog can be empty only when first record is being written */
437 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
439 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
440 CERROR("%s: modify unset record %u\n",
441 o->do_lu.lo_dev->ld_obd->obd_name, idx);
445 if (idx != rec->lrh_index) {
446 CERROR("%s: modify index mismatch %d %u\n",
447 o->do_lu.lo_dev->ld_obd->obd_name, idx,
452 if (idx == LLOG_HEADER_IDX) {
453 /* llog header update */
454 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
458 /* If it does not indicate the bitmap index
459 * (reccookie == NULL), then it means update
460 * the whole update header. Otherwise only
461 * update header and bits needs to be updated,
462 * and in DNE cases, it will signaficantly
463 * shrink the RPC size.
464 * see distribute_txn_cancel_records()*/
465 if (reccookie == NULL) {
466 lgi->lgi_buf.lb_len = reclen;
467 lgi->lgi_buf.lb_buf = rec;
468 rc = dt_record_write(env, o, &lgi->lgi_buf,
473 /* update the header */
474 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
475 lgi->lgi_buf.lb_buf = llh;
476 rc = dt_record_write(env, o, &lgi->lgi_buf,
481 /* update the bitmap */
482 index = reccookie->lgc_index;
483 lgi->lgi_off = llh->llh_bitmap_offset +
484 (index / (sizeof(*bitmap) * 8)) *
486 lgi->lgi_buf.lb_len = sizeof(*bitmap);
487 lgi->lgi_buf.lb_buf =
488 &bitmap[index/(sizeof(*bitmap)*8)];
489 rc = dt_record_write(env, o, &lgi->lgi_buf,
493 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
494 lgi->lgi_off = llh->llh_hdr.lrh_len +
496 } else if (reccookie != NULL && reccookie->lgc_index > 0) {
498 * The lgc_offset can be used only if index is
501 if (idx != reccookie->lgc_index) {
502 CERROR("%s: modify index mismatch %d %d\n",
503 o->do_lu.lo_dev->ld_obd->obd_name, idx,
504 reccookie->lgc_index);
508 lgi->lgi_off = reccookie->lgc_offset;
509 CDEBUG(D_OTHER, "modify record "DFID": idx:%u, "
510 "len:%u offset %llu\n",
511 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), idx,
512 rec->lrh_len, (long long)lgi->lgi_off);
514 /* This can be result of lgh_cur_idx is not set during
515 * llog processing or llh_size is not set to proper
516 * record size for fixed records llog. Therefore it is
517 * impossible to get record offset. */
518 CERROR("%s: can't get record offset, idx:%d, "
519 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
524 /* update only data, header and tail remain the same */
525 lgi->lgi_off += sizeof(struct llog_rec_hdr);
526 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
527 lgi->lgi_buf.lb_buf = REC_DATA(rec);
528 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
529 if (rc == 0 && reccookie) {
530 reccookie->lgc_lgl = loghandle->lgh_id;
531 reccookie->lgc_index = idx;
539 * The most common case of using llog. The new index is assigned to
540 * the new record, new bit is set in llog bitmap and llog count is
543 * Make sure that records don't cross a chunk boundary, so we can
544 * process them page-at-a-time if needed. If it will cross a chunk
545 * boundary, write in a fake (but referenced) entry to pad the chunk.
549 /* simulate ENOSPC when new plain llog is being added to the
551 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
552 llh->llh_flags & LLOG_F_IS_CAT)
555 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
556 orig_last_idx = loghandle->lgh_last_idx;
557 lgi->lgi_off = lgi->lgi_attr.la_size;
559 if (loghandle->lgh_max_size > 0 &&
560 lgi->lgi_off >= loghandle->lgh_max_size) {
561 CDEBUG(D_OTHER, "llog is getting too large (%u > %u) at %u "
562 DFID"\n", (unsigned)lgi->lgi_off,
563 loghandle->lgh_max_size, (int)loghandle->lgh_last_idx,
564 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
565 /* this is to signal that this llog is full */
566 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
570 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
571 /* NOTE: padding is a record, but no bit is set */
572 if (left != 0 && left != reclen &&
573 left < (reclen + LLOG_MIN_REC_SIZE)) {
574 index = loghandle->lgh_last_idx + 1;
575 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
579 loghandle->lgh_last_idx++; /* for pad rec */
581 /* if it's the last idx in log file, then return -ENOSPC
582 * or wrap around if a catalog */
583 if (llog_is_full(loghandle) ||
584 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
585 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
586 loghandle->lgh_last_idx >= cfs_fail_val)) {
587 if (llh->llh_flags & LLOG_F_IS_CAT)
588 loghandle->lgh_last_idx = 0;
593 /* increment the last_idx along with llh_tail index, they should
594 * be equal for a llog lifetime */
595 loghandle->lgh_last_idx++;
596 index = loghandle->lgh_last_idx;
597 LLOG_HDR_TAIL(llh)->lrt_index = index;
599 * NB: the caller should make sure only 1 process access
600 * the lgh_last_idx, e.g. append should be exclusive.
601 * Otherwise it might hit the assert.
603 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
604 rec->lrh_index = index;
606 lrt->lrt_len = rec->lrh_len;
607 lrt->lrt_index = rec->lrh_index;
609 /* the lgh_hdr_mutex protects llog header data from concurrent
610 * update/cancel, the llh_count and llh_bitmap are protected */
611 mutex_lock(&loghandle->lgh_hdr_mutex);
612 if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
613 CERROR("%s: index %u already set in log bitmap\n",
614 o->do_lu.lo_dev->ld_obd->obd_name, index);
615 mutex_unlock(&loghandle->lgh_hdr_mutex);
616 LBUG(); /* should never happen */
620 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
621 /* Update the minimum size of the llog record */
622 if (llh->llh_size == 0)
623 llh->llh_size = reclen;
624 else if (reclen < llh->llh_size)
625 llh->llh_size = reclen;
628 if (lgi->lgi_attr.la_size == 0) {
630 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
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 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
638 /* Note: If this is not initialization (size == 0), then do not
639 * write the whole header (8k bytes), only update header/tail
640 * and bits needs to be updated. Because this update might be
641 * part of cross-MDT operation, which needs to write these
642 * updates into the update log(32KB limit) and also pack inside
643 * the RPC (1MB limit), if we write 8K for each operation, which
644 * will cost a lot space, and keep us adding more updates to one
647 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
648 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
649 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
651 GOTO(out_unlock, rc);
653 lgi->lgi_off = llh->llh_bitmap_offset +
654 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
655 lgi->lgi_buf.lb_len = sizeof(*bitmap);
656 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
657 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
659 GOTO(out_unlock, rc);
661 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
663 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
664 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
665 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
667 GOTO(out_unlock, rc);
671 /* unlock here for remote object */
672 mutex_unlock(&loghandle->lgh_hdr_mutex);
676 /* computed index can be used to determine offset for fixed-size
677 * records. This also allows to handle Catalog wrap around case */
678 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
679 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
681 rc = dt_attr_get(env, o, &lgi->lgi_attr);
685 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
686 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
690 lgi->lgi_buf.lb_len = reclen;
691 lgi->lgi_buf.lb_buf = rec;
692 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
696 CDEBUG(D_HA, "added record "DFID".%u, %u off%llu\n",
697 PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
699 if (reccookie != NULL) {
700 reccookie->lgc_lgl = loghandle->lgh_id;
701 reccookie->lgc_index = index;
702 if ((rec->lrh_type == MDS_UNLINK_REC) ||
703 (rec->lrh_type == MDS_SETATTR64_REC))
704 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
705 else if (rec->lrh_type == OST_SZ_REC)
706 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
708 reccookie->lgc_subsys = -1;
713 /* cleanup llog for error case */
714 mutex_lock(&loghandle->lgh_hdr_mutex);
715 ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
717 mutex_unlock(&loghandle->lgh_hdr_mutex);
719 /* restore llog last_idx */
720 if (dt_object_remote(o)) {
721 loghandle->lgh_last_idx = orig_last_idx;
722 } else if (--loghandle->lgh_last_idx == 0 &&
723 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
724 /* catalog had just wrap-around case */
725 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
728 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
734 * We can skip reading at least as many log blocks as the number of
735 * minimum sized log records we are skipping. If it turns out
736 * that we are not far enough along the log (because the
737 * actual records are larger than minimum size) we just skip
740 * Note: in llog_process_thread, it will use bitmap offset as
741 * the index to locate the record, which also includs some pad
742 * records, whose record size is very small, and it also does not
743 * consider pad record when recording minimum record size (otherwise
744 * min_record size might be too small), so in some rare cases,
745 * it might skip too much record for @goal, see llog_osd_next_block().
747 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
748 * as the min record size to skip over, usually because in the previous
749 * try, it skip too much record, see loog_osd_next(prev)_block().
751 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
752 int curr, int goal, __u32 chunk_size,
755 struct llog_log_hdr *llh = lgh->lgh_hdr;
757 /* Goal should not bigger than the record count */
758 if (goal > lgh->lgh_last_idx)
759 goal = lgh->lgh_last_idx;
762 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
763 *off = chunk_size + (goal - 1) * llh->llh_size;
765 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
767 if (llh->llh_size > 0 && !force_mini_rec)
768 min_rec_size = llh->llh_size;
770 *off = *off + (goal - curr - 1) * min_rec_size;
773 /* always align with lower chunk boundary*/
774 *off &= ~(chunk_size - 1);
778 * Remove optional fields that the client doesn't expect.
779 * This is typically in order to ensure compatibility with older clients.
780 * It is assumed that since we exclusively remove fields, the block will be
781 * big enough to handle the remapped records. It is also assumed that records
782 * of a block have the same format (i.e.: the same features enabled).
784 * \param[in,out] hdr Header of the block of records to remap.
785 * \param[in,out] last_hdr Last header, don't read past this point.
786 * \param[in] flags Flags describing the fields to keep.
787 * \param[in] extra_flags Flags describing the extra fields to keep.
789 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
790 struct llog_rec_hdr *last_hdr,
791 struct llog_handle *loghandle)
793 enum changelog_rec_flags flags = CLF_SUPPORTED;
794 enum changelog_rec_extra_flags extra_flags = CLFE_SUPPORTED;
796 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_XATTR))
797 extra_flags &= ~CLFE_XATTR;
798 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_OMODE))
799 extra_flags &= ~CLFE_OPEN;
800 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_NID))
801 extra_flags &= ~CLFE_NID;
802 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_UIDGID))
803 extra_flags &= ~CLFE_UIDGID;
804 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_EXTRA_FLAGS))
805 flags &= ~CLF_EXTRA_FLAGS;
806 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
809 if (flags == CLF_SUPPORTED && extra_flags == CLFE_SUPPORTED)
812 if (hdr->lrh_type != CHANGELOG_REC)
816 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
817 enum changelog_rec_extra_flags xflag = CLFE_INVALID;
819 if (flags & CLF_EXTRA_FLAGS &&
820 rec->cr_flags & CLF_EXTRA_FLAGS) {
821 xflag = changelog_rec_extra_flags(rec)->cr_extra_flags &
825 if (unlikely(hdr->lrh_len == 0)) {
826 /* It is corruption case, we cannot know the next rec,
827 * jump to the last one directly to avoid dead loop. */
828 LCONSOLE(D_WARNING, "Hit invalid llog record: "
829 "idx %u, type %u, id %u\n",
830 hdr->lrh_index, hdr->lrh_type, hdr->lrh_id);
831 hdr = llog_rec_hdr_next(last_hdr);
832 if (unlikely(hdr == last_hdr))
833 LCONSOLE(D_WARNING, "The last record crashed: "
834 "idx %u, type %u, id %u\n",
835 hdr->lrh_index, hdr->lrh_type,
840 changelog_remap_rec(rec, rec->cr_flags & flags, xflag);
841 hdr = llog_rec_hdr_next(hdr);
842 /* Yield CPU to avoid soft-lockup if there are too many records
845 } while ((char *)hdr <= (char *)last_hdr);
849 * Implementation of the llog_operations::lop_next_block
851 * This function finds the the next llog block to return which contains
852 * record with required index. It is main part of llog processing.
854 * \param[in] env execution environment
855 * \param[in] loghandle llog handle of the current llog
856 * \param[in,out] cur_idx index preceeding cur_offset
857 * \param[in] next_idx target index to find
858 * \param[in,out] cur_offset furtherst point read in the file
859 * \param[in] buf pointer to data buffer to fill
860 * \param[in] len required len to read, it is
861 * usually llog chunk_size.
863 * \retval 0 on successful buffer read
864 * \retval negative value on error
866 static int llog_osd_next_block(const struct lu_env *env,
867 struct llog_handle *loghandle, int *cur_idx,
868 int next_idx, __u64 *cur_offset, void *buf,
871 struct llog_thread_info *lgi = llog_info(env);
873 struct dt_device *dt;
876 int last_idx = *cur_idx;
877 __u64 last_offset = *cur_offset;
878 bool force_mini_rec = false;
885 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
886 if (len == 0 || len & (chunk_size - 1))
890 LASSERT(loghandle->lgh_ctxt);
892 o = loghandle->lgh_obj;
894 LASSERT(dt_object_exists(o));
895 dt = lu2dt_dev(o->do_lu.lo_dev);
898 rc = dt_attr_get(env, o, &lgi->lgi_attr);
902 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off"
903 "%llu), size %llu\n", next_idx, *cur_idx,
904 *cur_offset, lgi->lgi_attr.la_size);
906 while (*cur_offset < lgi->lgi_attr.la_size) {
907 struct llog_rec_hdr *rec, *last_rec;
908 struct llog_rec_tail *tail;
910 llog_skip_over(loghandle, cur_offset, *cur_idx,
911 next_idx, chunk_size, force_mini_rec);
913 /* read up to next llog chunk_size block */
914 lgi->lgi_buf.lb_len = chunk_size -
915 (*cur_offset & (chunk_size - 1));
916 lgi->lgi_buf.lb_buf = buf;
918 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
920 if (rc == -EBADR && !force_mini_rec)
923 CERROR("%s: can't read llog block from log "DFID
924 " offset %llu: rc = %d\n",
925 o->do_lu.lo_dev->ld_obd->obd_name,
926 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
932 /* signal the end of the valid buffer to
934 memset(buf + rc, 0, len - rc);
937 if (rc == 0) { /* end of file, nothing to do */
943 if (rc < sizeof(*tail)) {
947 CERROR("%s: invalid llog block at log id "DFID":%x "
949 o->do_lu.lo_dev->ld_obd->obd_name,
950 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
951 loghandle->lgh_id.lgl_ogen, *cur_offset);
952 GOTO(out, rc = -EINVAL);
956 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
957 lustre_swab_llog_rec(rec);
959 tail = (struct llog_rec_tail *)((char *)buf + rc -
960 sizeof(struct llog_rec_tail));
961 /* get the last record in block */
962 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
965 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
966 lustre_swab_llog_rec(last_rec);
968 if (last_rec->lrh_index != tail->lrt_index) {
969 CERROR("%s: invalid llog tail at log id "DFID":%x "
970 "offset %llu last_rec idx %u tail idx %u"
971 "lrt len %u read_size %d\n",
972 o->do_lu.lo_dev->ld_obd->obd_name,
973 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
974 loghandle->lgh_id.lgl_ogen, *cur_offset,
975 last_rec->lrh_index, tail->lrt_index,
977 GOTO(out, rc = -EINVAL);
980 *cur_idx = tail->lrt_index;
982 /* this shouldn't happen */
983 if (tail->lrt_index == 0) {
984 CERROR("%s: invalid llog tail at log id "DFID":%x "
985 "offset %llu bytes %d\n",
986 o->do_lu.lo_dev->ld_obd->obd_name,
987 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
988 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
989 GOTO(out, rc = -EINVAL);
991 if (tail->lrt_index < next_idx) {
993 last_offset = *cur_offset;
997 /* sanity check that the start of the new buffer is no farther
998 * than the record that we wanted. This shouldn't happen. */
999 if (rec->lrh_index > next_idx) {
1000 if (!force_mini_rec && next_idx > last_idx)
1003 CERROR("%s: missed desired record? %u > %u\n",
1004 o->do_lu.lo_dev->ld_obd->obd_name,
1005 rec->lrh_index, next_idx);
1006 GOTO(out, rc = -ENOENT);
1009 /* Trim unsupported extensions for compat w/ older clients */
1010 changelog_block_trim_ext(rec, last_rec, loghandle);
1015 /* Note: because there are some pad records in the
1016 * llog, so llog_skip_over() might skip too much
1017 * records, let's try skip again with minimum record */
1018 force_mini_rec = true;
1019 *cur_offset = last_offset;
1020 *cur_idx = last_idx;
1022 GOTO(out, rc = -EIO);
1028 * Implementation of the llog_operations::lop_prev_block
1030 * This function finds the llog block to return which contains
1031 * record with required index but in reverse order - from end of llog
1033 * It is main part of reverse llog processing.
1035 * \param[in] env execution environment
1036 * \param[in] loghandle llog handle of the current llog
1037 * \param[in] prev_idx target index to find
1038 * \param[in] buf pointer to data buffer to fill
1039 * \param[in] len required len to read, it is llog_chunk_size usually.
1041 * \retval 0 on successful buffer read
1042 * \retval negative value on error
1044 static int llog_osd_prev_block(const struct lu_env *env,
1045 struct llog_handle *loghandle,
1046 int prev_idx, void *buf, int len)
1048 struct llog_thread_info *lgi = llog_info(env);
1049 struct dt_object *o;
1050 struct dt_device *dt;
1057 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1058 if (len == 0 || len & (chunk_size - 1))
1061 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1064 LASSERT(loghandle->lgh_ctxt);
1066 o = loghandle->lgh_obj;
1068 LASSERT(dt_object_exists(o));
1069 dt = lu2dt_dev(o->do_lu.lo_dev);
1072 /* Let's only use mini record size for previous block read
1074 cur_offset = chunk_size;
1075 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1078 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1082 while (cur_offset < lgi->lgi_attr.la_size) {
1083 struct llog_rec_hdr *rec, *last_rec;
1084 struct llog_rec_tail *tail;
1086 lgi->lgi_buf.lb_len = len;
1087 lgi->lgi_buf.lb_buf = buf;
1088 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1090 CERROR("%s: can't read llog block from log "DFID
1091 " offset %llu: rc = %d\n",
1092 o->do_lu.lo_dev->ld_obd->obd_name,
1093 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1097 if (rc == 0) /* end of file, nothing to do */
1100 if (rc < sizeof(*tail)) {
1101 CERROR("%s: invalid llog block at log id "DFID":%x "
1103 o->do_lu.lo_dev->ld_obd->obd_name,
1104 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1105 loghandle->lgh_id.lgl_ogen, cur_offset);
1106 GOTO(out, rc = -EINVAL);
1110 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1111 lustre_swab_llog_rec(rec);
1113 tail = (struct llog_rec_tail *)((char *)buf + rc -
1114 sizeof(struct llog_rec_tail));
1115 /* get the last record in block */
1116 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1117 le32_to_cpu(tail->lrt_len));
1119 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1120 lustre_swab_llog_rec(last_rec);
1121 LASSERT(last_rec->lrh_index == tail->lrt_index);
1123 /* this shouldn't happen */
1124 if (tail->lrt_index == 0) {
1125 CERROR("%s: invalid llog tail at log id "DFID":%x "
1127 o->do_lu.lo_dev->ld_obd->obd_name,
1128 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1129 loghandle->lgh_id.lgl_ogen, cur_offset);
1130 GOTO(out, rc = -EINVAL);
1132 if (tail->lrt_index < prev_idx)
1135 /* sanity check that the start of the new buffer is no farther
1136 * than the record that we wanted. This shouldn't happen. */
1137 if (rec->lrh_index > prev_idx) {
1138 CERROR("%s: missed desired record? %u > %u\n",
1139 o->do_lu.lo_dev->ld_obd->obd_name,
1140 rec->lrh_index, prev_idx);
1141 GOTO(out, rc = -ENOENT);
1144 /* Trim unsupported extensions for compat w/ older clients */
1145 changelog_block_trim_ext(rec, last_rec, loghandle);
1149 GOTO(out, rc = -EIO);
1155 * This is helper function to get llog directory object. It is used by named
1156 * llog operations to find/insert/delete llog entry from llog directory.
1158 * \param[in] env execution environment
1159 * \param[in] ctxt llog context
1161 * \retval dt_object of llog directory
1162 * \retval ERR_PTR of negative value on error
1164 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1165 struct llog_ctxt *ctxt)
1167 struct dt_device *dt;
1168 struct dt_thread_info *dti = dt_info(env);
1169 struct dt_object *dir;
1172 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1173 if (ctxt->loc_dir == NULL) {
1174 rc = dt_root_get(env, dt, &dti->dti_fid);
1177 dir = dt_locate(env, dt, &dti->dti_fid);
1179 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1180 dt_object_put(env, dir);
1181 return ERR_PTR(-ENOTDIR);
1184 lu_object_get(&ctxt->loc_dir->do_lu);
1185 dir = ctxt->loc_dir;
1192 * Implementation of the llog_operations::lop_open
1194 * This function opens the llog by its logid or by name, it may open also
1195 * non existent llog and assing then new id to it.
1196 * The llog_open/llog_close pair works similar to lu_object_find/put,
1197 * the object may not exist prior open. The result of open is just dt_object
1198 * in the llog header.
1200 * \param[in] env execution environment
1201 * \param[in] handle llog handle of the current llog
1202 * \param[in] logid logid of llog to open (nameless llog)
1203 * \param[in] name name of llog to open (named llog)
1204 * \param[in] open_param
1205 * LLOG_OPEN_NEW - new llog, may not exist
1206 * LLOG_OPEN_EXIST - old llog, must exist
1208 * \retval 0 on successful open, llog_handle::lgh_obj
1209 * contains the dt_object of the llog.
1210 * \retval negative value on error
1212 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1213 struct llog_logid *logid, char *name,
1214 enum llog_open_param open_param)
1216 struct llog_thread_info *lgi = llog_info(env);
1217 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1218 struct dt_object *o;
1219 struct dt_device *dt;
1220 struct ls_device *ls;
1221 struct local_oid_storage *los = NULL;
1223 bool new_id = false;
1229 LASSERT(ctxt->loc_exp);
1230 LASSERT(ctxt->loc_exp->exp_obd);
1231 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1233 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1234 struct lu_object_conf conf = { 0 };
1235 if (logid != NULL) {
1236 logid_to_fid(logid, &lgi->lgi_fid);
1238 /* If logid == NULL, then it means the caller needs
1239 * to allocate new FID (llog_cat_declare_add_rec()). */
1240 rc = obd_fid_alloc(env, ctxt->loc_exp,
1241 &lgi->lgi_fid, NULL);
1245 conf.loc_flags = LOC_F_NEW;
1248 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1249 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1256 ls = ls_device_get(dt);
1258 RETURN(PTR_ERR(ls));
1260 mutex_lock(&ls->ls_los_mutex);
1261 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1262 mutex_unlock(&ls->ls_los_mutex);
1264 ls_device_put(env, ls);
1268 if (logid != NULL) {
1269 logid_to_fid(logid, &lgi->lgi_fid);
1271 struct dt_object *llog_dir;
1273 llog_dir = llog_osd_dir_get(env, ctxt);
1274 if (IS_ERR(llog_dir))
1275 GOTO(out, rc = PTR_ERR(llog_dir));
1276 dt_read_lock(env, llog_dir, 0);
1277 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1278 dt_read_unlock(env, llog_dir);
1279 dt_object_put(env, llog_dir);
1280 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1281 /* generate fid for new llog */
1282 rc = local_object_fid_generate(env, los,
1288 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1289 if (handle->lgh_name)
1290 strcpy(handle->lgh_name, name);
1292 GOTO(out, rc = -ENOMEM);
1294 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1295 /* generate fid for new llog */
1297 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1303 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1305 GOTO(out_name, rc = PTR_ERR(o));
1307 if (dt_object_exists(o) && new_id) {
1308 /* llog exists with just generated ID, e.g. some old llog file
1309 * still is in use or is orphan, drop a warn and skip it. */
1310 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1312 o->do_lu.lo_dev->ld_obd->obd_name,
1313 PFID(lu_object_fid(&o->do_lu)));
1314 dt_object_put(env, o);
1315 /* just skip this llog ID, we shouldn't delete it because we
1316 * don't know exactly what is its purpose and state. */
1321 /* No new llog is expected but doesn't exist */
1322 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1323 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1324 o->do_lu.lo_dev->ld_obd->obd_name,
1325 PFID(lu_object_fid(&o->do_lu)), o);
1326 GOTO(out_put, rc = -ENOENT);
1328 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1329 handle->lgh_obj = o;
1330 handle->private_data = los;
1331 LASSERT(handle->lgh_ctxt);
1336 dt_object_put(env, o);
1338 if (handle->lgh_name != NULL)
1339 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1347 * Get dir for regular fid log object
1349 * Get directory for regular fid log object, and these regular fid log
1350 * object will be inserted under this directory, to satisfy the FS
1351 * consistency check, e2fsck etc.
1353 * \param [in] env execution environment
1354 * \param [in] dto llog object
1356 * \retval pointer to the directory if it is found.
1357 * \retval ERR_PTR(negative errno) if it fails.
1359 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1360 struct dt_object *dto)
1362 struct llog_thread_info *lgi = llog_info(env);
1363 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1364 struct lu_seq_range *range = &lgi->lgi_range;
1365 struct lu_fid *dir_fid = &lgi->lgi_fid;
1366 struct dt_object *dir;
1370 fld_range_set_any(range);
1371 LASSERT(ss != NULL);
1372 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1373 fid_seq(lu_object_fid(&dto->do_lu)), range);
1375 RETURN(ERR_PTR(rc));
1377 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1378 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1382 if (!dt_try_as_dir(env, dir)) {
1383 dt_object_put(env, dir);
1384 RETURN(ERR_PTR(-ENOTDIR));
1391 * Add llog object with regular FID to name entry
1393 * Add llog object with regular FID to name space, and each llog
1394 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1395 * so to satisfy the namespace consistency check, e2fsck etc.
1397 * \param [in] env execution environment
1398 * \param [in] dto llog object
1399 * \param [in] th thandle
1400 * \param [in] declare if it is declare or execution
1402 * \retval 0 if insertion succeeds.
1403 * \retval negative errno if insertion fails.
1406 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1407 struct dt_object *dto,
1408 struct thandle *th, bool declare)
1410 struct llog_thread_info *lgi = llog_info(env);
1411 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1412 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1413 struct dt_object *dir;
1414 char *name = lgi->lgi_name;
1418 if (!fid_is_norm(fid))
1421 dir = llog_osd_get_regular_fid_dir(env, dto);
1423 RETURN(PTR_ERR(dir));
1426 rec->rec_type = S_IFREG;
1427 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1428 dt_write_lock(env, dir, 0);
1430 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1431 (struct dt_key *)name, th);
1433 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1434 (struct dt_key *)name, th);
1436 dt_write_unlock(env, dir);
1438 dt_object_put(env, dir);
1444 * Implementation of the llog_operations::lop_declare_create
1446 * This function declares the llog create. It declares also name insert
1447 * into llog directory in case of named llog.
1449 * \param[in] env execution environment
1450 * \param[in] res llog handle of the current llog
1451 * \param[in] th current transaction handle
1453 * \retval 0 on successful create declaration
1454 * \retval negative value on error
1456 static int llog_osd_declare_create(const struct lu_env *env,
1457 struct llog_handle *res, struct thandle *th)
1459 struct llog_thread_info *lgi = llog_info(env);
1460 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1461 struct local_oid_storage *los;
1462 struct dt_object *o;
1467 LASSERT(res->lgh_obj);
1470 /* object can be created by another thread */
1472 if (dt_object_exists(o))
1475 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1476 struct llog_thread_info *lgi = llog_info(env);
1478 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1479 lgi->lgi_attr.la_size = 0;
1480 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1481 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1483 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1489 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1493 los = res->private_data;
1496 rc = llog_osd_declare_new_object(env, los, o, th);
1500 /* do not declare header initialization here as it's declared
1501 * in llog_osd_declare_write_rec() which is always called */
1503 if (res->lgh_name) {
1504 struct dt_object *llog_dir;
1506 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1507 if (IS_ERR(llog_dir))
1508 RETURN(PTR_ERR(llog_dir));
1509 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1510 rec->rec_fid = &lgi->lgi_fid;
1511 rec->rec_type = S_IFREG;
1512 rc = dt_declare_insert(env, llog_dir,
1513 (struct dt_rec *)rec,
1514 (struct dt_key *)res->lgh_name, th);
1515 dt_object_put(env, llog_dir);
1517 CERROR("%s: can't declare named llog %s: rc = %d\n",
1518 o->do_lu.lo_dev->ld_obd->obd_name,
1525 * Implementation of the llog_operations::lop_create
1527 * This function creates the llog according with llog_handle::lgh_obj
1528 * and llog_handle::lgh_name.
1530 * \param[in] env execution environment
1531 * \param[in] res llog handle of the current llog
1532 * \param[in] th current transaction handle
1534 * \retval 0 on successful create
1535 * \retval negative value on error
1537 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1540 struct llog_thread_info *lgi = llog_info(env);
1541 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1542 struct local_oid_storage *los;
1543 struct dt_object *o;
1552 /* llog can be already created */
1553 if (dt_object_exists(o))
1556 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1557 struct llog_thread_info *lgi = llog_info(env);
1559 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1560 lgi->lgi_attr.la_size = 0;
1561 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1562 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1564 dt_write_lock(env, o, 0);
1565 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1567 dt_write_unlock(env, o);
1571 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1576 los = res->private_data;
1579 dt_write_lock(env, o, 0);
1580 if (!dt_object_exists(o))
1581 rc = llog_osd_create_new_object(env, los, o, th);
1585 dt_write_unlock(env, o);
1589 if (res->lgh_name) {
1590 struct dt_object *llog_dir;
1592 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1593 if (IS_ERR(llog_dir))
1594 RETURN(PTR_ERR(llog_dir));
1596 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1597 rec->rec_fid = &lgi->lgi_fid;
1598 rec->rec_type = S_IFREG;
1599 dt_read_lock(env, llog_dir, 0);
1600 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1601 (struct dt_key *)res->lgh_name, th);
1602 dt_read_unlock(env, llog_dir);
1603 dt_object_put(env, llog_dir);
1605 CERROR("%s: can't create named llog %s: rc = %d\n",
1606 o->do_lu.lo_dev->ld_obd->obd_name,
1613 * Implementation of the llog_operations::lop_close
1615 * This function closes the llog. It just put llog object and referenced
1618 * \param[in] env execution environment
1619 * \param[in] handle llog handle of the current llog
1621 * \retval 0 on successful llog close
1622 * \retval negative value on error
1624 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1626 struct local_oid_storage *los;
1631 LASSERT(handle->lgh_obj);
1633 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1634 /* Remove the object from the cache, otherwise it may
1635 * hold LOD being released during cleanup process */
1636 dt_object_put_nocache(env, handle->lgh_obj);
1637 LASSERT(handle->private_data == NULL);
1640 dt_object_put(env, handle->lgh_obj);
1642 los = handle->private_data;
1646 if (handle->lgh_name)
1647 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1653 * delete llog object name entry
1655 * Delete llog object (with regular FID) from name space (under
1658 * \param [in] env execution environment
1659 * \param [in] dto llog object
1660 * \param [in] th thandle
1661 * \param [in] declare if it is declare or execution
1663 * \retval 0 if deletion succeeds.
1664 * \retval negative errno if deletion fails.
1667 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1668 struct dt_object *dto,
1669 struct thandle *th, bool declare)
1671 struct llog_thread_info *lgi = llog_info(env);
1672 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1673 struct dt_object *dir;
1674 char *name = lgi->lgi_name;
1678 if (!fid_is_norm(fid))
1681 dir = llog_osd_get_regular_fid_dir(env, dto);
1683 RETURN(PTR_ERR(dir));
1685 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1686 dt_write_lock(env, dir, 0);
1688 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1691 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1693 dt_write_unlock(env, dir);
1695 dt_object_put(env, dir);
1700 * Implementation of the llog_operations::lop_declare_destroy
1702 * This function declare destroys the llog and deletes also entry in the
1703 * llog directory in case of named llog. Llog should be opened prior that.
1705 * \param[in] env execution environment
1706 * \param[in] loghandle llog handle of the current llog
1708 * \retval 0 on successful destroy
1709 * \retval negative value on error
1711 static int llog_osd_declare_destroy(const struct lu_env *env,
1712 struct llog_handle *loghandle,
1715 struct llog_ctxt *ctxt;
1716 struct dt_object *o, *llog_dir = NULL;
1721 ctxt = loghandle->lgh_ctxt;
1724 o = loghandle->lgh_obj;
1727 if (loghandle->lgh_name) {
1728 llog_dir = llog_osd_dir_get(env, ctxt);
1729 if (IS_ERR(llog_dir))
1730 RETURN(PTR_ERR(llog_dir));
1732 rc = dt_declare_delete(env, llog_dir,
1733 (struct dt_key *)loghandle->lgh_name,
1739 rc = dt_declare_ref_del(env, o, th);
1743 rc = dt_declare_destroy(env, o, th);
1747 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1748 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1754 if (!(IS_ERR_OR_NULL(llog_dir)))
1755 dt_object_put(env, llog_dir);
1762 * Implementation of the llog_operations::lop_destroy
1764 * This function destroys the llog and deletes also entry in the
1765 * llog directory in case of named llog. Llog should be opened prior that.
1766 * Destroy method is not part of external transaction and does everything
1769 * \param[in] env execution environment
1770 * \param[in] loghandle llog handle of the current llog
1772 * \retval 0 on successful destroy
1773 * \retval negative value on error
1775 static int llog_osd_destroy(const struct lu_env *env,
1776 struct llog_handle *loghandle, struct thandle *th)
1778 struct llog_ctxt *ctxt;
1779 struct dt_object *o, *llog_dir = NULL;
1784 ctxt = loghandle->lgh_ctxt;
1785 LASSERT(ctxt != NULL);
1787 o = loghandle->lgh_obj;
1790 dt_write_lock(env, o, 0);
1791 if (!dt_object_exists(o))
1792 GOTO(out_unlock, rc = 0);
1794 if (loghandle->lgh_name) {
1795 llog_dir = llog_osd_dir_get(env, ctxt);
1796 if (IS_ERR(llog_dir))
1797 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1799 dt_read_lock(env, llog_dir, 0);
1800 rc = dt_delete(env, llog_dir,
1801 (struct dt_key *)loghandle->lgh_name,
1803 dt_read_unlock(env, llog_dir);
1805 CERROR("%s: can't remove llog %s: rc = %d\n",
1806 o->do_lu.lo_dev->ld_obd->obd_name,
1807 loghandle->lgh_name, rc);
1808 GOTO(out_unlock, rc);
1812 dt_ref_del(env, o, th);
1813 rc = dt_destroy(env, o, th);
1815 GOTO(out_unlock, rc);
1817 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1818 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1820 GOTO(out_unlock, rc);
1824 dt_write_unlock(env, o);
1825 if (!(IS_ERR_OR_NULL(llog_dir)))
1826 dt_object_put(env, llog_dir);
1831 * Implementation of the llog_operations::lop_setup
1833 * This function setup the llog on local storage.
1835 * \param[in] env execution environment
1836 * \param[in] obd obd device the llog belongs to
1837 * \param[in] olg the llog group, it is always zero group now.
1838 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1839 * Every new llog type have to use own index.
1840 * \param[in] disk_obd the storage obd, where llog is stored.
1842 * \retval 0 on successful llog setup
1843 * \retval negative value on error
1845 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1846 struct obd_llog_group *olg, int ctxt_idx,
1847 struct obd_device *disk_obd)
1849 struct llog_thread_info *lgi = llog_info(env);
1850 struct llog_ctxt *ctxt;
1855 LASSERT(olg->olg_ctxts[ctxt_idx]);
1857 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1860 if (disk_obd == NULL)
1863 /* initialize data allowing to generate new fids,
1864 * literally we need a sequece */
1865 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1866 lgi->lgi_fid.f_oid = 1;
1867 lgi->lgi_fid.f_ver = 0;
1868 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1870 &ctxt->loc_los_nameless);
1874 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1875 lgi->lgi_fid.f_oid = 1;
1876 lgi->lgi_fid.f_ver = 0;
1877 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1879 &ctxt->loc_los_named);
1881 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1882 ctxt->loc_los_nameless = NULL;
1888 llog_ctxt_put(ctxt);
1893 * Implementation of the llog_operations::lop_cleanup
1895 * This function cleanups the llog on local storage.
1897 * \param[in] env execution environment
1898 * \param[in] ctxt the llog context
1902 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1904 if (ctxt->loc_los_nameless != NULL) {
1905 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1906 ctxt->loc_los_nameless = NULL;
1909 if (ctxt->loc_los_named != NULL) {
1910 local_oid_storage_fini(env, ctxt->loc_los_named);
1911 ctxt->loc_los_named = NULL;
1917 struct llog_operations llog_osd_ops = {
1918 .lop_next_block = llog_osd_next_block,
1919 .lop_prev_block = llog_osd_prev_block,
1920 .lop_read_header = llog_osd_read_header,
1921 .lop_declare_destroy = llog_osd_declare_destroy,
1922 .lop_destroy = llog_osd_destroy,
1923 .lop_setup = llog_osd_setup,
1924 .lop_cleanup = llog_osd_cleanup,
1925 .lop_open = llog_osd_open,
1926 .lop_exist = llog_osd_exist,
1927 .lop_declare_create = llog_osd_declare_create,
1928 .lop_create = llog_osd_create,
1929 .lop_declare_write_rec = llog_osd_declare_write_rec,
1930 .lop_write_rec = llog_osd_write_rec,
1931 .lop_close = llog_osd_close,
1933 EXPORT_SYMBOL(llog_osd_ops);
1935 struct llog_operations llog_common_cat_ops = {
1936 .lop_next_block = llog_osd_next_block,
1937 .lop_prev_block = llog_osd_prev_block,
1938 .lop_read_header = llog_osd_read_header,
1939 .lop_declare_destroy = llog_osd_declare_destroy,
1940 .lop_destroy = llog_osd_destroy,
1941 .lop_setup = llog_osd_setup,
1942 .lop_cleanup = llog_osd_cleanup,
1943 .lop_open = llog_osd_open,
1944 .lop_exist = llog_osd_exist,
1945 .lop_declare_create = llog_osd_declare_create,
1946 .lop_create = llog_osd_create,
1947 .lop_declare_write_rec = llog_osd_declare_write_rec,
1948 .lop_write_rec = llog_osd_write_rec,
1949 .lop_close = llog_osd_close,
1950 .lop_add = llog_cat_add_rec,
1951 .lop_declare_add = llog_cat_declare_add_rec,
1953 EXPORT_SYMBOL(llog_common_cat_ops);
1956 * Read the special file which contains the list of llog catalogs IDs
1958 * This function reads the CATALOGS file which contains the array of llog
1959 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1960 * by OST/MDT number.
1962 * \param[in] env execution environment
1963 * \param[in] d corresponding storage device
1964 * \param[in] idx position to start from, usually OST/MDT index
1965 * \param[in] count how many catalog IDs to read
1966 * \param[out] idarray the buffer for the data. If it is NULL then
1967 * function returns just number of catalog IDs
1969 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1971 * \retval 0 on successful read of catalog IDs
1972 * \retval negative value on error
1973 * \retval positive value which is number of records in
1974 * the file if \a idarray is NULL
1976 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1977 int idx, int count, struct llog_catid *idarray,
1978 const struct lu_fid *fid)
1980 struct llog_thread_info *lgi = llog_info(env);
1981 struct dt_object *o = NULL;
1989 size = sizeof(*idarray) * count;
1990 lgi->lgi_off = idx * sizeof(*idarray);
1992 lgi->lgi_fid = *fid;
1993 o = dt_locate(env, d, &lgi->lgi_fid);
1997 if (!dt_object_exists(o)) {
1998 th = dt_trans_create(env, d);
2000 GOTO(out, rc = PTR_ERR(th));
2002 lgi->lgi_attr.la_valid = LA_MODE;
2003 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2004 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2006 th->th_wait_submit = 1;
2007 /* Make the llog object creation synchronization, so
2008 * it will be reliable to the reference, especially
2009 * for remote reference */
2012 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2015 GOTO(out_trans, rc);
2017 rc = dt_trans_start_local(env, d, th);
2019 GOTO(out_trans, rc);
2021 dt_write_lock(env, o, 0);
2022 if (!dt_object_exists(o))
2023 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2025 dt_write_unlock(env, o);
2027 dt_trans_stop(env, d, th);
2032 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2036 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2037 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2038 o->do_lu.lo_dev->ld_obd->obd_name,
2039 lgi->lgi_attr.la_mode);
2040 GOTO(out, rc = -ENOENT);
2043 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2044 (int)lgi->lgi_attr.la_size, size);
2046 /* return just number of llogs */
2047 if (idarray == NULL) {
2048 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2052 /* read for new ost index or for empty file */
2053 memset(idarray, 0, size);
2054 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2056 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2057 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2059 lgi->lgi_buf.lb_buf = idarray;
2060 lgi->lgi_buf.lb_len = size;
2061 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2062 /* -EFAULT means the llog is a sparse file. This is not an error
2063 * after arbitrary OST index is supported. */
2064 if (rc < 0 && rc != -EFAULT) {
2065 CERROR("%s: error reading CATALOGS: rc = %d\n",
2066 o->do_lu.lo_dev->ld_obd->obd_name, rc);
2072 dt_object_put(env, o);
2075 EXPORT_SYMBOL(llog_osd_get_cat_list);
2078 * Write the special file which contains the list of llog catalogs IDs
2080 * This function writes the CATALOG file which contains the array of llog
2081 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2084 * \param[in] env execution environment
2085 * \param[in] d corresponding storage device
2086 * \param[in] idx position to start from, usually OST/MDT index
2087 * \param[in] count how many catalog IDs to write
2088 * \param[out] idarray the buffer with the data to write.
2089 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2091 * \retval 0 on successful write of catalog IDs
2092 * \retval negative value on error
2094 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2095 int idx, int count, struct llog_catid *idarray,
2096 const struct lu_fid *fid)
2098 struct llog_thread_info *lgi = llog_info(env);
2099 struct dt_object *o = NULL;
2108 size = sizeof(*idarray) * count;
2109 lgi->lgi_off = idx * sizeof(*idarray);
2110 lgi->lgi_fid = *fid;
2112 o = dt_locate(env, d, &lgi->lgi_fid);
2116 if (!dt_object_exists(o))
2117 GOTO(out, rc = -ENOENT);
2119 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2123 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2124 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2125 o->do_lu.lo_dev->ld_obd->obd_name,
2126 lgi->lgi_attr.la_mode);
2127 GOTO(out, rc = -ENOENT);
2130 th = dt_trans_create(env, d);
2132 GOTO(out, rc = PTR_ERR(th));
2134 lgi->lgi_buf.lb_len = size;
2135 lgi->lgi_buf.lb_buf = idarray;
2136 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2138 GOTO(out_trans, rc);
2140 /* For update log, this happens during initialization,
2141 * see lod_sub_prep_llog(), and we need make sure catlog
2142 * file ID is written to catlist file(committed) before
2143 * cross-MDT operation write update records to catlog FILE,
2144 * otherwise, during failover these update records might
2146 if (fid_is_update_log(fid))
2149 rc = dt_trans_start_local(env, d, th);
2151 GOTO(out_trans, rc);
2153 th->th_wait_submit = 1;
2155 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2157 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2160 dt_trans_stop(env, d, th);
2162 dt_object_put(env, o);
2165 EXPORT_SYMBOL(llog_osd_put_cat_list);