4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 only,
8 * as published by the Free Software Foundation.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License version 2 for more details (a copy is included
14 * in the LICENSE file that accompanied this code).
16 * You should have received a copy of the GNU General Public License
17 * version 2 along with this program; If not, see
18 * http://www.gnu.org/licenses/gpl-2.0.html
23 * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
24 * Use is subject to license terms.
26 * Copyright (c) 2012, 2015, Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * lustre/obdclass/llog_osd.c
35 * Low level llog routines on top of OSD API
37 * This file provides set of methods for llog operations on top of
38 * dt_device. It contains all supported llog_operations interfaces and
39 * supplimental functions.
41 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42 * Author: Mikhail Pershin <mike.pershin@intel.com>
45 #define DEBUG_SUBSYSTEM S_LOG
47 #include <dt_object.h>
48 #include <llog_swab.h>
49 #include <lustre_fid.h>
51 #include <obd_class.h>
53 #include "llog_internal.h"
54 #include "local_storage.h"
57 * Implementation of the llog_operations::lop_declare_create
59 * This function is a wrapper over local_storage API function
60 * local_object_declare_create().
62 * \param[in] env execution environment
63 * \param[in] los local_storage for bottom storage device
64 * \param[in] o dt_object to create
65 * \param[in] th current transaction handle
67 * \retval 0 on successful declaration of the new object
68 * \retval negative error if declaration was failed
70 static int llog_osd_declare_new_object(const struct lu_env *env,
71 struct local_oid_storage *los,
75 struct llog_thread_info *lgi = llog_info(env);
77 lgi->lgi_attr.la_valid = LA_MODE;
78 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
79 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
81 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
86 * Implementation of the llog_operations::lop_create
88 * This function is a wrapper over local_storage API function
89 * local_object_create().
91 * \param[in] env execution environment
92 * \param[in] los local_storage for bottom storage device
93 * \param[in] o dt_object to create
94 * \param[in] th current transaction handle
96 * \retval 0 on successful creation of the new object
97 * \retval negative error if creation was failed
99 static int llog_osd_create_new_object(const struct lu_env *env,
100 struct local_oid_storage *los,
104 struct llog_thread_info *lgi = llog_info(env);
106 lgi->lgi_attr.la_valid = LA_MODE;
107 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
108 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
110 return local_object_create(env, los, o, &lgi->lgi_attr,
115 * Implementation of the llog_operations::lop_exist
117 * This function checks that llog exists on storage.
119 * \param[in] handle llog handle of the current llog
121 * \retval true if llog object exists and is not just destroyed
122 * \retval false if llog doesn't exist or just destroyed
124 static int llog_osd_exist(struct llog_handle *handle)
126 LASSERT(handle->lgh_obj);
127 return dt_object_exists(handle->lgh_obj) &&
128 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header);
132 * Write a padding record to the llog
134 * This function writes a padding record to the end of llog. That may
135 * be needed if llog contains records of variable size, e.g. config logs
137 * The padding record just aligns llog to the llog chunk_size boundary if
138 * the current record doesn't fit in the remaining space.
140 * It allocates full length to avoid two separate writes for header and tail.
141 * Such 2-steps scheme needs extra protection and complex error handling.
143 * \param[in] env execution environment
144 * \param[in] o dt_object to create
145 * \param[in,out] off pointer to the padding start offset
146 * \param[in] len padding length
147 * \param[in] index index of the padding record in a llog
148 * \param[in] th current transaction handle
150 * \retval 0 on successful padding write
151 * \retval negative error if write failed
153 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
154 loff_t *off, int len, int index, struct thandle *th)
156 struct llog_thread_info *lgi = llog_info(env);
157 struct llog_rec_hdr *rec;
158 struct llog_rec_tail *tail;
165 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
172 rec->lrh_index = index;
173 rec->lrh_type = LLOG_PAD_MAGIC;
175 tail = rec_tail(rec);
177 tail->lrt_index = index;
179 lgi->lgi_buf.lb_buf = rec;
180 lgi->lgi_buf.lb_len = len;
181 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
183 CERROR("%s: error writing padding record: rc = %d\n",
184 o->do_lu.lo_dev->ld_obd->obd_name, rc);
191 * Implementation of the llog_operations::lop_read_header
193 * This function reads the current llog header from the bottom storage
196 * \param[in] env execution environment
197 * \param[in] handle llog handle of the current llog
199 * \retval 0 on successful header read
200 * \retval negative error if read failed
202 static int llog_osd_read_header(const struct lu_env *env,
203 struct llog_handle *handle)
205 struct llog_rec_hdr *llh_hdr;
207 struct llog_thread_info *lgi;
208 enum llog_flag flags;
216 lgi = llog_info(env);
218 rc = dt_attr_get(env, o, &lgi->lgi_attr);
222 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
224 if (lgi->lgi_attr.la_size == 0) {
225 CDEBUG(D_HA, "not reading header from 0-byte log\n");
229 flags = handle->lgh_hdr->llh_flags;
232 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
233 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
234 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
235 llh_hdr = &handle->lgh_hdr->llh_hdr;
236 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
237 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
238 o->do_lu.lo_dev->ld_obd->obd_name,
239 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
248 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
249 lustre_swab_llog_hdr(handle->lgh_hdr);
251 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
252 CERROR("%s: bad log %s "DFID" header magic: %#x "
253 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
254 handle->lgh_name ? handle->lgh_name : "",
255 PFID(lu_object_fid(&o->do_lu)),
256 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
258 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
259 llh_hdr->lrh_len > handle->lgh_hdr_size) {
260 CERROR("%s: incorrectly sized log %s "DFID" header: "
261 "%#x (expected at least %#x)\n"
262 "you may need to re-run lconf --write_conf.\n",
263 o->do_lu.lo_dev->ld_obd->obd_name,
264 handle->lgh_name ? handle->lgh_name : "",
265 PFID(lu_object_fid(&o->do_lu)),
266 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
268 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
269 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
270 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
272 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
274 o->do_lu.lo_dev->ld_obd->obd_name,
275 handle->lgh_name ? handle->lgh_name : "",
276 PFID(lu_object_fid(&o->do_lu)),
277 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
281 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
282 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
283 handle->lgh_write_offset = lgi->lgi_attr.la_size;
289 * Implementation of the llog_operations::lop_declare_write
291 * This function declares the new record write.
293 * \param[in] env execution environment
294 * \param[in] loghandle llog handle of the current llog
295 * \param[in] rec llog record header. This is a real header of the full
296 * llog record to write. This is the beginning of buffer
297 * to write, the length of buffer is stored in
299 * \param[in] idx index of the llog record. If \a idx == -1 then this is
300 * append case, otherwise \a idx is the index of record
302 * \param[in] th current transaction handle
304 * \retval 0 on successful declaration
305 * \retval negative error if declaration failed
307 static int llog_osd_declare_write_rec(const struct lu_env *env,
308 struct llog_handle *loghandle,
309 struct llog_rec_hdr *rec,
310 int idx, struct thandle *th)
312 struct llog_thread_info *lgi = llog_info(env);
323 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
325 o = loghandle->lgh_obj;
328 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
329 lgi->lgi_buf.lb_len = chunk_size;
330 lgi->lgi_buf.lb_buf = NULL;
331 /* each time we update header */
332 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
334 if (rc || idx == 0) /* if error or just header */
338 * the pad record can be inserted so take into account double
341 lgi->lgi_buf.lb_len = chunk_size * 2;
342 lgi->lgi_buf.lb_buf = NULL;
343 /* XXX: implement declared window or multi-chunks approach */
344 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
350 * Implementation of the llog_operations::lop_write
352 * This function writes the new record in the llog or modify the existed one.
354 * \param[in] env execution environment
355 * \param[in] loghandle llog handle of the current llog
356 * \param[in] rec llog record header. This is a real header of
357 * the full llog record to write. This is
358 * the beginning of buffer to write, the length
359 * of buffer is stored in \a rec::lrh_len
360 * \param[out] reccookie pointer to the cookie to return back if needed.
361 * It is used for further cancel of this llog
363 * \param[in] idx index of the llog record. If \a idx == -1 then
364 * this is append case, otherwise \a idx is
365 * the index of record to modify
366 * \param[in] th current transaction handle
368 * \retval 0 on successful write && \a reccookie == NULL
369 * 1 on successful write && \a reccookie != NULL
370 * \retval negative error if write failed
372 static int llog_osd_write_rec(const struct lu_env *env,
373 struct llog_handle *loghandle,
374 struct llog_rec_hdr *rec,
375 struct llog_cookie *reccookie,
376 int idx, struct thandle *th)
378 struct llog_thread_info *lgi = llog_info(env);
379 struct llog_log_hdr *llh;
380 int reclen = rec->lrh_len;
382 struct llog_rec_tail *lrt;
387 __u64 orig_write_offset;
390 llh = loghandle->lgh_hdr;
391 o = loghandle->lgh_obj;
393 chunk_size = llh->llh_hdr.lrh_len;
394 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
395 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
397 if (!llog_osd_exist(loghandle))
400 /* record length should not bigger than */
401 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
404 /* sanity check for fixed-records llog */
405 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
406 LASSERT(llh->llh_size != 0);
407 LASSERT(llh->llh_size == reclen);
410 rc = dt_attr_get(env, o, &lgi->lgi_attr);
415 * The modification case.
416 * If idx set then the record with that index must be modified.
417 * There are three cases possible:
418 * 1) the common case is the llog header update (idx == 0)
419 * 2) the llog record modification during llog process.
420 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
421 * In that case the \a loghandle::lgh_cur_offset
422 * 3) otherwise this is assumed that llog consist of records of
423 * fixed size, i.e. catalog. The llog header must has llh_size
424 * field equal to record size. The record offset is calculated
425 * just by /a idx value
427 * During modification we don't need extra header update because
428 * the bitmap and record count are not changed. The record header
429 * and tail remains the same too.
431 if (idx != LLOG_NEXT_IDX) {
432 /* llog can be empty only when first record is being written */
433 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
435 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
436 CERROR("%s: modify unset record %u\n",
437 o->do_lu.lo_dev->ld_obd->obd_name, idx);
441 if (idx != rec->lrh_index) {
442 CERROR("%s: modify index mismatch %d %u\n",
443 o->do_lu.lo_dev->ld_obd->obd_name, idx,
448 if (idx == LLOG_HEADER_IDX) {
449 /* llog header update */
450 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
454 /* If it does not indicate the bitmap index
455 * (reccookie == NULL), then it means update
456 * the whole update header. Otherwise only
457 * update header and bits needs to be updated,
458 * and in DNE cases, it will signaficantly
459 * shrink the RPC size.
460 * see distribute_txn_cancel_records()*/
461 if (reccookie == NULL) {
462 lgi->lgi_buf.lb_len = reclen;
463 lgi->lgi_buf.lb_buf = rec;
464 rc = dt_record_write(env, o, &lgi->lgi_buf,
469 /* update the header */
470 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
471 lgi->lgi_buf.lb_buf = llh;
472 rc = dt_record_write(env, o, &lgi->lgi_buf,
477 /* update the bitmap */
478 index = reccookie->lgc_index;
479 lgi->lgi_off = llh->llh_bitmap_offset +
480 (index / (sizeof(*bitmap) * 8)) *
482 lgi->lgi_buf.lb_len = sizeof(*bitmap);
483 lgi->lgi_buf.lb_buf =
484 &bitmap[index/(sizeof(*bitmap)*8)];
485 rc = dt_record_write(env, o, &lgi->lgi_buf,
489 } else if (loghandle->lgh_cur_idx > 0) {
491 * The lgh_cur_offset can be used only if index is
494 if (idx != loghandle->lgh_cur_idx) {
495 CERROR("%s: modify index mismatch %d %d\n",
496 o->do_lu.lo_dev->ld_obd->obd_name, idx,
497 loghandle->lgh_cur_idx);
501 lgi->lgi_off = loghandle->lgh_cur_offset;
502 CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
503 "len:%u offset %llu\n",
504 POSTID(&loghandle->lgh_id.lgl_oi), idx,
505 rec->lrh_len, (long long)lgi->lgi_off);
506 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
507 lgi->lgi_off = llh->llh_hdr.lrh_len +
510 /* This can be result of lgh_cur_idx is not set during
511 * llog processing or llh_size is not set to proper
512 * record size for fixed records llog. Therefore it is
513 * impossible to get record offset. */
514 CERROR("%s: can't get record offset, idx:%d, "
515 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
520 /* update only data, header and tail remain the same */
521 lgi->lgi_off += sizeof(struct llog_rec_hdr);
522 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
523 lgi->lgi_buf.lb_buf = REC_DATA(rec);
524 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
525 if (rc == 0 && reccookie) {
526 reccookie->lgc_lgl = loghandle->lgh_id;
527 reccookie->lgc_index = idx;
535 * The most common case of using llog. The new index is assigned to
536 * the new record, new bit is set in llog bitmap and llog count is
539 * Make sure that records don't cross a chunk boundary, so we can
540 * process them page-at-a-time if needed. If it will cross a chunk
541 * boundary, write in a fake (but referenced) entry to pad the chunk.
545 /* simulate ENOSPC when new plain llog is being added to the
547 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
548 llh->llh_flags & LLOG_F_IS_CAT)
551 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
552 orig_last_idx = loghandle->lgh_last_idx;
553 orig_write_offset = loghandle->lgh_write_offset;
554 lgi->lgi_off = lgi->lgi_attr.la_size;
555 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
556 /* NOTE: padding is a record, but no bit is set */
557 if (left != 0 && left != reclen &&
558 left < (reclen + LLOG_MIN_REC_SIZE)) {
559 index = loghandle->lgh_last_idx + 1;
560 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
564 if (dt_object_remote(o))
565 loghandle->lgh_write_offset = lgi->lgi_off;
567 loghandle->lgh_last_idx++; /* for pad rec */
569 /* if it's the last idx in log file, then return -ENOSPC
570 * or wrap around if a catalog */
571 if (llog_is_full(loghandle) ||
572 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
573 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
574 loghandle->lgh_last_idx >= cfs_fail_val)) {
575 if (llh->llh_flags & LLOG_F_IS_CAT)
576 loghandle->lgh_last_idx = 0;
581 /* increment the last_idx along with llh_tail index, they should
582 * be equal for a llog lifetime */
583 loghandle->lgh_last_idx++;
584 index = loghandle->lgh_last_idx;
585 LLOG_HDR_TAIL(llh)->lrt_index = index;
587 * NB: the caller should make sure only 1 process access
588 * the lgh_last_idx, e.g. append should be exclusive.
589 * Otherwise it might hit the assert.
591 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
592 rec->lrh_index = index;
594 lrt->lrt_len = rec->lrh_len;
595 lrt->lrt_index = rec->lrh_index;
597 /* the lgh_hdr_mutex protects llog header data from concurrent
598 * update/cancel, the llh_count and llh_bitmap are protected */
599 mutex_lock(&loghandle->lgh_hdr_mutex);
600 if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
601 CERROR("%s: index %u already set in log bitmap\n",
602 o->do_lu.lo_dev->ld_obd->obd_name, index);
603 mutex_unlock(&loghandle->lgh_hdr_mutex);
604 LBUG(); /* should never happen */
608 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
609 /* Update the minimum size of the llog record */
610 if (llh->llh_size == 0)
611 llh->llh_size = reclen;
612 else if (reclen < llh->llh_size)
613 llh->llh_size = reclen;
616 if (lgi->lgi_attr.la_size == 0) {
618 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
619 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
620 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
622 GOTO(out_unlock, rc);
624 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
626 /* Note: If this is not initialization (size == 0), then do not
627 * write the whole header (8k bytes), only update header/tail
628 * and bits needs to be updated. Because this update might be
629 * part of cross-MDT operation, which needs to write these
630 * updates into the update log(32KB limit) and also pack inside
631 * the RPC (1MB limit), if we write 8K for each operation, which
632 * will cost a lot space, and keep us adding more updates to one
635 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
636 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
637 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
639 GOTO(out_unlock, rc);
641 lgi->lgi_off = llh->llh_bitmap_offset +
642 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
643 lgi->lgi_buf.lb_len = sizeof(*bitmap);
644 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
645 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
647 GOTO(out_unlock, rc);
649 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
651 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
652 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
653 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
655 GOTO(out_unlock, rc);
659 /* unlock here for remote object */
660 mutex_unlock(&loghandle->lgh_hdr_mutex);
664 /* computed index can be used to determine offset for fixed-size
665 * records. This also allows to handle Catalog wrap around case */
666 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
667 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
668 } else if (dt_object_remote(o)) {
669 lgi->lgi_off = max_t(__u64, loghandle->lgh_write_offset,
672 rc = dt_attr_get(env, o, &lgi->lgi_attr);
676 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
677 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
681 lgi->lgi_buf.lb_len = reclen;
682 lgi->lgi_buf.lb_buf = rec;
683 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
687 if (dt_object_remote(o))
688 loghandle->lgh_write_offset = lgi->lgi_off;
690 CDEBUG(D_HA, "added record "DFID": idx: %u, %u off"LPU64"\n",
691 PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
693 if (reccookie != NULL) {
694 reccookie->lgc_lgl = loghandle->lgh_id;
695 reccookie->lgc_index = index;
696 if ((rec->lrh_type == MDS_UNLINK_REC) ||
697 (rec->lrh_type == MDS_SETATTR64_REC))
698 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
699 else if (rec->lrh_type == OST_SZ_REC)
700 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
702 reccookie->lgc_subsys = -1;
707 /* cleanup llog for error case */
708 mutex_lock(&loghandle->lgh_hdr_mutex);
709 ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
711 mutex_unlock(&loghandle->lgh_hdr_mutex);
713 /* restore llog last_idx */
714 if (dt_object_remote(o)) {
715 loghandle->lgh_last_idx = orig_last_idx;
716 loghandle->lgh_write_offset = orig_write_offset;
717 } else if (--loghandle->lgh_last_idx == 0 &&
718 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
719 /* catalog had just wrap-around case */
720 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
723 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
729 * We can skip reading at least as many log blocks as the number of
730 * minimum sized log records we are skipping. If it turns out
731 * that we are not far enough along the log (because the
732 * actual records are larger than minimum size) we just skip
735 * Note: in llog_process_thread, it will use bitmap offset as
736 * the index to locate the record, which also includs some pad
737 * records, whose record size is very small, and it also does not
738 * consider pad record when recording minimum record size (otherwise
739 * min_record size might be too small), so in some rare cases,
740 * it might skip too much record for @goal, see llog_osd_next_block().
742 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
743 * as the min record size to skip over, usually because in the previous
744 * try, it skip too much record, see loog_osd_next(prev)_block().
746 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
747 int curr, int goal, __u32 chunk_size,
750 struct llog_log_hdr *llh = lgh->lgh_hdr;
752 /* Goal should not bigger than the record count */
753 if (goal > lgh->lgh_last_idx)
754 goal = lgh->lgh_last_idx;
757 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
758 *off = chunk_size + (goal - 1) * llh->llh_size;
760 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
762 if (llh->llh_size > 0 && !force_mini_rec)
763 min_rec_size = llh->llh_size;
765 *off = *off + (goal - curr - 1) * min_rec_size;
768 /* always align with lower chunk boundary*/
769 *off &= ~(chunk_size - 1);
773 * Remove optional fields that the client doesn't expect.
774 * This is typically in order to ensure compatibility with older clients.
775 * It is assumed that since we exclusively remove fields, the block will be
776 * big enough to handle the remapped records. It is also assumed that records
777 * of a block have the same format (i.e.: the same features enabled).
779 * \param[in,out] hdr Header of the block of records to remap.
780 * \param[in,out] last_hdr Last header, don't read past this point.
781 * \param[in] flags Flags describing the fields to keep.
783 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
784 struct llog_rec_hdr *last_hdr,
785 enum changelog_rec_flags flags)
787 if (hdr->lrh_type != CHANGELOG_REC)
791 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
793 changelog_remap_rec(rec, rec->cr_flags & flags);
794 hdr = llog_rec_hdr_next(hdr);
795 } while ((char *)hdr <= (char *)last_hdr);
799 * Implementation of the llog_operations::lop_next_block
801 * This function finds the the next llog block to return which contains
802 * record with required index. It is main part of llog processing.
804 * \param[in] env execution environment
805 * \param[in] loghandle llog handle of the current llog
806 * \param[in,out] cur_idx index preceeding cur_offset
807 * \param[in] next_idx target index to find
808 * \param[in,out] cur_offset furtherst point read in the file
809 * \param[in] buf pointer to data buffer to fill
810 * \param[in] len required len to read, it is
811 * usually llog chunk_size.
813 * \retval 0 on successful buffer read
814 * \retval negative value on error
816 static int llog_osd_next_block(const struct lu_env *env,
817 struct llog_handle *loghandle, int *cur_idx,
818 int next_idx, __u64 *cur_offset, void *buf,
821 struct llog_thread_info *lgi = llog_info(env);
823 struct dt_device *dt;
826 int last_idx = *cur_idx;
827 __u64 last_offset = *cur_offset;
828 bool force_mini_rec = false;
835 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
836 if (len == 0 || len & (chunk_size - 1))
840 LASSERT(loghandle->lgh_ctxt);
842 o = loghandle->lgh_obj;
844 LASSERT(dt_object_exists(o));
845 dt = lu2dt_dev(o->do_lu.lo_dev);
848 rc = dt_attr_get(env, o, &lgi->lgi_attr);
852 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off"
853 LPU64"), size %llu\n", next_idx, *cur_idx,
854 *cur_offset, lgi->lgi_attr.la_size);
856 while (*cur_offset < lgi->lgi_attr.la_size) {
857 struct llog_rec_hdr *rec, *last_rec;
858 struct llog_rec_tail *tail;
860 llog_skip_over(loghandle, cur_offset, *cur_idx,
861 next_idx, chunk_size, force_mini_rec);
863 /* read up to next llog chunk_size block */
864 lgi->lgi_buf.lb_len = chunk_size -
865 (*cur_offset & (chunk_size - 1));
866 lgi->lgi_buf.lb_buf = buf;
868 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
870 if (rc == -EBADR && !force_mini_rec)
873 CERROR("%s: can't read llog block from log "DFID
874 " offset "LPU64": rc = %d\n",
875 o->do_lu.lo_dev->ld_obd->obd_name,
876 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
882 /* signal the end of the valid buffer to
884 memset(buf + rc, 0, len - rc);
887 if (rc == 0) { /* end of file, nothing to do */
893 if (rc < sizeof(*tail)) {
897 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
899 o->do_lu.lo_dev->ld_obd->obd_name,
900 POSTID(&loghandle->lgh_id.lgl_oi),
901 loghandle->lgh_id.lgl_ogen, *cur_offset);
902 GOTO(out, rc = -EINVAL);
906 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
907 lustre_swab_llog_rec(rec);
909 tail = (struct llog_rec_tail *)((char *)buf + rc -
910 sizeof(struct llog_rec_tail));
911 /* get the last record in block */
912 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
915 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
916 lustre_swab_llog_rec(last_rec);
918 if (last_rec->lrh_index != tail->lrt_index) {
919 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
920 "offset "LPU64" last_rec idx %u tail idx %u\n",
921 o->do_lu.lo_dev->ld_obd->obd_name,
922 POSTID(&loghandle->lgh_id.lgl_oi),
923 loghandle->lgh_id.lgl_ogen, *cur_offset,
924 last_rec->lrh_index, tail->lrt_index);
925 GOTO(out, rc = -EINVAL);
928 *cur_idx = tail->lrt_index;
930 /* this shouldn't happen */
931 if (tail->lrt_index == 0) {
932 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
933 "offset "LPU64" bytes %d\n",
934 o->do_lu.lo_dev->ld_obd->obd_name,
935 POSTID(&loghandle->lgh_id.lgl_oi),
936 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
937 GOTO(out, rc = -EINVAL);
939 if (tail->lrt_index < next_idx) {
941 last_offset = *cur_offset;
945 /* sanity check that the start of the new buffer is no farther
946 * than the record that we wanted. This shouldn't happen. */
947 if (rec->lrh_index > next_idx) {
948 if (!force_mini_rec && next_idx > last_idx)
951 CERROR("%s: missed desired record? %u > %u\n",
952 o->do_lu.lo_dev->ld_obd->obd_name,
953 rec->lrh_index, next_idx);
954 GOTO(out, rc = -ENOENT);
957 /* Trim unsupported extensions for compat w/ older clients */
958 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
959 changelog_block_trim_ext(rec, last_rec,
960 CLF_VERSION | CLF_RENAME);
965 /* Note: because there are some pad records in the
966 * llog, so llog_skip_over() might skip too much
967 * records, let's try skip again with minimum record */
968 force_mini_rec = true;
969 *cur_offset = last_offset;
972 GOTO(out, rc = -EIO);
978 * Implementation of the llog_operations::lop_prev_block
980 * This function finds the llog block to return which contains
981 * record with required index but in reverse order - from end of llog
983 * It is main part of reverse llog processing.
985 * \param[in] env execution environment
986 * \param[in] loghandle llog handle of the current llog
987 * \param[in] prev_idx target index to find
988 * \param[in] buf pointer to data buffer to fill
989 * \param[in] len required len to read, it is llog_chunk_size usually.
991 * \retval 0 on successful buffer read
992 * \retval negative value on error
994 static int llog_osd_prev_block(const struct lu_env *env,
995 struct llog_handle *loghandle,
996 int prev_idx, void *buf, int len)
998 struct llog_thread_info *lgi = llog_info(env);
1000 struct dt_device *dt;
1007 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1008 if (len == 0 || len & (chunk_size - 1))
1011 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1014 LASSERT(loghandle->lgh_ctxt);
1016 o = loghandle->lgh_obj;
1018 LASSERT(dt_object_exists(o));
1019 dt = lu2dt_dev(o->do_lu.lo_dev);
1022 /* Let's only use mini record size for previous block read
1024 cur_offset = chunk_size;
1025 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1028 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1032 while (cur_offset < lgi->lgi_attr.la_size) {
1033 struct llog_rec_hdr *rec, *last_rec;
1034 struct llog_rec_tail *tail;
1036 lgi->lgi_buf.lb_len = len;
1037 lgi->lgi_buf.lb_buf = buf;
1038 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1040 CERROR("%s: can't read llog block from log "DFID
1041 " offset "LPU64": rc = %d\n",
1042 o->do_lu.lo_dev->ld_obd->obd_name,
1043 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1047 if (rc == 0) /* end of file, nothing to do */
1050 if (rc < sizeof(*tail)) {
1051 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
1053 o->do_lu.lo_dev->ld_obd->obd_name,
1054 POSTID(&loghandle->lgh_id.lgl_oi),
1055 loghandle->lgh_id.lgl_ogen, cur_offset);
1056 GOTO(out, rc = -EINVAL);
1060 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1061 lustre_swab_llog_rec(rec);
1063 tail = (struct llog_rec_tail *)((char *)buf + rc -
1064 sizeof(struct llog_rec_tail));
1065 /* get the last record in block */
1066 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1067 le32_to_cpu(tail->lrt_len));
1069 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1070 lustre_swab_llog_rec(last_rec);
1071 LASSERT(last_rec->lrh_index == tail->lrt_index);
1073 /* this shouldn't happen */
1074 if (tail->lrt_index == 0) {
1075 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
1077 o->do_lu.lo_dev->ld_obd->obd_name,
1078 POSTID(&loghandle->lgh_id.lgl_oi),
1079 loghandle->lgh_id.lgl_ogen, cur_offset);
1080 GOTO(out, rc = -EINVAL);
1082 if (tail->lrt_index < prev_idx)
1085 /* sanity check that the start of the new buffer is no farther
1086 * than the record that we wanted. This shouldn't happen. */
1087 if (rec->lrh_index > prev_idx) {
1088 CERROR("%s: missed desired record? %u > %u\n",
1089 o->do_lu.lo_dev->ld_obd->obd_name,
1090 rec->lrh_index, prev_idx);
1091 GOTO(out, rc = -ENOENT);
1094 /* Trim unsupported extensions for compat w/ older clients */
1095 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
1096 changelog_block_trim_ext(rec, last_rec,
1097 CLF_VERSION | CLF_RENAME);
1101 GOTO(out, rc = -EIO);
1107 * This is helper function to get llog directory object. It is used by named
1108 * llog operations to find/insert/delete llog entry from llog directory.
1110 * \param[in] env execution environment
1111 * \param[in] ctxt llog context
1113 * \retval dt_object of llog directory
1114 * \retval ERR_PTR of negative value on error
1116 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1117 struct llog_ctxt *ctxt)
1119 struct dt_device *dt;
1120 struct dt_thread_info *dti = dt_info(env);
1121 struct dt_object *dir;
1124 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1125 if (ctxt->loc_dir == NULL) {
1126 rc = dt_root_get(env, dt, &dti->dti_fid);
1129 dir = dt_locate(env, dt, &dti->dti_fid);
1131 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1132 lu_object_put(env, &dir->do_lu);
1133 return ERR_PTR(-ENOTDIR);
1136 lu_object_get(&ctxt->loc_dir->do_lu);
1137 dir = ctxt->loc_dir;
1144 * Implementation of the llog_operations::lop_open
1146 * This function opens the llog by its logid or by name, it may open also
1147 * non existent llog and assing then new id to it.
1148 * The llog_open/llog_close pair works similar to lu_object_find/put,
1149 * the object may not exist prior open. The result of open is just dt_object
1150 * in the llog header.
1152 * \param[in] env execution environment
1153 * \param[in] handle llog handle of the current llog
1154 * \param[in] logid logid of llog to open (nameless llog)
1155 * \param[in] name name of llog to open (named llog)
1156 * \param[in] open_param
1157 * LLOG_OPEN_NEW - new llog, may not exist
1158 * LLOG_OPEN_EXIST - old llog, must exist
1160 * \retval 0 on successful open, llog_handle::lgh_obj
1161 * contains the dt_object of the llog.
1162 * \retval negative value on error
1164 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1165 struct llog_logid *logid, char *name,
1166 enum llog_open_param open_param)
1168 struct llog_thread_info *lgi = llog_info(env);
1169 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1170 struct dt_object *o;
1171 struct dt_device *dt;
1172 struct ls_device *ls;
1173 struct local_oid_storage *los = NULL;
1175 bool new_id = false;
1181 LASSERT(ctxt->loc_exp);
1182 LASSERT(ctxt->loc_exp->exp_obd);
1183 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1185 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1186 struct lu_object_conf conf = { 0 };
1187 if (logid != NULL) {
1188 logid_to_fid(logid, &lgi->lgi_fid);
1190 /* If logid == NULL, then it means the caller needs
1191 * to allocate new FID (llog_cat_declare_add_rec()). */
1192 rc = obd_fid_alloc(env, ctxt->loc_exp,
1193 &lgi->lgi_fid, NULL);
1197 conf.loc_flags = LOC_F_NEW;
1200 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1201 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1208 ls = ls_device_get(dt);
1210 RETURN(PTR_ERR(ls));
1212 mutex_lock(&ls->ls_los_mutex);
1213 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1214 mutex_unlock(&ls->ls_los_mutex);
1216 ls_device_put(env, ls);
1220 if (logid != NULL) {
1221 logid_to_fid(logid, &lgi->lgi_fid);
1223 struct dt_object *llog_dir;
1225 llog_dir = llog_osd_dir_get(env, ctxt);
1226 if (IS_ERR(llog_dir))
1227 GOTO(out, rc = PTR_ERR(llog_dir));
1228 dt_read_lock(env, llog_dir, 0);
1229 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1230 dt_read_unlock(env, llog_dir);
1231 lu_object_put(env, &llog_dir->do_lu);
1232 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1233 /* generate fid for new llog */
1234 rc = local_object_fid_generate(env, los,
1240 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1241 if (handle->lgh_name)
1242 strcpy(handle->lgh_name, name);
1244 GOTO(out, rc = -ENOMEM);
1246 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1247 /* generate fid for new llog */
1249 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1255 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1257 GOTO(out_name, rc = PTR_ERR(o));
1259 if (dt_object_exists(o) && new_id) {
1260 /* llog exists with just generated ID, e.g. some old llog file
1261 * still is in use or is orphan, drop a warn and skip it. */
1262 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1264 o->do_lu.lo_dev->ld_obd->obd_name,
1265 PFID(lu_object_fid(&o->do_lu)));
1266 lu_object_put(env, &o->do_lu);
1267 /* just skip this llog ID, we shouldn't delete it because we
1268 * don't know exactly what is its purpose and state. */
1273 /* No new llog is expected but doesn't exist */
1274 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
1275 GOTO(out_put, rc = -ENOENT);
1277 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1278 handle->lgh_obj = o;
1279 handle->private_data = los;
1280 LASSERT(handle->lgh_ctxt);
1285 lu_object_put(env, &o->do_lu);
1287 if (handle->lgh_name != NULL)
1288 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1296 * Get dir for regular fid log object
1298 * Get directory for regular fid log object, and these regular fid log
1299 * object will be inserted under this directory, to satisfy the FS
1300 * consistency check, e2fsck etc.
1302 * \param [in] env execution environment
1303 * \param [in] dto llog object
1305 * \retval pointer to the directory if it is found.
1306 * \retval ERR_PTR(negative errno) if it fails.
1308 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1309 struct dt_object *dto)
1311 struct llog_thread_info *lgi = llog_info(env);
1312 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1313 struct lu_seq_range *range = &lgi->lgi_range;
1314 struct lu_fid *dir_fid = &lgi->lgi_fid;
1315 struct dt_object *dir;
1319 fld_range_set_any(range);
1320 LASSERT(ss != NULL);
1321 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1322 fid_seq(lu_object_fid(&dto->do_lu)), range);
1324 RETURN(ERR_PTR(rc));
1326 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1327 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1331 if (!dt_try_as_dir(env, dir)) {
1332 lu_object_put(env, &dir->do_lu);
1333 RETURN(ERR_PTR(-ENOTDIR));
1340 * Add llog object with regular FID to name entry
1342 * Add llog object with regular FID to name space, and each llog
1343 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1344 * so to satisfy the namespace consistency check, e2fsck etc.
1346 * \param [in] env execution environment
1347 * \param [in] dto llog object
1348 * \param [in] th thandle
1349 * \param [in] declare if it is declare or execution
1351 * \retval 0 if insertion succeeds.
1352 * \retval negative errno if insertion fails.
1355 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1356 struct dt_object *dto,
1357 struct thandle *th, bool declare)
1359 struct llog_thread_info *lgi = llog_info(env);
1360 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1361 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1362 struct dt_object *dir;
1363 char *name = lgi->lgi_name;
1367 if (!fid_is_norm(fid))
1370 dir = llog_osd_get_regular_fid_dir(env, dto);
1372 RETURN(PTR_ERR(dir));
1375 rec->rec_type = S_IFREG;
1376 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1377 dt_write_lock(env, dir, 0);
1379 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1380 (struct dt_key *)name, th);
1382 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1383 (struct dt_key *)name, th, 1);
1385 dt_write_unlock(env, dir);
1387 lu_object_put(env, &dir->do_lu);
1393 * Implementation of the llog_operations::lop_declare_create
1395 * This function declares the llog create. It declares also name insert
1396 * into llog directory in case of named llog.
1398 * \param[in] env execution environment
1399 * \param[in] res llog handle of the current llog
1400 * \param[in] th current transaction handle
1402 * \retval 0 on successful create declaration
1403 * \retval negative value on error
1405 static int llog_osd_declare_create(const struct lu_env *env,
1406 struct llog_handle *res, struct thandle *th)
1408 struct llog_thread_info *lgi = llog_info(env);
1409 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1410 struct local_oid_storage *los;
1411 struct dt_object *o;
1416 LASSERT(res->lgh_obj);
1419 /* object can be created by another thread */
1421 if (dt_object_exists(o))
1424 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1425 struct llog_thread_info *lgi = llog_info(env);
1427 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1428 lgi->lgi_attr.la_size = 0;
1429 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1430 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1432 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1438 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1442 los = res->private_data;
1445 rc = llog_osd_declare_new_object(env, los, o, th);
1449 /* do not declare header initialization here as it's declared
1450 * in llog_osd_declare_write_rec() which is always called */
1452 if (res->lgh_name) {
1453 struct dt_object *llog_dir;
1455 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1456 if (IS_ERR(llog_dir))
1457 RETURN(PTR_ERR(llog_dir));
1458 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1459 rec->rec_fid = &lgi->lgi_fid;
1460 rec->rec_type = S_IFREG;
1461 rc = dt_declare_insert(env, llog_dir,
1462 (struct dt_rec *)rec,
1463 (struct dt_key *)res->lgh_name, th);
1464 lu_object_put(env, &llog_dir->do_lu);
1466 CERROR("%s: can't declare named llog %s: rc = %d\n",
1467 o->do_lu.lo_dev->ld_obd->obd_name,
1474 * Implementation of the llog_operations::lop_create
1476 * This function creates the llog according with llog_handle::lgh_obj
1477 * and llog_handle::lgh_name.
1479 * \param[in] env execution environment
1480 * \param[in] res llog handle of the current llog
1481 * \param[in] th current transaction handle
1483 * \retval 0 on successful create
1484 * \retval negative value on error
1486 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1489 struct llog_thread_info *lgi = llog_info(env);
1490 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1491 struct local_oid_storage *los;
1492 struct dt_object *o;
1501 /* llog can be already created */
1502 if (dt_object_exists(o))
1505 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1506 struct llog_thread_info *lgi = llog_info(env);
1508 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1509 lgi->lgi_attr.la_size = 0;
1510 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1511 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1513 dt_write_lock(env, o, 0);
1514 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1516 dt_write_unlock(env, o);
1520 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1525 los = res->private_data;
1528 dt_write_lock(env, o, 0);
1529 if (!dt_object_exists(o))
1530 rc = llog_osd_create_new_object(env, los, o, th);
1534 dt_write_unlock(env, o);
1538 if (res->lgh_name) {
1539 struct dt_object *llog_dir;
1541 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1542 if (IS_ERR(llog_dir))
1543 RETURN(PTR_ERR(llog_dir));
1545 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1546 rec->rec_fid = &lgi->lgi_fid;
1547 rec->rec_type = S_IFREG;
1548 dt_read_lock(env, llog_dir, 0);
1549 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1550 (struct dt_key *)res->lgh_name,
1552 dt_read_unlock(env, llog_dir);
1553 lu_object_put(env, &llog_dir->do_lu);
1555 CERROR("%s: can't create named llog %s: rc = %d\n",
1556 o->do_lu.lo_dev->ld_obd->obd_name,
1563 * Implementation of the llog_operations::lop_close
1565 * This function closes the llog. It just put llog object and referenced
1568 * \param[in] env execution environment
1569 * \param[in] handle llog handle of the current llog
1571 * \retval 0 on successful llog close
1572 * \retval negative value on error
1574 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1576 struct local_oid_storage *los;
1581 LASSERT(handle->lgh_obj);
1583 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1584 /* Remove the object from the cache, otherwise it may
1585 * hold LOD being released during cleanup process */
1586 lu_object_put_nocache(env, &handle->lgh_obj->do_lu);
1587 LASSERT(handle->private_data == NULL);
1590 lu_object_put(env, &handle->lgh_obj->do_lu);
1592 los = handle->private_data;
1596 if (handle->lgh_name)
1597 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1603 * delete llog object name entry
1605 * Delete llog object (with regular FID) from name space (under
1608 * \param [in] env execution environment
1609 * \param [in] dto llog object
1610 * \param [in] th thandle
1611 * \param [in] declare if it is declare or execution
1613 * \retval 0 if deletion succeeds.
1614 * \retval negative errno if deletion fails.
1617 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1618 struct dt_object *dto,
1619 struct thandle *th, bool declare)
1621 struct llog_thread_info *lgi = llog_info(env);
1622 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1623 struct dt_object *dir;
1624 char *name = lgi->lgi_name;
1628 if (!fid_is_norm(fid))
1631 dir = llog_osd_get_regular_fid_dir(env, dto);
1633 RETURN(PTR_ERR(dir));
1635 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1636 dt_write_lock(env, dir, 0);
1638 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1641 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1643 dt_write_unlock(env, dir);
1645 lu_object_put(env, &dir->do_lu);
1650 * Implementation of the llog_operations::lop_declare_destroy
1652 * This function declare destroys the llog and deletes also entry in the
1653 * llog directory in case of named llog. Llog should be opened prior that.
1655 * \param[in] env execution environment
1656 * \param[in] loghandle llog handle of the current llog
1658 * \retval 0 on successful destroy
1659 * \retval negative value on error
1661 static int llog_osd_declare_destroy(const struct lu_env *env,
1662 struct llog_handle *loghandle,
1665 struct llog_ctxt *ctxt;
1666 struct dt_object *o, *llog_dir = NULL;
1671 ctxt = loghandle->lgh_ctxt;
1674 o = loghandle->lgh_obj;
1677 if (loghandle->lgh_name) {
1678 llog_dir = llog_osd_dir_get(env, ctxt);
1679 if (IS_ERR(llog_dir))
1680 RETURN(PTR_ERR(llog_dir));
1682 rc = dt_declare_delete(env, llog_dir,
1683 (struct dt_key *)loghandle->lgh_name,
1689 rc = dt_declare_ref_del(env, o, th);
1693 rc = dt_declare_destroy(env, o, th);
1697 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1698 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1704 if (!(IS_ERR_OR_NULL(llog_dir)))
1705 lu_object_put(env, &llog_dir->do_lu);
1712 * Implementation of the llog_operations::lop_destroy
1714 * This function destroys the llog and deletes also entry in the
1715 * llog directory in case of named llog. Llog should be opened prior that.
1716 * Destroy method is not part of external transaction and does everything
1719 * \param[in] env execution environment
1720 * \param[in] loghandle llog handle of the current llog
1722 * \retval 0 on successful destroy
1723 * \retval negative value on error
1725 static int llog_osd_destroy(const struct lu_env *env,
1726 struct llog_handle *loghandle, struct thandle *th)
1728 struct llog_ctxt *ctxt;
1729 struct dt_object *o, *llog_dir = NULL;
1734 ctxt = loghandle->lgh_ctxt;
1735 LASSERT(ctxt != NULL);
1737 o = loghandle->lgh_obj;
1740 dt_write_lock(env, o, 0);
1741 if (!dt_object_exists(o))
1742 GOTO(out_unlock, rc = 0);
1744 if (loghandle->lgh_name) {
1745 llog_dir = llog_osd_dir_get(env, ctxt);
1746 if (IS_ERR(llog_dir))
1747 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1749 dt_read_lock(env, llog_dir, 0);
1750 rc = dt_delete(env, llog_dir,
1751 (struct dt_key *)loghandle->lgh_name,
1753 dt_read_unlock(env, llog_dir);
1755 CERROR("%s: can't remove llog %s: rc = %d\n",
1756 o->do_lu.lo_dev->ld_obd->obd_name,
1757 loghandle->lgh_name, rc);
1758 GOTO(out_unlock, rc);
1762 dt_ref_del(env, o, th);
1763 rc = dt_destroy(env, o, th);
1765 GOTO(out_unlock, rc);
1767 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1768 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1770 GOTO(out_unlock, rc);
1774 dt_write_unlock(env, o);
1775 if (!(IS_ERR_OR_NULL(llog_dir)))
1776 lu_object_put(env, &llog_dir->do_lu);
1781 * Implementation of the llog_operations::lop_setup
1783 * This function setup the llog on local storage.
1785 * \param[in] env execution environment
1786 * \param[in] obd obd device the llog belongs to
1787 * \param[in] olg the llog group, it is always zero group now.
1788 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1789 * Every new llog type have to use own index.
1790 * \param[in] disk_obd the storage obd, where llog is stored.
1792 * \retval 0 on successful llog setup
1793 * \retval negative value on error
1795 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1796 struct obd_llog_group *olg, int ctxt_idx,
1797 struct obd_device *disk_obd)
1799 struct llog_thread_info *lgi = llog_info(env);
1800 struct llog_ctxt *ctxt;
1805 LASSERT(olg->olg_ctxts[ctxt_idx]);
1807 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1810 if (disk_obd == NULL)
1813 /* initialize data allowing to generate new fids,
1814 * literally we need a sequece */
1815 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1816 lgi->lgi_fid.f_oid = 1;
1817 lgi->lgi_fid.f_ver = 0;
1818 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1820 &ctxt->loc_los_nameless);
1824 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1825 lgi->lgi_fid.f_oid = 1;
1826 lgi->lgi_fid.f_ver = 0;
1827 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1829 &ctxt->loc_los_named);
1831 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1832 ctxt->loc_los_nameless = NULL;
1838 llog_ctxt_put(ctxt);
1843 * Implementation of the llog_operations::lop_cleanup
1845 * This function cleanups the llog on local storage.
1847 * \param[in] env execution environment
1848 * \param[in] ctxt the llog context
1852 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1854 if (ctxt->loc_los_nameless != NULL) {
1855 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1856 ctxt->loc_los_nameless = NULL;
1859 if (ctxt->loc_los_named != NULL) {
1860 local_oid_storage_fini(env, ctxt->loc_los_named);
1861 ctxt->loc_los_named = NULL;
1867 struct llog_operations llog_osd_ops = {
1868 .lop_next_block = llog_osd_next_block,
1869 .lop_prev_block = llog_osd_prev_block,
1870 .lop_read_header = llog_osd_read_header,
1871 .lop_declare_destroy = llog_osd_declare_destroy,
1872 .lop_destroy = llog_osd_destroy,
1873 .lop_setup = llog_osd_setup,
1874 .lop_cleanup = llog_osd_cleanup,
1875 .lop_open = llog_osd_open,
1876 .lop_exist = llog_osd_exist,
1877 .lop_declare_create = llog_osd_declare_create,
1878 .lop_create = llog_osd_create,
1879 .lop_declare_write_rec = llog_osd_declare_write_rec,
1880 .lop_write_rec = llog_osd_write_rec,
1881 .lop_close = llog_osd_close,
1883 EXPORT_SYMBOL(llog_osd_ops);
1885 struct llog_operations llog_common_cat_ops = {
1886 .lop_next_block = llog_osd_next_block,
1887 .lop_prev_block = llog_osd_prev_block,
1888 .lop_read_header = llog_osd_read_header,
1889 .lop_declare_destroy = llog_osd_declare_destroy,
1890 .lop_destroy = llog_osd_destroy,
1891 .lop_setup = llog_osd_setup,
1892 .lop_cleanup = llog_osd_cleanup,
1893 .lop_open = llog_osd_open,
1894 .lop_exist = llog_osd_exist,
1895 .lop_declare_create = llog_osd_declare_create,
1896 .lop_create = llog_osd_create,
1897 .lop_declare_write_rec = llog_osd_declare_write_rec,
1898 .lop_write_rec = llog_osd_write_rec,
1899 .lop_close = llog_osd_close,
1900 .lop_add = llog_cat_add_rec,
1901 .lop_declare_add = llog_cat_declare_add_rec,
1903 EXPORT_SYMBOL(llog_common_cat_ops);
1906 * Read the special file which contains the list of llog catalogs IDs
1908 * This function reads the CATALOGS file which contains the array of llog
1909 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1910 * by OST/MDT number.
1912 * \param[in] env execution environment
1913 * \param[in] d corresponding storage device
1914 * \param[in] idx position to start from, usually OST/MDT index
1915 * \param[in] count how many catalog IDs to read
1916 * \param[out] idarray the buffer for the data. If it is NULL then
1917 * function returns just number of catalog IDs
1919 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1921 * \retval 0 on successful read of catalog IDs
1922 * \retval negative value on error
1923 * \retval positive value which is number of records in
1924 * the file if \a idarray is NULL
1926 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1927 int idx, int count, struct llog_catid *idarray,
1928 const struct lu_fid *fid)
1930 struct llog_thread_info *lgi = llog_info(env);
1931 struct dt_object *o = NULL;
1939 size = sizeof(*idarray) * count;
1940 lgi->lgi_off = idx * sizeof(*idarray);
1942 lgi->lgi_fid = *fid;
1943 o = dt_locate(env, d, &lgi->lgi_fid);
1947 if (!dt_object_exists(o)) {
1948 th = dt_trans_create(env, d);
1950 GOTO(out, rc = PTR_ERR(th));
1952 lgi->lgi_attr.la_valid = LA_MODE;
1953 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1954 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1956 th->th_wait_submit = 1;
1957 /* Make the llog object creation synchronization, so
1958 * it will be reliable to the reference, especially
1959 * for remote reference */
1962 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1965 GOTO(out_trans, rc);
1967 rc = dt_trans_start_local(env, d, th);
1969 GOTO(out_trans, rc);
1971 dt_write_lock(env, o, 0);
1972 if (!dt_object_exists(o))
1973 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1975 dt_write_unlock(env, o);
1977 dt_trans_stop(env, d, th);
1982 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1986 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1987 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1988 o->do_lu.lo_dev->ld_obd->obd_name,
1989 lgi->lgi_attr.la_mode);
1990 GOTO(out, rc = -ENOENT);
1993 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1994 (int)lgi->lgi_attr.la_size, size);
1996 /* return just number of llogs */
1997 if (idarray == NULL) {
1998 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2002 /* read for new ost index or for empty file */
2003 memset(idarray, 0, size);
2004 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2006 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2007 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2009 lgi->lgi_buf.lb_buf = idarray;
2010 lgi->lgi_buf.lb_len = size;
2011 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2012 /* -EFAULT means the llog is a sparse file. This is not an error
2013 * after arbitrary OST index is supported. */
2014 if (rc < 0 && rc != -EFAULT) {
2015 CERROR("%s: error reading CATALOGS: rc = %d\n",
2016 o->do_lu.lo_dev->ld_obd->obd_name, rc);
2022 lu_object_put(env, &o->do_lu);
2025 EXPORT_SYMBOL(llog_osd_get_cat_list);
2028 * Write the special file which contains the list of llog catalogs IDs
2030 * This function writes the CATALOG file which contains the array of llog
2031 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2034 * \param[in] env execution environment
2035 * \param[in] d corresponding storage device
2036 * \param[in] idx position to start from, usually OST/MDT index
2037 * \param[in] count how many catalog IDs to write
2038 * \param[out] idarray the buffer with the data to write.
2039 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2041 * \retval 0 on successful write of catalog IDs
2042 * \retval negative value on error
2044 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2045 int idx, int count, struct llog_catid *idarray,
2046 const struct lu_fid *fid)
2048 struct llog_thread_info *lgi = llog_info(env);
2049 struct dt_object *o = NULL;
2058 size = sizeof(*idarray) * count;
2059 lgi->lgi_off = idx * sizeof(*idarray);
2060 lgi->lgi_fid = *fid;
2062 o = dt_locate(env, d, &lgi->lgi_fid);
2066 if (!dt_object_exists(o))
2067 GOTO(out, rc = -ENOENT);
2069 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2073 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2074 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2075 o->do_lu.lo_dev->ld_obd->obd_name,
2076 lgi->lgi_attr.la_mode);
2077 GOTO(out, rc = -ENOENT);
2080 th = dt_trans_create(env, d);
2082 GOTO(out, rc = PTR_ERR(th));
2084 lgi->lgi_buf.lb_len = size;
2085 lgi->lgi_buf.lb_buf = idarray;
2086 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2088 GOTO(out_trans, rc);
2090 /* For update log, this happens during initialization,
2091 * see lod_sub_prep_llog(), and we need make sure catlog
2092 * file ID is written to catlist file(committed) before
2093 * cross-MDT operation write update records to catlog FILE,
2094 * otherwise, during failover these update records might
2096 if (fid_is_update_log(fid))
2099 rc = dt_trans_start_local(env, d, th);
2101 GOTO(out_trans, rc);
2103 th->th_wait_submit = 1;
2105 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2107 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2110 dt_trans_stop(env, d, th);
2112 lu_object_put(env, &o->do_lu);
2115 EXPORT_SYMBOL(llog_osd_put_cat_list);