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 <linux/delay.h>
49 #include <dt_object.h>
50 #include <llog_swab.h>
51 #include <lustre_fid.h>
53 #include <obd_class.h>
55 #include "llog_internal.h"
56 #include "local_storage.h"
59 * Implementation of the llog_operations::lop_declare_create
61 * This function is a wrapper over local_storage API function
62 * local_object_declare_create().
64 * \param[in] env execution environment
65 * \param[in] los local_storage for bottom storage device
66 * \param[in] o dt_object to create
67 * \param[in] th current transaction handle
69 * \retval 0 on successful declaration of the new object
70 * \retval negative error if declaration was failed
72 static int llog_osd_declare_new_object(const struct lu_env *env,
73 struct local_oid_storage *los,
77 struct llog_thread_info *lgi = llog_info(env);
79 lgi->lgi_attr.la_valid = LA_MODE;
80 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
81 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
83 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
88 * Implementation of the llog_operations::lop_create
90 * This function is a wrapper over local_storage API function
91 * local_object_create().
93 * \param[in] env execution environment
94 * \param[in] los local_storage for bottom storage device
95 * \param[in] o dt_object to create
96 * \param[in] th current transaction handle
98 * \retval 0 on successful creation of the new object
99 * \retval negative error if creation was failed
101 static int llog_osd_create_new_object(const struct lu_env *env,
102 struct local_oid_storage *los,
106 struct llog_thread_info *lgi = llog_info(env);
108 lgi->lgi_attr.la_valid = LA_MODE;
109 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
110 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
112 return local_object_create(env, los, o, &lgi->lgi_attr,
117 * Implementation of the llog_operations::lop_exist
119 * This function checks that llog exists on storage.
121 * \param[in] handle llog handle of the current llog
123 * \retval true if llog object exists and is not just destroyed
124 * \retval false if llog doesn't exist or just destroyed
126 static int llog_osd_exist(struct llog_handle *handle)
128 LASSERT(handle->lgh_obj);
129 return dt_object_exists(handle->lgh_obj) && !handle->lgh_destroyed;
132 static void *rec_tail(struct llog_rec_hdr *rec)
134 return (void *)((char *)rec + rec->lrh_len -
135 sizeof(struct llog_rec_tail));
139 * Write a padding record to the llog
141 * This function writes a padding record to the end of llog. That may
142 * be needed if llog contains records of variable size, e.g. config logs
144 * The padding record just aligns llog to the llog chunk_size boundary if
145 * the current record doesn't fit in the remaining space.
147 * It allocates full length to avoid two separate writes for header and tail.
148 * Such 2-steps scheme needs extra protection and complex error handling.
150 * \param[in] env execution environment
151 * \param[in] o dt_object to create
152 * \param[in,out] off pointer to the padding start offset
153 * \param[in] len padding length
154 * \param[in] index index of the padding record in a llog
155 * \param[in] th current transaction handle
157 * \retval 0 on successful padding write
158 * \retval negative error if write failed
160 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
161 loff_t *off, int len, int index, struct thandle *th)
163 struct llog_thread_info *lgi = llog_info(env);
164 struct llog_rec_hdr *rec;
165 struct llog_rec_tail *tail;
172 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
179 rec->lrh_index = index;
180 rec->lrh_type = LLOG_PAD_MAGIC;
182 tail = rec_tail(rec);
184 tail->lrt_index = index;
186 lgi->lgi_buf.lb_buf = rec;
187 lgi->lgi_buf.lb_len = len;
188 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
190 CERROR("%s: error writing padding record: rc = %d\n",
191 o->do_lu.lo_dev->ld_obd->obd_name, rc);
198 * Implementation of the llog_operations::lop_read_header
200 * This function reads the current llog header from the bottom storage
203 * \param[in] env execution environment
204 * \param[in] handle llog handle of the current llog
206 * \retval 0 on successful header read
207 * \retval negative error if read failed
209 static int llog_osd_read_header(const struct lu_env *env,
210 struct llog_handle *handle)
212 struct llog_rec_hdr *llh_hdr;
214 struct llog_thread_info *lgi;
215 enum llog_flag flags;
223 lgi = llog_info(env);
225 rc = dt_attr_get(env, o, &lgi->lgi_attr);
229 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
231 if (lgi->lgi_attr.la_size == 0) {
232 CDEBUG(D_HA, "not reading header from 0-byte log\n");
236 flags = handle->lgh_hdr->llh_flags;
239 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
240 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
241 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
242 llh_hdr = &handle->lgh_hdr->llh_hdr;
243 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
244 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
245 o->do_lu.lo_dev->ld_obd->obd_name,
246 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
255 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
256 lustre_swab_llog_hdr(handle->lgh_hdr);
258 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
259 CERROR("%s: bad log %s "DFID" header magic: %#x "
260 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
261 handle->lgh_name ? handle->lgh_name : "",
262 PFID(lu_object_fid(&o->do_lu)),
263 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
265 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
266 llh_hdr->lrh_len > handle->lgh_hdr_size) {
267 CERROR("%s: incorrectly sized log %s "DFID" header: "
268 "%#x (expected at least %#x)\n"
269 "you may need to re-run lconf --write_conf.\n",
270 o->do_lu.lo_dev->ld_obd->obd_name,
271 handle->lgh_name ? handle->lgh_name : "",
272 PFID(lu_object_fid(&o->do_lu)),
273 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
275 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
276 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
277 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
279 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
281 o->do_lu.lo_dev->ld_obd->obd_name,
282 handle->lgh_name ? handle->lgh_name : "",
283 PFID(lu_object_fid(&o->do_lu)),
284 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
288 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
289 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
295 * Implementation of the llog_operations::lop_declare_write
297 * This function declares the new record write.
299 * \param[in] env execution environment
300 * \param[in] loghandle llog handle of the current llog
301 * \param[in] rec llog record header. This is a real header of the full
302 * llog record to write. This is the beginning of buffer
303 * to write, the length of buffer is stored in
305 * \param[in] idx index of the llog record. If \a idx == -1 then this is
306 * append case, otherwise \a idx is the index of record
308 * \param[in] th current transaction handle
310 * \retval 0 on successful declaration
311 * \retval negative error if declaration failed
313 static int llog_osd_declare_write_rec(const struct lu_env *env,
314 struct llog_handle *loghandle,
315 struct llog_rec_hdr *rec,
316 int idx, struct thandle *th)
318 struct llog_thread_info *lgi = llog_info(env);
329 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
331 o = loghandle->lgh_obj;
334 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
335 lgi->lgi_buf.lb_len = chunk_size;
336 lgi->lgi_buf.lb_buf = NULL;
337 /* each time we update header */
338 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
340 if (rc || idx == 0) /* if error or just header */
344 * the pad record can be inserted so take into account double
347 lgi->lgi_buf.lb_len = chunk_size * 2;
348 lgi->lgi_buf.lb_buf = NULL;
349 /* XXX: implement declared window or multi-chunks approach */
350 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
356 * Implementation of the llog_operations::lop_write
358 * This function writes the new record in the llog or modify the existed one.
360 * \param[in] env execution environment
361 * \param[in] loghandle llog handle of the current llog
362 * \param[in] rec llog record header. This is a real header of
363 * the full llog record to write. This is
364 * the beginning of buffer to write, the length
365 * of buffer is stored in \a rec::lrh_len
366 * \param[in,out] reccookie pointer to the cookie to return back if needed.
367 * It is used for further cancel of this llog
369 * \param[in] idx index of the llog record. If \a idx == -1 then
370 * this is append case, otherwise \a idx is
371 * the index of record to modify
372 * \param[in] th current transaction handle
374 * \retval 0 on successful write && \a reccookie == NULL
375 * 1 on successful write && \a reccookie != NULL
376 * \retval negative error if write failed
378 static int llog_osd_write_rec(const struct lu_env *env,
379 struct llog_handle *loghandle,
380 struct llog_rec_hdr *rec,
381 struct llog_cookie *reccookie,
382 int idx, struct thandle *th)
384 struct llog_thread_info *lgi = llog_info(env);
385 struct llog_log_hdr *llh;
386 int reclen = rec->lrh_len;
388 struct llog_rec_tail *lrt;
395 llh = loghandle->lgh_hdr;
396 o = loghandle->lgh_obj;
398 chunk_size = llh->llh_hdr.lrh_len;
399 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
400 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
402 if (!llog_osd_exist(loghandle))
405 /* record length should not bigger than */
406 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
409 /* sanity check for fixed-records llog */
410 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
411 LASSERT(llh->llh_size != 0);
412 LASSERT(llh->llh_size == reclen);
415 rc = dt_attr_get(env, o, &lgi->lgi_attr);
420 * The modification case.
421 * If idx set then the record with that index must be modified.
422 * There are three cases possible:
423 * 1) the common case is the llog header update (idx == 0)
424 * 2) the llog record modification during llog process.
425 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
426 * In that case the \a loghandle::lgh_cur_offset
427 * 3) otherwise this is assumed that llog consist of records of
428 * fixed size, i.e. catalog. The llog header must has llh_size
429 * field equal to record size. The record offset is calculated
430 * just by /a idx value
432 * During modification we don't need extra header update because
433 * the bitmap and record count are not changed. The record header
434 * and tail remains the same too.
436 if (idx != LLOG_NEXT_IDX) {
437 /* llog can be empty only when first record is being written */
438 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
440 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
441 CERROR("%s: modify unset record %u\n",
442 o->do_lu.lo_dev->ld_obd->obd_name, idx);
446 if (idx != rec->lrh_index) {
447 CERROR("%s: modify index mismatch %d %u\n",
448 o->do_lu.lo_dev->ld_obd->obd_name, idx,
453 if (idx == LLOG_HEADER_IDX) {
454 /* llog header update */
455 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
459 /* If it does not indicate the bitmap index
460 * (reccookie == NULL), then it means update
461 * the whole update header. Otherwise only
462 * update header and bits needs to be updated,
463 * and in DNE cases, it will signaficantly
464 * shrink the RPC size.
465 * see distribute_txn_cancel_records()*/
466 if (reccookie == NULL) {
467 lgi->lgi_buf.lb_len = reclen;
468 lgi->lgi_buf.lb_buf = rec;
469 rc = dt_record_write(env, o, &lgi->lgi_buf,
474 /* update the header */
475 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
476 lgi->lgi_buf.lb_buf = llh;
477 rc = dt_record_write(env, o, &lgi->lgi_buf,
482 /* update the bitmap */
483 index = reccookie->lgc_index;
484 lgi->lgi_off = llh->llh_bitmap_offset +
485 (index / (sizeof(*bitmap) * 8)) *
487 lgi->lgi_buf.lb_len = sizeof(*bitmap);
488 lgi->lgi_buf.lb_buf =
489 &bitmap[index/(sizeof(*bitmap)*8)];
490 rc = dt_record_write(env, o, &lgi->lgi_buf,
494 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
495 lgi->lgi_off = llh->llh_hdr.lrh_len +
497 } else if (reccookie != NULL && reccookie->lgc_index > 0) {
499 * The lgc_offset can be used only if index is
502 if (idx != reccookie->lgc_index) {
503 CERROR("%s: modify index mismatch %d %d\n",
504 o->do_lu.lo_dev->ld_obd->obd_name, idx,
505 reccookie->lgc_index);
509 lgi->lgi_off = reccookie->lgc_offset;
510 CDEBUG(D_OTHER, "modify record "DFID": idx:%u, "
511 "len:%u offset %llu\n",
512 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), idx,
513 rec->lrh_len, (long long)lgi->lgi_off);
515 /* This can be result of lgh_cur_idx is not set during
516 * llog processing or llh_size is not set to proper
517 * record size for fixed records llog. Therefore it is
518 * impossible to get record offset. */
519 CERROR("%s: can't get record offset, idx:%d, "
520 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
525 /* update only data, header and tail remain the same */
526 lgi->lgi_off += sizeof(struct llog_rec_hdr);
527 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
528 lgi->lgi_buf.lb_buf = REC_DATA(rec);
529 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
530 if (rc == 0 && reccookie) {
531 reccookie->lgc_lgl = loghandle->lgh_id;
532 reccookie->lgc_index = idx;
540 * The most common case of using llog. The new index is assigned to
541 * the new record, new bit is set in llog bitmap and llog count is
544 * Make sure that records don't cross a chunk boundary, so we can
545 * process them page-at-a-time if needed. If it will cross a chunk
546 * boundary, write in a fake (but referenced) entry to pad the chunk.
550 /* simulate ENOSPC when new plain llog is being added to the
552 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
553 llh->llh_flags & LLOG_F_IS_CAT)
556 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
557 orig_last_idx = loghandle->lgh_last_idx;
558 lgi->lgi_off = lgi->lgi_attr.la_size;
560 if (loghandle->lgh_max_size > 0 &&
561 lgi->lgi_off >= loghandle->lgh_max_size) {
562 CDEBUG(D_OTHER, "llog is getting too large (%u > %u) at %u "
563 DFID"\n", (unsigned)lgi->lgi_off,
564 loghandle->lgh_max_size, (int)loghandle->lgh_last_idx,
565 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
566 /* this is to signal that this llog is full */
567 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
571 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
572 /* NOTE: padding is a record, but no bit is set */
573 if (left != 0 && left != reclen &&
574 left < (reclen + LLOG_MIN_REC_SIZE)) {
575 index = loghandle->lgh_last_idx + 1;
576 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
580 loghandle->lgh_last_idx++; /* for pad rec */
582 /* if it's the last idx in log file, then return -ENOSPC
583 * or wrap around if a catalog */
584 if (llog_is_full(loghandle) ||
585 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
586 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
587 loghandle->lgh_last_idx >= cfs_fail_val)) {
588 if (llh->llh_flags & LLOG_F_IS_CAT)
589 loghandle->lgh_last_idx = 0;
594 down_write(&loghandle->lgh_last_sem);
595 /* increment the last_idx along with llh_tail index, they should
596 * be equal for a llog lifetime */
597 loghandle->lgh_last_idx++;
598 index = loghandle->lgh_last_idx;
599 LLOG_HDR_TAIL(llh)->lrt_index = index;
601 * NB: the caller should make sure only 1 process access
602 * the lgh_last_idx, e.g. append should be exclusive.
603 * Otherwise it might hit the assert.
605 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
606 rec->lrh_index = index;
608 lrt->lrt_len = rec->lrh_len;
609 lrt->lrt_index = rec->lrh_index;
611 /* the lgh_hdr_mutex protects llog header data from concurrent
612 * update/cancel, the llh_count and llh_bitmap are protected */
613 mutex_lock(&loghandle->lgh_hdr_mutex);
614 if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
615 CERROR("%s: index %u already set in log bitmap\n",
616 o->do_lu.lo_dev->ld_obd->obd_name, index);
617 mutex_unlock(&loghandle->lgh_hdr_mutex);
618 LBUG(); /* should never happen */
622 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
623 /* Update the minimum size of the llog record */
624 if (llh->llh_size == 0)
625 llh->llh_size = reclen;
626 else if (reclen < llh->llh_size)
627 llh->llh_size = reclen;
630 if (lgi->lgi_attr.la_size == 0) {
632 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
633 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
634 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
636 GOTO(out_unlock, rc);
638 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
640 /* Note: If this is not initialization (size == 0), then do not
641 * write the whole header (8k bytes), only update header/tail
642 * and bits needs to be updated. Because this update might be
643 * part of cross-MDT operation, which needs to write these
644 * updates into the update log(32KB limit) and also pack inside
645 * the RPC (1MB limit), if we write 8K for each operation, which
646 * will cost a lot space, and keep us adding more updates to one
649 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
650 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
651 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
653 GOTO(out_unlock, rc);
655 lgi->lgi_off = llh->llh_bitmap_offset +
656 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
657 lgi->lgi_buf.lb_len = sizeof(*bitmap);
658 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
659 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
661 GOTO(out_unlock, rc);
663 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
665 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
666 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
667 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
669 GOTO(out_unlock, rc);
673 /* unlock here for remote object */
674 mutex_unlock(&loghandle->lgh_hdr_mutex);
678 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
679 cfs_fail_val == (unsigned int)(loghandle->lgh_id.lgl_oi.oi.oi_id &
681 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
682 msleep(1 * MSEC_PER_SEC);
684 /* computed index can be used to determine offset for fixed-size
685 * records. This also allows to handle Catalog wrap around case */
686 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
687 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
689 rc = dt_attr_get(env, o, &lgi->lgi_attr);
693 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
694 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
698 lgi->lgi_buf.lb_len = reclen;
699 lgi->lgi_buf.lb_buf = rec;
700 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
704 up_write(&loghandle->lgh_last_sem);
706 CDEBUG(D_HA, "added record "DFID".%u, %u off%llu\n",
707 PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
709 if (reccookie != NULL) {
710 reccookie->lgc_lgl = loghandle->lgh_id;
711 reccookie->lgc_index = index;
712 if ((rec->lrh_type == MDS_UNLINK_REC) ||
713 (rec->lrh_type == MDS_SETATTR64_REC))
714 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
715 else if (rec->lrh_type == OST_SZ_REC)
716 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
718 reccookie->lgc_subsys = -1;
723 /* cleanup llog for error case */
724 mutex_lock(&loghandle->lgh_hdr_mutex);
725 ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
727 mutex_unlock(&loghandle->lgh_hdr_mutex);
729 /* restore llog last_idx */
730 if (dt_object_remote(o)) {
731 loghandle->lgh_last_idx = orig_last_idx;
732 } else if (--loghandle->lgh_last_idx == 0 &&
733 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
734 /* catalog had just wrap-around case */
735 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
738 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
739 up_write(&loghandle->lgh_last_sem);
745 * We can skip reading at least as many log blocks as the number of
746 * minimum sized log records we are skipping. If it turns out
747 * that we are not far enough along the log (because the
748 * actual records are larger than minimum size) we just skip
751 * Note: in llog_process_thread, it will use bitmap offset as
752 * the index to locate the record, which also includs some pad
753 * records, whose record size is very small, and it also does not
754 * consider pad record when recording minimum record size (otherwise
755 * min_record size might be too small), so in some rare cases,
756 * it might skip too much record for @goal, see llog_osd_next_block().
758 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
759 * as the min record size to skip over, usually because in the previous
760 * try, it skip too much record, see loog_osd_next(prev)_block().
762 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
763 int curr, int goal, __u32 chunk_size,
766 struct llog_log_hdr *llh = lgh->lgh_hdr;
768 /* Goal should not bigger than the record count */
769 if (goal > lgh->lgh_last_idx)
770 goal = lgh->lgh_last_idx;
773 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
774 *off = chunk_size + (goal - 1) * llh->llh_size;
776 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
778 if (llh->llh_size > 0 && !force_mini_rec)
779 min_rec_size = llh->llh_size;
781 *off = *off + (goal - curr - 1) * min_rec_size;
784 /* always align with lower chunk boundary*/
785 *off &= ~(chunk_size - 1);
789 * Remove optional fields that the client doesn't expect.
790 * This is typically in order to ensure compatibility with older clients.
791 * It is assumed that since we exclusively remove fields, the block will be
792 * big enough to handle the remapped records. It is also assumed that records
793 * of a block have the same format (i.e.: the same features enabled).
795 * \param[in,out] hdr Header of the block of records to remap.
796 * \param[in,out] last_hdr Last header, don't read past this point.
797 * \param[in] flags Flags describing the fields to keep.
798 * \param[in] extra_flags Flags describing the extra fields to keep.
800 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
801 struct llog_rec_hdr *last_hdr,
802 struct llog_handle *loghandle)
804 enum changelog_rec_flags flags = CLF_SUPPORTED;
805 enum changelog_rec_extra_flags extra_flags = CLFE_SUPPORTED;
807 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_XATTR))
808 extra_flags &= ~CLFE_XATTR;
809 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_OMODE))
810 extra_flags &= ~CLFE_OPEN;
811 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_NID))
812 extra_flags &= ~CLFE_NID;
813 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_UIDGID))
814 extra_flags &= ~CLFE_UIDGID;
815 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_EXTRA_FLAGS))
816 flags &= ~CLF_EXTRA_FLAGS;
817 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
820 if (flags == CLF_SUPPORTED && extra_flags == CLFE_SUPPORTED)
823 if (hdr->lrh_type != CHANGELOG_REC)
827 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
828 enum changelog_rec_extra_flags xflag = CLFE_INVALID;
830 if (flags & CLF_EXTRA_FLAGS &&
831 rec->cr_flags & CLF_EXTRA_FLAGS) {
832 xflag = changelog_rec_extra_flags(rec)->cr_extra_flags &
836 if (unlikely(hdr->lrh_len == 0)) {
837 /* It is corruption case, we cannot know the next rec,
838 * jump to the last one directly to avoid dead loop. */
839 LCONSOLE(D_WARNING, "Hit invalid llog record: "
840 "idx %u, type %u, id %u\n",
841 hdr->lrh_index, hdr->lrh_type, hdr->lrh_id);
842 hdr = llog_rec_hdr_next(last_hdr);
843 if (unlikely(hdr == last_hdr))
844 LCONSOLE(D_WARNING, "The last record crashed: "
845 "idx %u, type %u, id %u\n",
846 hdr->lrh_index, hdr->lrh_type,
851 changelog_remap_rec(rec, rec->cr_flags & flags, xflag);
852 hdr = llog_rec_hdr_next(hdr);
853 /* Yield CPU to avoid soft-lockup if there are too many records
856 } while ((char *)hdr <= (char *)last_hdr);
860 * Implementation of the llog_operations::lop_next_block
862 * This function finds the the next llog block to return which contains
863 * record with required index. It is main part of llog processing.
865 * \param[in] env execution environment
866 * \param[in] loghandle llog handle of the current llog
867 * \param[in,out] cur_idx index preceeding cur_offset
868 * \param[in] next_idx target index to find
869 * \param[in,out] cur_offset furtherst point read in the file
870 * \param[in] buf pointer to data buffer to fill
871 * \param[in] len required len to read, it is
872 * usually llog chunk_size.
874 * \retval 0 on successful buffer read
875 * \retval negative value on error
877 static int llog_osd_next_block(const struct lu_env *env,
878 struct llog_handle *loghandle, int *cur_idx,
879 int next_idx, __u64 *cur_offset, void *buf,
882 struct llog_thread_info *lgi = llog_info(env);
884 struct dt_device *dt;
887 int last_idx = *cur_idx;
888 __u64 last_offset = *cur_offset;
889 bool force_mini_rec = false;
896 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
897 if (len == 0 || len & (chunk_size - 1))
901 LASSERT(loghandle->lgh_ctxt);
903 o = loghandle->lgh_obj;
905 LASSERT(llog_osd_exist(loghandle));
906 dt = lu2dt_dev(o->do_lu.lo_dev);
909 rc = dt_attr_get(env, o, &lgi->lgi_attr);
913 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off"
914 "%llu), size %llu\n", next_idx, *cur_idx,
915 *cur_offset, lgi->lgi_attr.la_size);
917 while (*cur_offset < lgi->lgi_attr.la_size) {
918 struct llog_rec_hdr *rec, *last_rec;
919 struct llog_rec_tail *tail;
921 llog_skip_over(loghandle, cur_offset, *cur_idx,
922 next_idx, chunk_size, force_mini_rec);
924 /* read up to next llog chunk_size block */
925 lgi->lgi_buf.lb_len = chunk_size -
926 (*cur_offset & (chunk_size - 1));
927 lgi->lgi_buf.lb_buf = buf;
929 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
931 if (rc == -EBADR && !force_mini_rec)
934 CERROR("%s: can't read llog block from log "DFID
935 " offset %llu: rc = %d\n",
936 o->do_lu.lo_dev->ld_obd->obd_name,
937 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
943 /* signal the end of the valid buffer to
945 memset(buf + rc, 0, len - rc);
948 if (rc == 0) { /* end of file, nothing to do */
954 if (rc < sizeof(*tail)) {
958 CERROR("%s: invalid llog block at log id "DFID":%x "
960 o->do_lu.lo_dev->ld_obd->obd_name,
961 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
962 loghandle->lgh_id.lgl_ogen, *cur_offset);
963 GOTO(out, rc = -EINVAL);
967 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
968 lustre_swab_llog_rec(rec);
970 tail = (struct llog_rec_tail *)((char *)buf + rc -
971 sizeof(struct llog_rec_tail));
972 /* get the last record in block */
973 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
976 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
977 lustre_swab_llog_rec(last_rec);
979 if (last_rec->lrh_index != tail->lrt_index) {
980 CERROR("%s: invalid llog tail at log id "DFID":%x offset %llu last_rec idx %u tail idx %u lrt len %u read_size %d\n",
981 o->do_lu.lo_dev->ld_obd->obd_name,
982 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
983 loghandle->lgh_id.lgl_ogen, *cur_offset,
984 last_rec->lrh_index, tail->lrt_index,
986 GOTO(out, rc = -EINVAL);
989 *cur_idx = tail->lrt_index;
991 /* this shouldn't happen */
992 if (tail->lrt_index == 0) {
993 CERROR("%s: invalid llog tail at log id "DFID":%x "
994 "offset %llu bytes %d\n",
995 o->do_lu.lo_dev->ld_obd->obd_name,
996 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
997 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
998 GOTO(out, rc = -EINVAL);
1000 if (tail->lrt_index < next_idx) {
1001 last_idx = *cur_idx;
1002 last_offset = *cur_offset;
1006 /* sanity check that the start of the new buffer is no farther
1007 * than the record that we wanted. This shouldn't happen. */
1008 if (rec->lrh_index > next_idx) {
1009 if (!force_mini_rec && next_idx > last_idx)
1012 CERROR("%s: missed desired record? %u > %u\n",
1013 o->do_lu.lo_dev->ld_obd->obd_name,
1014 rec->lrh_index, next_idx);
1015 GOTO(out, rc = -ENOENT);
1018 /* Trim unsupported extensions for compat w/ older clients */
1019 changelog_block_trim_ext(rec, last_rec, loghandle);
1024 /* Note: because there are some pad records in the
1025 * llog, so llog_skip_over() might skip too much
1026 * records, let's try skip again with minimum record */
1027 force_mini_rec = true;
1028 *cur_offset = last_offset;
1029 *cur_idx = last_idx;
1031 GOTO(out, rc = -EIO);
1037 * Implementation of the llog_operations::lop_prev_block
1039 * This function finds the llog block to return which contains
1040 * record with required index but in reverse order - from end of llog
1042 * It is main part of reverse llog processing.
1044 * \param[in] env execution environment
1045 * \param[in] loghandle llog handle of the current llog
1046 * \param[in] prev_idx target index to find
1047 * \param[in] buf pointer to data buffer to fill
1048 * \param[in] len required len to read, it is llog_chunk_size usually.
1050 * \retval 0 on successful buffer read
1051 * \retval negative value on error
1053 static int llog_osd_prev_block(const struct lu_env *env,
1054 struct llog_handle *loghandle,
1055 int prev_idx, void *buf, int len)
1057 struct llog_thread_info *lgi = llog_info(env);
1058 struct dt_object *o;
1059 struct dt_device *dt;
1066 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1067 if (len == 0 || len & (chunk_size - 1))
1070 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1073 LASSERT(loghandle->lgh_ctxt);
1075 o = loghandle->lgh_obj;
1077 LASSERT(llog_osd_exist(loghandle));
1078 dt = lu2dt_dev(o->do_lu.lo_dev);
1081 /* Let's only use mini record size for previous block read
1083 cur_offset = chunk_size;
1084 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1087 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1091 while (cur_offset < lgi->lgi_attr.la_size) {
1092 struct llog_rec_hdr *rec, *last_rec;
1093 struct llog_rec_tail *tail;
1095 lgi->lgi_buf.lb_len = len;
1096 lgi->lgi_buf.lb_buf = buf;
1097 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1099 CERROR("%s: can't read llog block from log "DFID
1100 " offset %llu: rc = %d\n",
1101 o->do_lu.lo_dev->ld_obd->obd_name,
1102 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1106 if (rc == 0) /* end of file, nothing to do */
1109 if (rc < sizeof(*tail)) {
1110 CERROR("%s: invalid llog block at log id "DFID":%x "
1112 o->do_lu.lo_dev->ld_obd->obd_name,
1113 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1114 loghandle->lgh_id.lgl_ogen, cur_offset);
1115 GOTO(out, rc = -EINVAL);
1119 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1120 lustre_swab_llog_rec(rec);
1122 tail = (struct llog_rec_tail *)((char *)buf + rc -
1123 sizeof(struct llog_rec_tail));
1124 /* get the last record in block */
1125 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1126 le32_to_cpu(tail->lrt_len));
1128 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1129 lustre_swab_llog_rec(last_rec);
1130 LASSERT(last_rec->lrh_index == tail->lrt_index);
1132 /* this shouldn't happen */
1133 if (tail->lrt_index == 0) {
1134 CERROR("%s: invalid llog tail at log id "DFID":%x "
1136 o->do_lu.lo_dev->ld_obd->obd_name,
1137 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1138 loghandle->lgh_id.lgl_ogen, cur_offset);
1139 GOTO(out, rc = -EINVAL);
1141 if (tail->lrt_index < prev_idx)
1144 /* sanity check that the start of the new buffer is no farther
1145 * than the record that we wanted. This shouldn't happen. */
1146 if (rec->lrh_index > prev_idx) {
1147 CERROR("%s: missed desired record? %u > %u\n",
1148 o->do_lu.lo_dev->ld_obd->obd_name,
1149 rec->lrh_index, prev_idx);
1150 GOTO(out, rc = -ENOENT);
1153 /* Trim unsupported extensions for compat w/ older clients */
1154 changelog_block_trim_ext(rec, last_rec, loghandle);
1158 GOTO(out, rc = -EIO);
1164 * This is helper function to get llog directory object. It is used by named
1165 * llog operations to find/insert/delete llog entry from llog directory.
1167 * \param[in] env execution environment
1168 * \param[in] ctxt llog context
1170 * \retval dt_object of llog directory
1171 * \retval ERR_PTR of negative value on error
1173 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1174 struct llog_ctxt *ctxt)
1176 struct dt_device *dt;
1177 struct dt_thread_info *dti = dt_info(env);
1178 struct dt_object *dir;
1181 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1182 if (ctxt->loc_dir == NULL) {
1183 rc = dt_root_get(env, dt, &dti->dti_fid);
1186 dir = dt_locate(env, dt, &dti->dti_fid);
1188 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1189 dt_object_put(env, dir);
1190 return ERR_PTR(-ENOTDIR);
1193 lu_object_get(&ctxt->loc_dir->do_lu);
1194 dir = ctxt->loc_dir;
1201 * Implementation of the llog_operations::lop_open
1203 * This function opens the llog by its logid or by name, it may open also
1204 * non existent llog and assing then new id to it.
1205 * The llog_open/llog_close pair works similar to lu_object_find/put,
1206 * the object may not exist prior open. The result of open is just dt_object
1207 * in the llog header.
1209 * \param[in] env execution environment
1210 * \param[in] handle llog handle of the current llog
1211 * \param[in] logid logid of llog to open (nameless llog)
1212 * \param[in] name name of llog to open (named llog)
1213 * \param[in] open_param
1214 * LLOG_OPEN_NEW - new llog, may not exist
1215 * LLOG_OPEN_EXIST - old llog, must exist
1217 * \retval 0 on successful open, llog_handle::lgh_obj
1218 * contains the dt_object of the llog.
1219 * \retval negative value on error
1221 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1222 struct llog_logid *logid, char *name,
1223 enum llog_open_param open_param)
1225 struct llog_thread_info *lgi = llog_info(env);
1226 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1227 struct dt_object *o;
1228 struct dt_device *dt;
1229 struct ls_device *ls;
1230 struct local_oid_storage *los = NULL;
1232 bool new_id = false;
1238 LASSERT(ctxt->loc_exp);
1239 LASSERT(ctxt->loc_exp->exp_obd);
1240 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1242 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1243 struct lu_object_conf conf = { 0 };
1244 if (logid != NULL) {
1245 logid_to_fid(logid, &lgi->lgi_fid);
1247 /* If logid == NULL, then it means the caller needs
1248 * to allocate new FID (llog_cat_declare_add_rec()). */
1249 rc = obd_fid_alloc(env, ctxt->loc_exp,
1250 &lgi->lgi_fid, NULL);
1254 conf.loc_flags = LOC_F_NEW;
1257 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1258 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1265 ls = ls_device_get(dt);
1267 RETURN(PTR_ERR(ls));
1269 mutex_lock(&ls->ls_los_mutex);
1270 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1271 mutex_unlock(&ls->ls_los_mutex);
1273 ls_device_put(env, ls);
1277 if (logid != NULL) {
1278 logid_to_fid(logid, &lgi->lgi_fid);
1280 struct dt_object *llog_dir;
1282 llog_dir = llog_osd_dir_get(env, ctxt);
1283 if (IS_ERR(llog_dir))
1284 GOTO(out, rc = PTR_ERR(llog_dir));
1285 dt_read_lock(env, llog_dir, 0);
1286 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1287 dt_read_unlock(env, llog_dir);
1288 dt_object_put(env, llog_dir);
1289 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1290 /* generate fid for new llog */
1291 rc = local_object_fid_generate(env, los,
1297 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1298 if (handle->lgh_name)
1299 strcpy(handle->lgh_name, name);
1301 GOTO(out, rc = -ENOMEM);
1303 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1304 /* generate fid for new llog */
1306 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1312 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1314 GOTO(out_name, rc = PTR_ERR(o));
1316 if (dt_object_exists(o) && new_id) {
1317 /* llog exists with just generated ID, e.g. some old llog file
1318 * still is in use or is orphan, drop a warn and skip it. */
1319 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1321 o->do_lu.lo_dev->ld_obd->obd_name,
1322 PFID(lu_object_fid(&o->do_lu)));
1323 dt_object_put(env, o);
1324 /* just skip this llog ID, we shouldn't delete it because we
1325 * don't know exactly what is its purpose and state. */
1330 /* No new llog is expected but doesn't exist */
1331 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1332 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1333 o->do_lu.lo_dev->ld_obd->obd_name,
1334 PFID(lu_object_fid(&o->do_lu)), o);
1335 GOTO(out_put, rc = -ENOENT);
1337 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1338 handle->lgh_obj = o;
1339 handle->private_data = los;
1340 LASSERT(handle->lgh_ctxt);
1345 dt_object_put(env, o);
1347 if (handle->lgh_name != NULL)
1348 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1356 * Get dir for regular fid log object
1358 * Get directory for regular fid log object, and these regular fid log
1359 * object will be inserted under this directory, to satisfy the FS
1360 * consistency check, e2fsck etc.
1362 * \param [in] env execution environment
1363 * \param [in] dto llog object
1365 * \retval pointer to the directory if it is found.
1366 * \retval ERR_PTR(negative errno) if it fails.
1368 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1369 struct dt_object *dto)
1371 struct llog_thread_info *lgi = llog_info(env);
1372 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1373 struct lu_seq_range *range = &lgi->lgi_range;
1374 struct lu_fid *dir_fid = &lgi->lgi_fid;
1375 struct dt_object *dir;
1379 fld_range_set_any(range);
1380 LASSERT(ss != NULL);
1381 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1382 fid_seq(lu_object_fid(&dto->do_lu)), range);
1384 RETURN(ERR_PTR(rc));
1386 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1387 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1391 if (!dt_try_as_dir(env, dir)) {
1392 dt_object_put(env, dir);
1393 RETURN(ERR_PTR(-ENOTDIR));
1400 * Add llog object with regular FID to name entry
1402 * Add llog object with regular FID to name space, and each llog
1403 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1404 * so to satisfy the namespace consistency check, e2fsck etc.
1406 * \param [in] env execution environment
1407 * \param [in] dto llog object
1408 * \param [in] th thandle
1409 * \param [in] declare if it is declare or execution
1411 * \retval 0 if insertion succeeds.
1412 * \retval negative errno if insertion fails.
1415 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1416 struct dt_object *dto,
1417 struct thandle *th, bool declare)
1419 struct llog_thread_info *lgi = llog_info(env);
1420 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1421 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1422 struct dt_object *dir;
1423 char *name = lgi->lgi_name;
1427 if (!fid_is_norm(fid))
1430 dir = llog_osd_get_regular_fid_dir(env, dto);
1432 RETURN(PTR_ERR(dir));
1435 rec->rec_type = S_IFREG;
1436 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1437 dt_write_lock(env, dir, 0);
1439 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1440 (struct dt_key *)name, th);
1442 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1443 (struct dt_key *)name, th);
1445 dt_write_unlock(env, dir);
1447 dt_object_put(env, dir);
1453 * Implementation of the llog_operations::lop_declare_create
1455 * This function declares the llog create. It declares also name insert
1456 * into llog directory in case of named llog.
1458 * \param[in] env execution environment
1459 * \param[in] res llog handle of the current llog
1460 * \param[in] th current transaction handle
1462 * \retval 0 on successful create declaration
1463 * \retval negative value on error
1465 static int llog_osd_declare_create(const struct lu_env *env,
1466 struct llog_handle *res, struct thandle *th)
1468 struct llog_thread_info *lgi = llog_info(env);
1469 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1470 struct local_oid_storage *los;
1471 struct dt_object *o;
1476 LASSERT(res->lgh_obj);
1479 /* object can be created by another thread */
1481 if (dt_object_exists(o))
1484 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1485 struct llog_thread_info *lgi = llog_info(env);
1487 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1488 lgi->lgi_attr.la_size = 0;
1489 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1490 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1492 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1498 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1502 los = res->private_data;
1505 rc = llog_osd_declare_new_object(env, los, o, th);
1509 /* do not declare header initialization here as it's declared
1510 * in llog_osd_declare_write_rec() which is always called */
1512 if (res->lgh_name) {
1513 struct dt_object *llog_dir;
1515 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1516 if (IS_ERR(llog_dir))
1517 RETURN(PTR_ERR(llog_dir));
1518 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1519 rec->rec_fid = &lgi->lgi_fid;
1520 rec->rec_type = S_IFREG;
1521 rc = dt_declare_insert(env, llog_dir,
1522 (struct dt_rec *)rec,
1523 (struct dt_key *)res->lgh_name, th);
1524 dt_object_put(env, llog_dir);
1526 CERROR("%s: can't declare named llog %s: rc = %d\n",
1527 o->do_lu.lo_dev->ld_obd->obd_name,
1534 * Implementation of the llog_operations::lop_create
1536 * This function creates the llog according with llog_handle::lgh_obj
1537 * and llog_handle::lgh_name.
1539 * \param[in] env execution environment
1540 * \param[in] res llog handle of the current llog
1541 * \param[in] th current transaction handle
1543 * \retval 0 on successful create
1544 * \retval negative value on error
1546 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1549 struct llog_thread_info *lgi = llog_info(env);
1550 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1551 struct local_oid_storage *los;
1552 struct dt_object *o;
1561 /* llog can be already created */
1562 if (dt_object_exists(o))
1565 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1566 struct llog_thread_info *lgi = llog_info(env);
1568 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1569 lgi->lgi_attr.la_size = 0;
1570 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1571 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1573 dt_write_lock(env, o, 0);
1574 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1576 dt_write_unlock(env, o);
1580 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1585 los = res->private_data;
1588 dt_write_lock(env, o, 0);
1589 if (!dt_object_exists(o))
1590 rc = llog_osd_create_new_object(env, los, o, th);
1594 dt_write_unlock(env, o);
1598 if (res->lgh_name) {
1599 struct dt_object *llog_dir;
1601 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1602 if (IS_ERR(llog_dir))
1603 RETURN(PTR_ERR(llog_dir));
1605 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1606 rec->rec_fid = &lgi->lgi_fid;
1607 rec->rec_type = S_IFREG;
1608 dt_read_lock(env, llog_dir, 0);
1609 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1610 (struct dt_key *)res->lgh_name, th);
1611 dt_read_unlock(env, llog_dir);
1612 dt_object_put(env, llog_dir);
1614 CERROR("%s: can't create named llog %s: rc = %d\n",
1615 o->do_lu.lo_dev->ld_obd->obd_name,
1622 * Implementation of the llog_operations::lop_close
1624 * This function closes the llog. It just put llog object and referenced
1627 * \param[in] env execution environment
1628 * \param[in] handle llog handle of the current llog
1630 * \retval 0 on successful llog close
1631 * \retval negative value on error
1633 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1635 struct local_oid_storage *los;
1640 LASSERT(handle->lgh_obj);
1642 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1643 /* Remove the object from the cache, otherwise it may
1644 * hold LOD being released during cleanup process */
1645 dt_object_put_nocache(env, handle->lgh_obj);
1646 LASSERT(handle->private_data == NULL);
1649 dt_object_put(env, handle->lgh_obj);
1651 los = handle->private_data;
1655 if (handle->lgh_name)
1656 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1662 * delete llog object name entry
1664 * Delete llog object (with regular FID) from name space (under
1667 * \param [in] env execution environment
1668 * \param [in] dto llog object
1669 * \param [in] th thandle
1670 * \param [in] declare if it is declare or execution
1672 * \retval 0 if deletion succeeds.
1673 * \retval negative errno if deletion fails.
1676 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1677 struct dt_object *dto,
1678 struct thandle *th, bool declare)
1680 struct llog_thread_info *lgi = llog_info(env);
1681 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1682 struct dt_object *dir;
1683 char *name = lgi->lgi_name;
1687 if (!fid_is_norm(fid))
1690 dir = llog_osd_get_regular_fid_dir(env, dto);
1692 RETURN(PTR_ERR(dir));
1694 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1695 dt_write_lock(env, dir, 0);
1697 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1700 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1702 dt_write_unlock(env, dir);
1704 dt_object_put(env, dir);
1709 * Implementation of the llog_operations::lop_declare_destroy
1711 * This function declare destroys the llog and deletes also entry in the
1712 * llog directory in case of named llog. Llog should be opened prior that.
1714 * \param[in] env execution environment
1715 * \param[in] loghandle llog handle of the current llog
1717 * \retval 0 on successful destroy
1718 * \retval negative value on error
1720 static int llog_osd_declare_destroy(const struct lu_env *env,
1721 struct llog_handle *loghandle,
1724 struct llog_ctxt *ctxt;
1725 struct dt_object *o, *llog_dir = NULL;
1730 ctxt = loghandle->lgh_ctxt;
1733 o = loghandle->lgh_obj;
1736 if (loghandle->lgh_name) {
1737 llog_dir = llog_osd_dir_get(env, ctxt);
1738 if (IS_ERR(llog_dir))
1739 RETURN(PTR_ERR(llog_dir));
1741 rc = dt_declare_delete(env, llog_dir,
1742 (struct dt_key *)loghandle->lgh_name,
1748 rc = dt_declare_ref_del(env, o, th);
1752 rc = dt_declare_destroy(env, o, th);
1756 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1757 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1763 if (!(IS_ERR_OR_NULL(llog_dir)))
1764 dt_object_put(env, llog_dir);
1771 * Implementation of the llog_operations::lop_destroy
1773 * This function destroys the llog and deletes also entry in the
1774 * llog directory in case of named llog. Llog should be opened prior that.
1775 * Destroy method is not part of external transaction and does everything
1778 * \param[in] env execution environment
1779 * \param[in] loghandle llog handle of the current llog
1781 * \retval 0 on successful destroy
1782 * \retval negative value on error
1784 static int llog_osd_destroy(const struct lu_env *env,
1785 struct llog_handle *loghandle, struct thandle *th)
1787 struct llog_ctxt *ctxt;
1788 struct dt_object *o, *llog_dir = NULL;
1793 ctxt = loghandle->lgh_ctxt;
1794 LASSERT(ctxt != NULL);
1796 o = loghandle->lgh_obj;
1799 dt_write_lock(env, o, 0);
1800 if (!llog_osd_exist(loghandle))
1801 GOTO(out_unlock, rc = 0);
1803 if (loghandle->lgh_name) {
1804 llog_dir = llog_osd_dir_get(env, ctxt);
1805 if (IS_ERR(llog_dir))
1806 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1808 dt_read_lock(env, llog_dir, 0);
1809 rc = dt_delete(env, llog_dir,
1810 (struct dt_key *)loghandle->lgh_name,
1812 dt_read_unlock(env, llog_dir);
1814 CERROR("%s: can't remove llog %s: rc = %d\n",
1815 o->do_lu.lo_dev->ld_obd->obd_name,
1816 loghandle->lgh_name, rc);
1817 GOTO(out_unlock, rc);
1821 dt_ref_del(env, o, th);
1822 rc = dt_destroy(env, o, th);
1824 GOTO(out_unlock, rc);
1826 loghandle->lgh_destroyed = true;
1827 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1828 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1830 GOTO(out_unlock, rc);
1834 dt_write_unlock(env, o);
1835 if (!(IS_ERR_OR_NULL(llog_dir)))
1836 dt_object_put(env, llog_dir);
1841 * Implementation of the llog_operations::lop_setup
1843 * This function setup the llog on local storage.
1845 * \param[in] env execution environment
1846 * \param[in] obd obd device the llog belongs to
1847 * \param[in] olg the llog group, it is always zero group now.
1848 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1849 * Every new llog type have to use own index.
1850 * \param[in] disk_obd the storage obd, where llog is stored.
1852 * \retval 0 on successful llog setup
1853 * \retval negative value on error
1855 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1856 struct obd_llog_group *olg, int ctxt_idx,
1857 struct obd_device *disk_obd)
1859 struct llog_thread_info *lgi = llog_info(env);
1860 struct llog_ctxt *ctxt;
1865 LASSERT(olg->olg_ctxts[ctxt_idx]);
1867 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1870 if (disk_obd == NULL)
1873 /* initialize data allowing to generate new fids,
1874 * literally we need a sequece */
1875 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1876 lgi->lgi_fid.f_oid = 1;
1877 lgi->lgi_fid.f_ver = 0;
1878 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1880 &ctxt->loc_los_nameless);
1884 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1885 lgi->lgi_fid.f_oid = 1;
1886 lgi->lgi_fid.f_ver = 0;
1887 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1889 &ctxt->loc_los_named);
1891 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1892 ctxt->loc_los_nameless = NULL;
1898 llog_ctxt_put(ctxt);
1903 * Implementation of the llog_operations::lop_cleanup
1905 * This function cleanups the llog on local storage.
1907 * \param[in] env execution environment
1908 * \param[in] ctxt the llog context
1912 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1914 if (ctxt->loc_los_nameless != NULL) {
1915 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1916 ctxt->loc_los_nameless = NULL;
1919 if (ctxt->loc_los_named != NULL) {
1920 local_oid_storage_fini(env, ctxt->loc_los_named);
1921 ctxt->loc_los_named = NULL;
1927 struct llog_operations llog_osd_ops = {
1928 .lop_next_block = llog_osd_next_block,
1929 .lop_prev_block = llog_osd_prev_block,
1930 .lop_read_header = llog_osd_read_header,
1931 .lop_declare_destroy = llog_osd_declare_destroy,
1932 .lop_destroy = llog_osd_destroy,
1933 .lop_setup = llog_osd_setup,
1934 .lop_cleanup = llog_osd_cleanup,
1935 .lop_open = llog_osd_open,
1936 .lop_exist = llog_osd_exist,
1937 .lop_declare_create = llog_osd_declare_create,
1938 .lop_create = llog_osd_create,
1939 .lop_declare_write_rec = llog_osd_declare_write_rec,
1940 .lop_write_rec = llog_osd_write_rec,
1941 .lop_close = llog_osd_close,
1943 EXPORT_SYMBOL(llog_osd_ops);
1945 struct llog_operations llog_common_cat_ops = {
1946 .lop_next_block = llog_osd_next_block,
1947 .lop_prev_block = llog_osd_prev_block,
1948 .lop_read_header = llog_osd_read_header,
1949 .lop_declare_destroy = llog_osd_declare_destroy,
1950 .lop_destroy = llog_osd_destroy,
1951 .lop_setup = llog_osd_setup,
1952 .lop_cleanup = llog_osd_cleanup,
1953 .lop_open = llog_osd_open,
1954 .lop_exist = llog_osd_exist,
1955 .lop_declare_create = llog_osd_declare_create,
1956 .lop_create = llog_osd_create,
1957 .lop_declare_write_rec = llog_osd_declare_write_rec,
1958 .lop_write_rec = llog_osd_write_rec,
1959 .lop_close = llog_osd_close,
1960 .lop_add = llog_cat_add_rec,
1961 .lop_declare_add = llog_cat_declare_add_rec,
1963 EXPORT_SYMBOL(llog_common_cat_ops);
1966 * Read the special file which contains the list of llog catalogs IDs
1968 * This function reads the CATALOGS file which contains the array of llog
1969 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1970 * by OST/MDT number.
1972 * \param[in] env execution environment
1973 * \param[in] d corresponding storage device
1974 * \param[in] idx position to start from, usually OST/MDT index
1975 * \param[in] count how many catalog IDs to read
1976 * \param[out] idarray the buffer for the data. If it is NULL then
1977 * function returns just number of catalog IDs
1979 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1981 * \retval 0 on successful read of catalog IDs
1982 * \retval negative value on error
1983 * \retval positive value which is number of records in
1984 * the file if \a idarray is NULL
1986 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1987 int idx, int count, struct llog_catid *idarray,
1988 const struct lu_fid *fid)
1990 struct llog_thread_info *lgi = llog_info(env);
1991 struct dt_object *o = NULL;
1999 size = sizeof(*idarray) * count;
2000 lgi->lgi_off = idx * sizeof(*idarray);
2002 lgi->lgi_fid = *fid;
2003 o = dt_locate(env, d, &lgi->lgi_fid);
2007 if (!dt_object_exists(o)) {
2008 th = dt_trans_create(env, d);
2010 GOTO(out, rc = PTR_ERR(th));
2012 lgi->lgi_attr.la_valid = LA_MODE;
2013 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2014 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2016 th->th_wait_submit = 1;
2017 /* Make the llog object creation synchronization, so
2018 * it will be reliable to the reference, especially
2019 * for remote reference */
2022 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2025 GOTO(out_trans, rc);
2027 rc = dt_trans_start_local(env, d, th);
2029 GOTO(out_trans, rc);
2031 dt_write_lock(env, o, 0);
2032 if (!dt_object_exists(o))
2033 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2035 dt_write_unlock(env, o);
2037 dt_trans_stop(env, d, th);
2042 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2046 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2047 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2048 o->do_lu.lo_dev->ld_obd->obd_name,
2049 lgi->lgi_attr.la_mode);
2050 GOTO(out, rc = -ENOENT);
2053 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2054 (int)lgi->lgi_attr.la_size, size);
2056 /* return just number of llogs */
2057 if (idarray == NULL) {
2058 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2062 /* read for new ost index or for empty file */
2063 memset(idarray, 0, size);
2064 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2066 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2067 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2069 lgi->lgi_buf.lb_buf = idarray;
2070 lgi->lgi_buf.lb_len = size;
2071 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2072 /* -EFAULT means the llog is a sparse file. This is not an error
2073 * after arbitrary OST index is supported. */
2074 if (rc < 0 && rc != -EFAULT) {
2075 CERROR("%s: error reading CATALOGS: rc = %d\n",
2076 o->do_lu.lo_dev->ld_obd->obd_name, rc);
2082 dt_object_put(env, o);
2085 EXPORT_SYMBOL(llog_osd_get_cat_list);
2088 * Write the special file which contains the list of llog catalogs IDs
2090 * This function writes the CATALOG file which contains the array of llog
2091 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2094 * \param[in] env execution environment
2095 * \param[in] d corresponding storage device
2096 * \param[in] idx position to start from, usually OST/MDT index
2097 * \param[in] count how many catalog IDs to write
2098 * \param[out] idarray the buffer with the data to write.
2099 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2101 * \retval 0 on successful write of catalog IDs
2102 * \retval negative value on error
2104 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2105 int idx, int count, struct llog_catid *idarray,
2106 const struct lu_fid *fid)
2108 struct llog_thread_info *lgi = llog_info(env);
2109 struct dt_object *o = NULL;
2118 size = sizeof(*idarray) * count;
2119 lgi->lgi_off = idx * sizeof(*idarray);
2120 lgi->lgi_fid = *fid;
2122 o = dt_locate(env, d, &lgi->lgi_fid);
2126 if (!dt_object_exists(o))
2127 GOTO(out, rc = -ENOENT);
2129 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2133 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2134 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2135 o->do_lu.lo_dev->ld_obd->obd_name,
2136 lgi->lgi_attr.la_mode);
2137 GOTO(out, rc = -ENOENT);
2140 th = dt_trans_create(env, d);
2142 GOTO(out, rc = PTR_ERR(th));
2144 lgi->lgi_buf.lb_len = size;
2145 lgi->lgi_buf.lb_buf = idarray;
2146 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2148 GOTO(out_trans, rc);
2150 /* For update log, this happens during initialization,
2151 * see lod_sub_prep_llog(), and we need make sure catlog
2152 * file ID is written to catlist file(committed) before
2153 * cross-MDT operation write update records to catlog FILE,
2154 * otherwise, during failover these update records might
2156 if (fid_is_update_log(fid))
2159 rc = dt_trans_start_local(env, d, th);
2161 GOTO(out_trans, rc);
2163 th->th_wait_submit = 1;
2165 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2167 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2170 dt_trans_stop(env, d, th);
2172 dt_object_put(env, o);
2175 EXPORT_SYMBOL(llog_osd_put_cat_list);