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/
32 * lustre/obdclass/llog_osd.c
34 * Low level llog routines on top of OSD API
36 * This file provides set of methods for llog operations on top of
37 * dt_device. It contains all supported llog_operations interfaces and
38 * supplimental functions.
40 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
41 * Author: Mikhail Pershin <mike.pershin@intel.com>
44 #define DEBUG_SUBSYSTEM S_LOG
46 #include <linux/delay.h>
48 #include <dt_object.h>
49 #include <llog_swab.h>
50 #include <lustre_fid.h>
52 #include <obd_class.h>
54 #include "llog_internal.h"
55 #include "local_storage.h"
58 * Implementation of the llog_operations::lop_declare_create
60 * This function is a wrapper over local_storage API function
61 * local_object_declare_create().
63 * \param[in] env execution environment
64 * \param[in] los local_storage for bottom storage device
65 * \param[in] o dt_object to create
66 * \param[in] th current transaction handle
68 * \retval 0 on successful declaration of the new object
69 * \retval negative error if declaration was failed
71 static int llog_osd_declare_new_object(const struct lu_env *env,
72 struct local_oid_storage *los,
76 struct llog_thread_info *lgi = llog_info(env);
78 lgi->lgi_attr.la_valid = LA_MODE;
79 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
80 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
82 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
87 * Implementation of the llog_operations::lop_create
89 * This function is a wrapper over local_storage API function
90 * local_object_create().
92 * \param[in] env execution environment
93 * \param[in] los local_storage for bottom storage device
94 * \param[in] o dt_object to create
95 * \param[in] th current transaction handle
97 * \retval 0 on successful creation of the new object
98 * \retval negative error if creation was failed
100 static int llog_osd_create_new_object(const struct lu_env *env,
101 struct local_oid_storage *los,
105 struct llog_thread_info *lgi = llog_info(env);
107 lgi->lgi_attr.la_valid = LA_MODE;
108 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
109 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
111 return local_object_create(env, los, o, &lgi->lgi_attr,
116 * Implementation of the llog_operations::lop_exist
118 * This function checks that llog exists on storage.
120 * \param[in] handle llog handle of the current llog
122 * \retval true if llog object exists and is not just destroyed
123 * \retval false if llog doesn't exist or just destroyed
125 static int llog_osd_exist(struct llog_handle *handle)
127 LASSERT(handle->lgh_obj);
128 return dt_object_exists(handle->lgh_obj) && !handle->lgh_destroyed;
131 static void *rec_tail(struct llog_rec_hdr *rec)
133 return (void *)((char *)rec + rec->lrh_len -
134 sizeof(struct llog_rec_tail));
138 * Write a padding record to the llog
140 * This function writes a padding record to the end of llog. That may
141 * be needed if llog contains records of variable size, e.g. config logs
143 * The padding record just aligns llog to the llog chunk_size boundary if
144 * the current record doesn't fit in the remaining space.
146 * It allocates full length to avoid two separate writes for header and tail.
147 * Such 2-steps scheme needs extra protection and complex error handling.
149 * \param[in] env execution environment
150 * \param[in] o dt_object to create
151 * \param[in,out] off pointer to the padding start offset
152 * \param[in] len padding length
153 * \param[in] index index of the padding record in a llog
154 * \param[in] th current transaction handle
156 * \retval 0 on successful padding write
157 * \retval negative error if write failed
159 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
160 loff_t *off, int len, int index, struct thandle *th)
162 struct llog_thread_info *lgi = llog_info(env);
163 struct llog_rec_hdr *rec;
164 struct llog_rec_tail *tail;
171 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
178 rec->lrh_index = index;
179 rec->lrh_type = LLOG_PAD_MAGIC;
181 tail = rec_tail(rec);
183 tail->lrt_index = index;
185 lgi->lgi_buf.lb_buf = rec;
186 lgi->lgi_buf.lb_len = len;
187 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
189 CERROR("%s: error writing padding record: rc = %d\n",
190 o->do_lu.lo_dev->ld_obd->obd_name, rc);
197 * Implementation of the llog_operations::lop_read_header
199 * This function reads the current llog header from the bottom storage
202 * \param[in] env execution environment
203 * \param[in] handle llog handle of the current llog
205 * \retval 0 on successful header read
206 * \retval negative error if read failed
208 static int llog_osd_read_header(const struct lu_env *env,
209 struct llog_handle *handle)
211 struct llog_rec_hdr *llh_hdr;
213 struct llog_thread_info *lgi;
214 enum llog_flag flags;
222 lgi = llog_info(env);
224 rc = dt_attr_get(env, o, &lgi->lgi_attr);
228 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
230 if (lgi->lgi_attr.la_size == 0) {
231 CDEBUG(D_HA, "not reading header from 0-byte log\n");
235 flags = handle->lgh_hdr->llh_flags;
238 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
239 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
240 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
241 llh_hdr = &handle->lgh_hdr->llh_hdr;
242 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
243 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
244 o->do_lu.lo_dev->ld_obd->obd_name,
245 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
254 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
255 lustre_swab_llog_hdr(handle->lgh_hdr);
257 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
258 CERROR("%s: bad log %s "DFID" header magic: %#x "
259 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
260 handle->lgh_name ? handle->lgh_name : "",
261 PFID(lu_object_fid(&o->do_lu)),
262 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
264 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
265 llh_hdr->lrh_len > handle->lgh_hdr_size) {
266 CERROR("%s: incorrectly sized log %s "DFID" header: "
267 "%#x (expected at least %#x)\n"
268 "you may need to re-run lconf --write_conf.\n",
269 o->do_lu.lo_dev->ld_obd->obd_name,
270 handle->lgh_name ? handle->lgh_name : "",
271 PFID(lu_object_fid(&o->do_lu)),
272 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
274 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
275 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
276 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
278 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
280 o->do_lu.lo_dev->ld_obd->obd_name,
281 handle->lgh_name ? handle->lgh_name : "",
282 PFID(lu_object_fid(&o->do_lu)),
283 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
287 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
288 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
294 * Implementation of the llog_operations::lop_declare_write
296 * This function declares the new record write.
298 * \param[in] env execution environment
299 * \param[in] loghandle llog handle of the current llog
300 * \param[in] rec llog record header. This is a real header of the full
301 * llog record to write. This is the beginning of buffer
302 * to write, the length of buffer is stored in
304 * \param[in] idx index of the llog record. If \a idx == -1 then this is
305 * append case, otherwise \a idx is the index of record
307 * \param[in] th current transaction handle
309 * \retval 0 on successful declaration
310 * \retval negative error if declaration failed
312 static int llog_osd_declare_write_rec(const struct lu_env *env,
313 struct llog_handle *loghandle,
314 struct llog_rec_hdr *rec,
315 int idx, struct thandle *th)
317 struct llog_thread_info *lgi = llog_info(env);
328 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
330 o = loghandle->lgh_obj;
333 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
334 lgi->lgi_buf.lb_len = chunk_size;
335 lgi->lgi_buf.lb_buf = NULL;
336 /* each time we update header */
337 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
339 if (rc || idx == 0) /* if error or just header */
343 * the pad record can be inserted so take into account double
346 lgi->lgi_buf.lb_len = chunk_size * 2;
347 lgi->lgi_buf.lb_buf = NULL;
348 /* XXX: implement declared window or multi-chunks approach */
349 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
355 * Implementation of the llog_operations::lop_write
357 * This function writes the new record in the llog or modify the existed one.
359 * \param[in] env execution environment
360 * \param[in] loghandle llog handle of the current llog
361 * \param[in] rec llog record header. This is a real header of
362 * the full llog record to write. This is
363 * the beginning of buffer to write, the length
364 * of buffer is stored in \a rec::lrh_len
365 * \param[in,out] reccookie pointer to the cookie to return back if needed.
366 * It is used for further cancel of this llog
368 * \param[in] idx index of the llog record. If \a idx == -1 then
369 * this is append case, otherwise \a idx is
370 * the index of record to modify
371 * \param[in] th current transaction handle
373 * \retval 0 on successful write && \a reccookie == NULL
374 * 1 on successful write && \a reccookie != NULL
375 * \retval negative error if write failed
377 static int llog_osd_write_rec(const struct lu_env *env,
378 struct llog_handle *loghandle,
379 struct llog_rec_hdr *rec,
380 struct llog_cookie *reccookie,
381 int idx, struct thandle *th)
383 struct llog_thread_info *lgi = llog_info(env);
384 struct llog_log_hdr *llh;
385 int reclen = rec->lrh_len;
387 struct llog_rec_tail *lrt;
394 llh = loghandle->lgh_hdr;
395 o = loghandle->lgh_obj;
397 chunk_size = llh->llh_hdr.lrh_len;
398 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
399 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
401 if (!llog_osd_exist(loghandle))
404 /* record length should not bigger than */
405 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
408 /* sanity check for fixed-records llog */
409 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
410 LASSERT(llh->llh_size != 0);
411 LASSERT(llh->llh_size == reclen);
414 /* return error if osp object is stale */
415 if (idx != LLOG_HEADER_IDX && dt_object_stale(o))
417 rc = dt_attr_get(env, o, &lgi->lgi_attr);
422 * The modification case.
423 * If idx set then the record with that index must be modified.
424 * There are three cases possible:
425 * 1) the common case is the llog header update (idx == 0)
426 * 2) the llog record modification during llog process.
427 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
428 * In that case the \a loghandle::lgh_cur_offset
429 * 3) otherwise this is assumed that llog consist of records of
430 * fixed size, i.e. catalog. The llog header must has llh_size
431 * field equal to record size. The record offset is calculated
432 * just by /a idx value
434 * During modification we don't need extra header update because
435 * the bitmap and record count are not changed. The record header
436 * and tail remains the same too.
438 if (idx != LLOG_NEXT_IDX) {
439 /* llog can be empty only when first record is being written */
440 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
442 if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
443 CERROR("%s: modify unset record %u\n",
444 o->do_lu.lo_dev->ld_obd->obd_name, idx);
448 if (idx != rec->lrh_index) {
449 CERROR("%s: modify index mismatch %d %u\n",
450 o->do_lu.lo_dev->ld_obd->obd_name, idx,
455 if (idx == LLOG_HEADER_IDX) {
456 /* llog header update */
457 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
461 /* If it does not indicate the bitmap index
462 * (reccookie == NULL), then it means update
463 * the whole update header. Otherwise only
464 * update header and bits needs to be updated,
465 * and in DNE cases, it will signaficantly
466 * shrink the RPC size.
467 * see distribute_txn_cancel_records()*/
468 if (reccookie == NULL) {
469 lgi->lgi_buf.lb_len = reclen;
470 lgi->lgi_buf.lb_buf = rec;
471 rc = dt_record_write(env, o, &lgi->lgi_buf,
476 /* update the header */
477 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
478 lgi->lgi_buf.lb_buf = llh;
479 rc = dt_record_write(env, o, &lgi->lgi_buf,
484 /* update the bitmap */
485 index = reccookie->lgc_index;
486 lgi->lgi_off = llh->llh_bitmap_offset +
487 (index / (sizeof(*bitmap) * 8)) *
489 lgi->lgi_buf.lb_len = sizeof(*bitmap);
490 lgi->lgi_buf.lb_buf =
491 &bitmap[index/(sizeof(*bitmap)*8)];
492 rc = dt_record_write(env, o, &lgi->lgi_buf,
496 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
497 lgi->lgi_off = llh->llh_hdr.lrh_len +
499 } else if (reccookie != NULL && reccookie->lgc_index > 0) {
501 * The lgc_offset can be used only if index is
504 if (idx != reccookie->lgc_index) {
505 CERROR("%s: modify index mismatch %d %d\n",
506 o->do_lu.lo_dev->ld_obd->obd_name, idx,
507 reccookie->lgc_index);
511 lgi->lgi_off = reccookie->lgc_offset;
512 CDEBUG(D_OTHER, "modify record "DFID": idx:%u, "
513 "len:%u offset %llu\n",
514 PFID(&loghandle->lgh_id.lgl_oi.oi_fid), idx,
515 rec->lrh_len, (long long)lgi->lgi_off);
517 /* This can be result of lgh_cur_idx is not set during
518 * llog processing or llh_size is not set to proper
519 * record size for fixed records llog. Therefore it is
520 * impossible to get record offset. */
521 CERROR("%s: can't get record offset, idx:%d, "
522 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
527 /* update only data, header and tail remain the same */
528 lgi->lgi_off += sizeof(struct llog_rec_hdr);
529 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
530 lgi->lgi_buf.lb_buf = REC_DATA(rec);
531 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
532 if (rc == 0 && reccookie) {
533 reccookie->lgc_lgl = loghandle->lgh_id;
534 reccookie->lgc_index = idx;
542 * The most common case of using llog. The new index is assigned to
543 * the new record, new bit is set in llog bitmap and llog count is
546 * Make sure that records don't cross a chunk boundary, so we can
547 * process them page-at-a-time if needed. If it will cross a chunk
548 * boundary, write in a fake (but referenced) entry to pad the chunk.
552 /* simulate ENOSPC when new plain llog is being added to the
554 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
555 llh->llh_flags & LLOG_F_IS_CAT)
558 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
559 orig_last_idx = loghandle->lgh_last_idx;
560 lgi->lgi_off = lgi->lgi_attr.la_size;
562 if (loghandle->lgh_max_size > 0 &&
563 lgi->lgi_off >= loghandle->lgh_max_size) {
564 CDEBUG(D_OTHER, "llog is getting too large (%u > %u) at %u "
565 DFID"\n", (unsigned)lgi->lgi_off,
566 loghandle->lgh_max_size, (int)loghandle->lgh_last_idx,
567 PFID(&loghandle->lgh_id.lgl_oi.oi_fid));
568 /* this is to signal that this llog is full */
569 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
573 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
574 /* NOTE: padding is a record, but no bit is set */
575 if (left != 0 && left != reclen &&
576 left < (reclen + LLOG_MIN_REC_SIZE)) {
577 index = loghandle->lgh_last_idx + 1;
578 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
582 loghandle->lgh_last_idx++; /* for pad rec */
584 /* if it's the last idx in log file, then return -ENOSPC
585 * or wrap around if a catalog */
586 if (llog_is_full(loghandle) ||
587 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
588 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
589 loghandle->lgh_last_idx >= cfs_fail_val)) {
590 if (llh->llh_flags & LLOG_F_IS_CAT)
591 loghandle->lgh_last_idx = 0;
596 down_write(&loghandle->lgh_last_sem);
597 /* increment the last_idx along with llh_tail index, they should
598 * be equal for a llog lifetime */
599 loghandle->lgh_last_idx++;
600 index = loghandle->lgh_last_idx;
601 LLOG_HDR_TAIL(llh)->lrt_index = index;
603 * NB: the caller should make sure only 1 process access
604 * the lgh_last_idx, e.g. append should be exclusive.
605 * Otherwise it might hit the assert.
607 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
608 rec->lrh_index = index;
610 lrt->lrt_len = rec->lrh_len;
611 lrt->lrt_index = rec->lrh_index;
613 /* the lgh_hdr_mutex protects llog header data from concurrent
614 * update/cancel, the llh_count and llh_bitmap are protected */
615 mutex_lock(&loghandle->lgh_hdr_mutex);
616 if (__test_and_set_bit_le(index, LLOG_HDR_BITMAP(llh))) {
617 CERROR("%s: index %u already set in llog bitmap "DFID"\n",
618 o->do_lu.lo_dev->ld_obd->obd_name, index,
619 PFID(lu_object_fid(&o->do_lu)));
620 mutex_unlock(&loghandle->lgh_hdr_mutex);
621 LBUG(); /* should never happen */
625 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
626 /* Update the minimum size of the llog record */
627 if (llh->llh_size == 0)
628 llh->llh_size = reclen;
629 else if (reclen < llh->llh_size)
630 llh->llh_size = reclen;
633 if (lgi->lgi_attr.la_size == 0) {
635 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
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 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
643 /* Note: If this is not initialization (size == 0), then do not
644 * write the whole header (8k bytes), only update header/tail
645 * and bits needs to be updated. Because this update might be
646 * part of cross-MDT operation, which needs to write these
647 * updates into the update log(32KB limit) and also pack inside
648 * the RPC (1MB limit), if we write 8K for each operation, which
649 * will cost a lot space, and keep us adding more updates to one
652 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
653 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
654 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
656 GOTO(out_unlock, rc);
658 lgi->lgi_off = llh->llh_bitmap_offset +
659 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
660 lgi->lgi_buf.lb_len = sizeof(*bitmap);
661 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
662 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
664 GOTO(out_unlock, rc);
666 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
668 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
669 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
670 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
672 GOTO(out_unlock, rc);
676 /* unlock here for remote object */
677 mutex_unlock(&loghandle->lgh_hdr_mutex);
681 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
682 cfs_fail_val == (unsigned int)(loghandle->lgh_id.lgl_oi.oi.oi_id &
684 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
685 msleep(1 * MSEC_PER_SEC);
687 /* computed index can be used to determine offset for fixed-size
688 * records. This also allows to handle Catalog wrap around case */
689 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
690 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
692 rc = dt_attr_get(env, o, &lgi->lgi_attr);
696 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
697 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
701 lgi->lgi_buf.lb_len = reclen;
702 lgi->lgi_buf.lb_buf = rec;
703 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
707 up_write(&loghandle->lgh_last_sem);
709 CDEBUG(D_HA, "added record "DFID".%u, %u off%llu\n",
710 PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
712 if (reccookie != NULL) {
713 reccookie->lgc_lgl = loghandle->lgh_id;
714 reccookie->lgc_index = index;
715 if ((rec->lrh_type == MDS_UNLINK_REC) ||
716 (rec->lrh_type == MDS_SETATTR64_REC))
717 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
718 else if (rec->lrh_type == OST_SZ_REC)
719 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
721 reccookie->lgc_subsys = -1;
726 /* cleanup llog for error case */
727 mutex_lock(&loghandle->lgh_hdr_mutex);
728 clear_bit_le(index, LLOG_HDR_BITMAP(llh));
730 mutex_unlock(&loghandle->lgh_hdr_mutex);
732 /* restore llog last_idx */
733 if (dt_object_remote(o)) {
734 loghandle->lgh_last_idx = orig_last_idx;
735 } else if (--loghandle->lgh_last_idx == 0 &&
736 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
737 /* catalog had just wrap-around case */
738 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
741 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
742 up_write(&loghandle->lgh_last_sem);
748 * We can skip reading at least as many log blocks as the number of
749 * minimum sized log records we are skipping. If it turns out
750 * that we are not far enough along the log (because the
751 * actual records are larger than minimum size) we just skip
754 * Note: in llog_process_thread, it will use bitmap offset as
755 * the index to locate the record, which also includs some pad
756 * records, whose record size is very small, and it also does not
757 * consider pad record when recording minimum record size (otherwise
758 * min_record size might be too small), so in some rare cases,
759 * it might skip too much record for @goal, see llog_osd_next_block().
761 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
762 * as the min record size to skip over, usually because in the previous
763 * try, it skip too much record, see loog_osd_next(prev)_block().
765 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
766 int curr, int goal, __u32 chunk_size,
769 struct llog_log_hdr *llh = lgh->lgh_hdr;
771 /* Goal should not bigger than the record count */
772 if (goal > lgh->lgh_last_idx)
773 goal = lgh->lgh_last_idx;
776 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
777 *off = chunk_size + (goal - 1) * llh->llh_size;
779 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
781 if (llh->llh_size > 0 && !force_mini_rec)
782 min_rec_size = llh->llh_size;
784 *off = *off + (goal - curr - 1) * min_rec_size;
787 /* always align with lower chunk boundary*/
788 *off &= ~(chunk_size - 1);
792 * Remove optional fields that the client doesn't expect.
793 * This is typically in order to ensure compatibility with older clients.
794 * It is assumed that since we exclusively remove fields, the block will be
795 * big enough to handle the remapped records. It is also assumed that records
796 * of a block have the same format (i.e.: the same features enabled).
798 * \param[in,out] hdr Header of the block of records to remap.
799 * \param[in,out] last_hdr Last header, don't read past this point.
800 * \param[in] flags Flags describing the fields to keep.
801 * \param[in] extra_flags Flags describing the extra fields to keep.
803 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
804 struct llog_rec_hdr *last_hdr,
805 struct llog_handle *loghandle)
807 enum changelog_rec_flags flags = CLF_SUPPORTED;
808 enum changelog_rec_extra_flags extra_flags = CLFE_SUPPORTED;
810 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_XATTR))
811 extra_flags &= ~CLFE_XATTR;
812 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_OMODE))
813 extra_flags &= ~CLFE_OPEN;
814 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_NID))
815 extra_flags &= ~CLFE_NID;
816 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_UIDGID))
817 extra_flags &= ~CLFE_UIDGID;
818 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_EXTRA_FLAGS))
819 flags &= ~CLF_EXTRA_FLAGS;
820 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
823 if (flags == CLF_SUPPORTED && extra_flags == CLFE_SUPPORTED)
826 if (hdr->lrh_type != CHANGELOG_REC)
830 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
831 enum changelog_rec_extra_flags xflag = CLFE_INVALID;
833 if (flags & CLF_EXTRA_FLAGS &&
834 rec->cr_flags & CLF_EXTRA_FLAGS) {
835 xflag = changelog_rec_extra_flags(rec)->cr_extra_flags &
839 if (unlikely(hdr->lrh_len == 0)) {
840 /* It is corruption case, we cannot know the next rec,
841 * jump to the last one directly to avoid dead loop. */
842 LCONSOLE(D_WARNING, "Hit invalid llog record: "
843 "idx %u, type %u, id %u\n",
844 hdr->lrh_index, hdr->lrh_type, hdr->lrh_id);
845 hdr = llog_rec_hdr_next(last_hdr);
846 if (unlikely(hdr == last_hdr))
847 LCONSOLE(D_WARNING, "The last record crashed: "
848 "idx %u, type %u, id %u\n",
849 hdr->lrh_index, hdr->lrh_type,
854 changelog_remap_rec(rec, rec->cr_flags & flags, xflag);
855 hdr = llog_rec_hdr_next(hdr);
856 /* Yield CPU to avoid soft-lockup if there are too many records
859 } while ((char *)hdr <= (char *)last_hdr);
863 * Implementation of the llog_operations::lop_next_block
865 * This function finds the the next llog block to return which contains
866 * record with required index. It is main part of llog processing.
868 * \param[in] env execution environment
869 * \param[in] loghandle llog handle of the current llog
870 * \param[in,out] cur_idx index preceeding cur_offset
871 * \param[in] next_idx target index to find
872 * \param[in,out] cur_offset furtherst point read in the file
873 * \param[in] buf pointer to data buffer to fill
874 * \param[in] len required len to read, it is
875 * usually llog chunk_size.
877 * \retval 0 on successful buffer read
878 * \retval negative value on error
880 static int llog_osd_next_block(const struct lu_env *env,
881 struct llog_handle *loghandle, int *cur_idx,
882 int next_idx, __u64 *cur_offset, void *buf,
885 struct llog_thread_info *lgi = llog_info(env);
887 struct dt_device *dt;
890 int last_idx = *cur_idx;
891 __u64 last_offset = *cur_offset;
892 bool force_mini_rec = false;
899 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
900 if (len == 0 || len & (chunk_size - 1))
904 LASSERT(loghandle->lgh_ctxt);
906 o = loghandle->lgh_obj;
908 LASSERT(llog_osd_exist(loghandle));
909 dt = lu2dt_dev(o->do_lu.lo_dev);
912 rc = dt_attr_get(env, o, &lgi->lgi_attr);
917 "looking for log index %u (cur idx %u off %llu), size %llu\n",
919 *cur_offset, lgi->lgi_attr.la_size);
921 while (*cur_offset < lgi->lgi_attr.la_size) {
922 struct llog_rec_hdr *rec, *last_rec;
923 struct llog_rec_tail *tail;
925 llog_skip_over(loghandle, cur_offset, *cur_idx,
926 next_idx, chunk_size, force_mini_rec);
928 /* read up to next llog chunk_size block */
929 lgi->lgi_buf.lb_len = chunk_size -
930 (*cur_offset & (chunk_size - 1));
931 lgi->lgi_buf.lb_buf = buf;
933 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
935 if (rc == -EBADR && !force_mini_rec)
938 CERROR("%s: can't read llog block from log "DFID
939 " offset %llu: rc = %d\n",
940 o->do_lu.lo_dev->ld_obd->obd_name,
941 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
947 /* signal the end of the valid buffer to
949 memset(buf + rc, 0, len - rc);
952 if (rc == 0) { /* end of file, nothing to do */
958 if (rc < sizeof(*tail)) {
962 CERROR("%s: invalid llog block at log id "DFID":%x "
964 o->do_lu.lo_dev->ld_obd->obd_name,
965 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
966 loghandle->lgh_id.lgl_ogen, *cur_offset);
967 GOTO(out, rc = -EINVAL);
971 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
972 lustre_swab_llog_rec(rec);
973 tail = (struct llog_rec_tail *)((char *)buf + rc -
974 sizeof(struct llog_rec_tail));
976 if (llog_verify_record(loghandle, rec)) {
978 * the block seems corrupted. make a pad record so the
979 * caller can skip the block and try with the next one
982 rec->lrh_index = next_idx;
983 rec->lrh_type = LLOG_PAD_MAGIC;
985 tail = rec_tail(rec);
987 tail->lrt_index = next_idx;
992 /* get the last record in block */
993 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
996 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
997 lustre_swab_llog_rec(last_rec);
999 if (last_rec->lrh_index != tail->lrt_index) {
1000 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",
1001 o->do_lu.lo_dev->ld_obd->obd_name,
1002 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1003 loghandle->lgh_id.lgl_ogen, *cur_offset,
1004 last_rec->lrh_index, tail->lrt_index,
1006 GOTO(out, rc = -EINVAL);
1009 *cur_idx = tail->lrt_index;
1011 /* this shouldn't happen */
1012 if (tail->lrt_index == 0) {
1013 CERROR("%s: invalid llog tail at log id "DFID":%x "
1014 "offset %llu bytes %d\n",
1015 o->do_lu.lo_dev->ld_obd->obd_name,
1016 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1017 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
1018 GOTO(out, rc = -EINVAL);
1020 if (tail->lrt_index < next_idx) {
1021 last_idx = *cur_idx;
1022 last_offset = *cur_offset;
1026 /* sanity check that the start of the new buffer is no farther
1027 * than the record that we wanted. This shouldn't happen. */
1028 if (next_idx && rec->lrh_index > next_idx) {
1029 if (!force_mini_rec && next_idx > last_idx)
1032 CERROR("%s: missed desired record? %u > %u\n",
1033 o->do_lu.lo_dev->ld_obd->obd_name,
1034 rec->lrh_index, next_idx);
1035 GOTO(out, rc = -ENOENT);
1038 /* Trim unsupported extensions for compat w/ older clients */
1039 changelog_block_trim_ext(rec, last_rec, loghandle);
1044 /* Note: because there are some pad records in the
1045 * llog, so llog_skip_over() might skip too much
1046 * records, let's try skip again with minimum record */
1047 force_mini_rec = true;
1048 *cur_offset = last_offset;
1049 *cur_idx = last_idx;
1051 GOTO(out, rc = -EIO);
1057 * Implementation of the llog_operations::lop_prev_block
1059 * This function finds the llog block to return which contains
1060 * record with required index but in reverse order - from end of llog
1062 * It is main part of reverse llog processing.
1064 * \param[in] env execution environment
1065 * \param[in] loghandle llog handle of the current llog
1066 * \param[in] prev_idx target index to find
1067 * \param[in] buf pointer to data buffer to fill
1068 * \param[in] len required len to read, it is llog_chunk_size usually.
1070 * \retval 0 on successful buffer read
1071 * \retval negative value on error
1073 static int llog_osd_prev_block(const struct lu_env *env,
1074 struct llog_handle *loghandle,
1075 int prev_idx, void *buf, int len)
1077 struct llog_thread_info *lgi = llog_info(env);
1078 struct dt_object *o;
1079 struct dt_device *dt;
1086 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1087 if (len == 0 || len & (chunk_size - 1))
1090 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1093 LASSERT(loghandle->lgh_ctxt);
1095 o = loghandle->lgh_obj;
1097 LASSERT(llog_osd_exist(loghandle));
1098 dt = lu2dt_dev(o->do_lu.lo_dev);
1101 /* Let's only use mini record size for previous block read
1103 cur_offset = chunk_size;
1104 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1107 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1111 while (cur_offset < lgi->lgi_attr.la_size) {
1112 struct llog_rec_hdr *rec, *last_rec;
1113 struct llog_rec_tail *tail;
1115 lgi->lgi_buf.lb_len = len;
1116 lgi->lgi_buf.lb_buf = buf;
1117 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1119 CERROR("%s: can't read llog block from log "DFID
1120 " offset %llu: rc = %d\n",
1121 o->do_lu.lo_dev->ld_obd->obd_name,
1122 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1126 if (rc == 0) /* end of file, nothing to do */
1129 if (rc < sizeof(*tail)) {
1130 CERROR("%s: invalid llog block at log id "DFID":%x "
1132 o->do_lu.lo_dev->ld_obd->obd_name,
1133 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1134 loghandle->lgh_id.lgl_ogen, cur_offset);
1135 GOTO(out, rc = -EINVAL);
1139 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1140 lustre_swab_llog_rec(rec);
1142 tail = (struct llog_rec_tail *)((char *)buf + rc -
1143 sizeof(struct llog_rec_tail));
1144 /* get the last record in block */
1145 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1146 le32_to_cpu(tail->lrt_len));
1148 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1149 lustre_swab_llog_rec(last_rec);
1150 LASSERT(last_rec->lrh_index == tail->lrt_index);
1152 /* this shouldn't happen */
1153 if (tail->lrt_index == 0) {
1154 CERROR("%s: invalid llog tail at log id "DFID":%x "
1156 o->do_lu.lo_dev->ld_obd->obd_name,
1157 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1158 loghandle->lgh_id.lgl_ogen, cur_offset);
1159 GOTO(out, rc = -EINVAL);
1161 if (tail->lrt_index < prev_idx)
1164 /* sanity check that the start of the new buffer is no farther
1165 * than the record that we wanted. This shouldn't happen. */
1166 if (rec->lrh_index > prev_idx) {
1167 CERROR("%s: missed desired record? %u > %u\n",
1168 o->do_lu.lo_dev->ld_obd->obd_name,
1169 rec->lrh_index, prev_idx);
1170 GOTO(out, rc = -ENOENT);
1173 /* Trim unsupported extensions for compat w/ older clients */
1174 changelog_block_trim_ext(rec, last_rec, loghandle);
1178 GOTO(out, rc = -EIO);
1184 * This is helper function to get llog directory object. It is used by named
1185 * llog operations to find/insert/delete llog entry from llog directory.
1187 * \param[in] env execution environment
1188 * \param[in] ctxt llog context
1190 * \retval dt_object of llog directory
1191 * \retval ERR_PTR of negative value on error
1193 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1194 struct llog_ctxt *ctxt)
1196 struct dt_device *dt;
1197 struct dt_thread_info *dti = dt_info(env);
1198 struct dt_object *dir;
1201 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1202 if (ctxt->loc_dir == NULL) {
1203 rc = dt_root_get(env, dt, &dti->dti_fid);
1206 dir = dt_locate(env, dt, &dti->dti_fid);
1208 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1209 dt_object_put(env, dir);
1210 return ERR_PTR(-ENOTDIR);
1213 lu_object_get(&ctxt->loc_dir->do_lu);
1214 dir = ctxt->loc_dir;
1221 * Implementation of the llog_operations::lop_open
1223 * This function opens the llog by its logid or by name, it may open also
1224 * non existent llog and assing then new id to it.
1225 * The llog_open/llog_close pair works similar to lu_object_find/put,
1226 * the object may not exist prior open. The result of open is just dt_object
1227 * in the llog header.
1229 * \param[in] env execution environment
1230 * \param[in] handle llog handle of the current llog
1231 * \param[in] logid logid of llog to open (nameless llog)
1232 * \param[in] name name of llog to open (named llog)
1233 * \param[in] open_param
1234 * LLOG_OPEN_NEW - new llog, may not exist
1235 * LLOG_OPEN_EXIST - old llog, must exist
1237 * \retval 0 on successful open, llog_handle::lgh_obj
1238 * contains the dt_object of the llog.
1239 * \retval negative value on error
1241 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1242 struct llog_logid *logid, char *name,
1243 enum llog_open_param open_param)
1245 struct llog_thread_info *lgi = llog_info(env);
1246 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1247 struct dt_object *o;
1248 struct dt_device *dt;
1249 struct ls_device *ls;
1250 struct local_oid_storage *los = NULL;
1252 bool new_id = false;
1258 LASSERT(ctxt->loc_exp);
1259 LASSERT(ctxt->loc_exp->exp_obd);
1260 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1262 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1263 struct lu_object_conf conf = { 0 };
1264 if (logid != NULL) {
1265 logid_to_fid(logid, &lgi->lgi_fid);
1267 /* If logid == NULL, then it means the caller needs
1268 * to allocate new FID (llog_cat_declare_add_rec()). */
1269 rc = dt_fid_alloc(env, dt, &lgi->lgi_fid, NULL, NULL);
1273 conf.loc_flags = LOC_F_NEW;
1276 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1277 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1284 ls = ls_device_get(dt);
1286 RETURN(PTR_ERR(ls));
1288 mutex_lock(&ls->ls_los_mutex);
1289 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1290 mutex_unlock(&ls->ls_los_mutex);
1292 ls_device_put(env, ls);
1296 if (logid != NULL) {
1297 logid_to_fid(logid, &lgi->lgi_fid);
1299 struct dt_object *llog_dir;
1301 llog_dir = llog_osd_dir_get(env, ctxt);
1302 if (IS_ERR(llog_dir))
1303 GOTO(out, rc = PTR_ERR(llog_dir));
1304 dt_read_lock(env, llog_dir, 0);
1305 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1306 dt_read_unlock(env, llog_dir);
1307 dt_object_put(env, llog_dir);
1308 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1309 /* generate fid for new llog */
1310 rc = local_object_fid_generate(env, los,
1316 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1317 if (handle->lgh_name)
1318 strcpy(handle->lgh_name, name);
1320 GOTO(out, rc = -ENOMEM);
1322 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1323 /* generate fid for new llog */
1325 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1330 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1331 cfs_fail_val == 1) {
1333 OBD_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
1334 msleep(MSEC_PER_SEC);
1336 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1338 GOTO(out_name, rc = PTR_ERR(o));
1340 if (dt_object_exists(o) && new_id) {
1341 /* llog exists with just generated ID, e.g. some old llog file
1342 * still is in use or is orphan, drop a warn and skip it. */
1343 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1345 o->do_lu.lo_dev->ld_obd->obd_name,
1346 PFID(lu_object_fid(&o->do_lu)));
1347 dt_object_put(env, o);
1348 /* just skip this llog ID, we shouldn't delete it because we
1349 * don't know exactly what is its purpose and state. */
1354 /* No new llog is expected but doesn't exist */
1355 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1356 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1357 o->do_lu.lo_dev->ld_obd->obd_name,
1358 PFID(lu_object_fid(&o->do_lu)), o);
1359 GOTO(out_put, rc = -ENOENT);
1361 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1362 handle->lgh_obj = o;
1363 handle->private_data = los;
1364 LASSERT(handle->lgh_ctxt);
1369 dt_object_put(env, o);
1371 if (handle->lgh_name != NULL)
1372 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1380 * Get dir for regular fid log object
1382 * Get directory for regular fid log object, and these regular fid log
1383 * object will be inserted under this directory, to satisfy the FS
1384 * consistency check, e2fsck etc.
1386 * \param [in] env execution environment
1387 * \param [in] dto llog object
1389 * \retval pointer to the directory if it is found.
1390 * \retval ERR_PTR(negative errno) if it fails.
1392 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1393 struct dt_object *dto)
1395 struct llog_thread_info *lgi = llog_info(env);
1396 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1397 struct lu_seq_range *range = &lgi->lgi_range;
1398 struct lu_fid *dir_fid = &lgi->lgi_fid;
1399 struct dt_object *dir;
1403 fld_range_set_any(range);
1404 LASSERT(ss != NULL);
1405 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1406 fid_seq(lu_object_fid(&dto->do_lu)), range);
1408 RETURN(ERR_PTR(rc));
1410 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1411 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1415 if (!dt_try_as_dir(env, dir)) {
1416 dt_object_put(env, dir);
1417 RETURN(ERR_PTR(-ENOTDIR));
1424 * Add llog object with regular FID to name entry
1426 * Add llog object with regular FID to name space, and each llog
1427 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1428 * so to satisfy the namespace consistency check, e2fsck etc.
1430 * \param [in] env execution environment
1431 * \param [in] dto llog object
1432 * \param [in] th thandle
1433 * \param [in] declare if it is declare or execution
1435 * \retval 0 if insertion succeeds.
1436 * \retval negative errno if insertion fails.
1439 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1440 struct dt_object *dto,
1441 struct thandle *th, bool declare)
1443 struct llog_thread_info *lgi = llog_info(env);
1444 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1445 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1446 struct dt_object *dir;
1447 char *name = lgi->lgi_name;
1451 if (!fid_is_norm(fid))
1454 dir = llog_osd_get_regular_fid_dir(env, dto);
1456 RETURN(PTR_ERR(dir));
1459 rec->rec_type = S_IFREG;
1460 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1461 dt_write_lock(env, dir, 0);
1463 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1464 (struct dt_key *)name, th);
1466 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1467 (struct dt_key *)name, th);
1469 dt_write_unlock(env, dir);
1471 dt_object_put(env, dir);
1477 * Implementation of the llog_operations::lop_declare_create
1479 * This function declares the llog create. It declares also name insert
1480 * into llog directory in case of named llog.
1482 * \param[in] env execution environment
1483 * \param[in] res llog handle of the current llog
1484 * \param[in] th current transaction handle
1486 * \retval 0 on successful create declaration
1487 * \retval negative value on error
1489 static int llog_osd_declare_create(const struct lu_env *env,
1490 struct llog_handle *res, struct thandle *th)
1492 struct llog_thread_info *lgi = llog_info(env);
1493 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1494 struct local_oid_storage *los;
1495 struct dt_object *o;
1500 LASSERT(res->lgh_obj);
1503 /* object can be created by another thread */
1505 if (dt_object_exists(o))
1508 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1509 struct llog_thread_info *lgi = llog_info(env);
1511 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1512 lgi->lgi_attr.la_size = 0;
1513 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1514 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1516 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1522 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1526 los = res->private_data;
1529 rc = llog_osd_declare_new_object(env, los, o, th);
1533 /* do not declare header initialization here as it's declared
1534 * in llog_osd_declare_write_rec() which is always called */
1536 if (res->lgh_name) {
1537 struct dt_object *llog_dir;
1539 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1540 if (IS_ERR(llog_dir))
1541 RETURN(PTR_ERR(llog_dir));
1542 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1543 rec->rec_fid = &lgi->lgi_fid;
1544 rec->rec_type = S_IFREG;
1545 rc = dt_declare_insert(env, llog_dir,
1546 (struct dt_rec *)rec,
1547 (struct dt_key *)res->lgh_name, th);
1548 dt_object_put(env, llog_dir);
1550 CERROR("%s: can't declare named llog %s: rc = %d\n",
1551 o->do_lu.lo_dev->ld_obd->obd_name,
1558 * Implementation of the llog_operations::lop_create
1560 * This function creates the llog according with llog_handle::lgh_obj
1561 * and llog_handle::lgh_name.
1563 * \param[in] env execution environment
1564 * \param[in] res llog handle of the current llog
1565 * \param[in] th current transaction handle
1567 * \retval 0 on successful create
1568 * \retval negative value on error
1570 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1573 struct llog_thread_info *lgi = llog_info(env);
1574 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1575 struct local_oid_storage *los;
1576 struct dt_object *o;
1585 /* llog can be already created */
1586 if (dt_object_exists(o))
1589 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1590 struct llog_thread_info *lgi = llog_info(env);
1592 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1593 lgi->lgi_attr.la_size = 0;
1594 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1595 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1597 dt_write_lock(env, o, 0);
1598 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1600 dt_write_unlock(env, o);
1604 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1609 los = res->private_data;
1612 dt_write_lock(env, o, 0);
1613 if (!dt_object_exists(o))
1614 rc = llog_osd_create_new_object(env, los, o, th);
1618 dt_write_unlock(env, o);
1622 if (res->lgh_name) {
1623 struct dt_object *llog_dir;
1625 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1626 if (IS_ERR(llog_dir))
1627 RETURN(PTR_ERR(llog_dir));
1629 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1630 rec->rec_fid = &lgi->lgi_fid;
1631 rec->rec_type = S_IFREG;
1632 dt_read_lock(env, llog_dir, 0);
1633 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1634 (struct dt_key *)res->lgh_name, th);
1635 dt_read_unlock(env, llog_dir);
1636 dt_object_put(env, llog_dir);
1638 CERROR("%s: can't create named llog %s: rc = %d\n",
1639 o->do_lu.lo_dev->ld_obd->obd_name,
1646 * Implementation of the llog_operations::lop_close
1648 * This function closes the llog. It just put llog object and referenced
1651 * \param[in] env execution environment
1652 * \param[in] handle llog handle of the current llog
1654 * \retval 0 on successful llog close
1655 * \retval negative value on error
1657 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1659 struct local_oid_storage *los;
1664 LASSERT(handle->lgh_obj);
1666 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1667 /* Remove the object from the cache, otherwise it may
1668 * hold LOD being released during cleanup process */
1669 dt_object_put_nocache(env, handle->lgh_obj);
1670 LASSERT(handle->private_data == NULL);
1673 dt_object_put(env, handle->lgh_obj);
1675 los = handle->private_data;
1679 if (handle->lgh_name)
1680 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1686 * delete llog object name entry
1688 * Delete llog object (with regular FID) from name space (under
1691 * \param [in] env execution environment
1692 * \param [in] dto llog object
1693 * \param [in] th thandle
1694 * \param [in] declare if it is declare or execution
1696 * \retval 0 if deletion succeeds.
1697 * \retval negative errno if deletion fails.
1700 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1701 struct dt_object *dto,
1702 struct thandle *th, bool declare)
1704 struct llog_thread_info *lgi = llog_info(env);
1705 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1706 struct dt_object *dir;
1707 char *name = lgi->lgi_name;
1711 if (!fid_is_norm(fid))
1714 dir = llog_osd_get_regular_fid_dir(env, dto);
1716 RETURN(PTR_ERR(dir));
1718 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1719 dt_write_lock(env, dir, 0);
1721 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1724 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1726 dt_write_unlock(env, dir);
1728 dt_object_put(env, dir);
1733 * Implementation of the llog_operations::lop_declare_destroy
1735 * This function declare destroys the llog and deletes also entry in the
1736 * llog directory in case of named llog. Llog should be opened prior that.
1738 * \param[in] env execution environment
1739 * \param[in] loghandle llog handle of the current llog
1741 * \retval 0 on successful destroy
1742 * \retval negative value on error
1744 static int llog_osd_declare_destroy(const struct lu_env *env,
1745 struct llog_handle *loghandle,
1748 struct llog_ctxt *ctxt;
1749 struct dt_object *o, *llog_dir = NULL;
1754 ctxt = loghandle->lgh_ctxt;
1757 o = loghandle->lgh_obj;
1760 if (loghandle->lgh_name) {
1761 llog_dir = llog_osd_dir_get(env, ctxt);
1762 if (IS_ERR(llog_dir))
1763 RETURN(PTR_ERR(llog_dir));
1765 rc = dt_declare_delete(env, llog_dir,
1766 (struct dt_key *)loghandle->lgh_name,
1772 rc = dt_declare_ref_del(env, o, th);
1776 rc = dt_declare_destroy(env, o, th);
1780 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1781 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1787 if (!(IS_ERR_OR_NULL(llog_dir)))
1788 dt_object_put(env, llog_dir);
1795 * Implementation of the llog_operations::lop_destroy
1797 * This function destroys the llog and deletes also entry in the
1798 * llog directory in case of named llog. Llog should be opened prior that.
1799 * Destroy method is not part of external transaction and does everything
1802 * \param[in] env execution environment
1803 * \param[in] loghandle llog handle of the current llog
1805 * \retval 0 on successful destroy
1806 * \retval negative value on error
1808 static int llog_osd_destroy(const struct lu_env *env,
1809 struct llog_handle *loghandle, struct thandle *th)
1811 struct llog_ctxt *ctxt;
1812 struct dt_object *o, *llog_dir = NULL;
1817 ctxt = loghandle->lgh_ctxt;
1818 LASSERT(ctxt != NULL);
1820 o = loghandle->lgh_obj;
1823 dt_write_lock(env, o, 0);
1824 if (!llog_osd_exist(loghandle))
1825 GOTO(out_unlock, rc = 0);
1827 if (loghandle->lgh_name) {
1828 llog_dir = llog_osd_dir_get(env, ctxt);
1829 if (IS_ERR(llog_dir))
1830 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1832 dt_read_lock(env, llog_dir, 0);
1833 rc = dt_delete(env, llog_dir,
1834 (struct dt_key *)loghandle->lgh_name,
1836 dt_read_unlock(env, llog_dir);
1838 CERROR("%s: can't remove llog %s: rc = %d\n",
1839 o->do_lu.lo_dev->ld_obd->obd_name,
1840 loghandle->lgh_name, rc);
1841 GOTO(out_unlock, rc);
1845 dt_ref_del(env, o, th);
1846 rc = dt_destroy(env, o, th);
1848 GOTO(out_unlock, rc);
1850 loghandle->lgh_destroyed = true;
1851 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1852 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1854 GOTO(out_unlock, rc);
1858 dt_write_unlock(env, o);
1859 if (!(IS_ERR_OR_NULL(llog_dir)))
1860 dt_object_put(env, llog_dir);
1865 * Implementation of the llog_operations::lop_setup
1867 * This function setup the llog on local storage.
1869 * \param[in] env execution environment
1870 * \param[in] obd obd device the llog belongs to
1871 * \param[in] olg the llog group, it is always zero group now.
1872 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1873 * Every new llog type have to use own index.
1874 * \param[in] disk_obd the storage obd, where llog is stored.
1876 * \retval 0 on successful llog setup
1877 * \retval negative value on error
1879 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1880 struct obd_llog_group *olg, int ctxt_idx,
1881 struct obd_device *disk_obd)
1883 struct llog_thread_info *lgi = llog_info(env);
1884 struct llog_ctxt *ctxt;
1889 LASSERT(olg->olg_ctxts[ctxt_idx]);
1891 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1894 if (disk_obd == NULL)
1897 /* initialize data allowing to generate new fids,
1898 * literally we need a sequece */
1899 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1900 lgi->lgi_fid.f_oid = 1;
1901 lgi->lgi_fid.f_ver = 0;
1902 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1904 &ctxt->loc_los_nameless);
1908 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1909 lgi->lgi_fid.f_oid = 1;
1910 lgi->lgi_fid.f_ver = 0;
1911 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1913 &ctxt->loc_los_named);
1915 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1916 ctxt->loc_los_nameless = NULL;
1922 llog_ctxt_put(ctxt);
1927 * Implementation of the llog_operations::lop_cleanup
1929 * This function cleanups the llog on local storage.
1931 * \param[in] env execution environment
1932 * \param[in] ctxt the llog context
1936 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1938 if (ctxt->loc_los_nameless != NULL) {
1939 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1940 ctxt->loc_los_nameless = NULL;
1943 if (ctxt->loc_los_named != NULL) {
1944 local_oid_storage_fini(env, ctxt->loc_los_named);
1945 ctxt->loc_los_named = NULL;
1951 const struct llog_operations llog_osd_ops = {
1952 .lop_next_block = llog_osd_next_block,
1953 .lop_prev_block = llog_osd_prev_block,
1954 .lop_read_header = llog_osd_read_header,
1955 .lop_declare_destroy = llog_osd_declare_destroy,
1956 .lop_destroy = llog_osd_destroy,
1957 .lop_setup = llog_osd_setup,
1958 .lop_cleanup = llog_osd_cleanup,
1959 .lop_open = llog_osd_open,
1960 .lop_exist = llog_osd_exist,
1961 .lop_declare_create = llog_osd_declare_create,
1962 .lop_create = llog_osd_create,
1963 .lop_declare_write_rec = llog_osd_declare_write_rec,
1964 .lop_write_rec = llog_osd_write_rec,
1965 .lop_close = llog_osd_close,
1967 EXPORT_SYMBOL(llog_osd_ops);
1969 const struct llog_operations llog_common_cat_ops = {
1970 .lop_next_block = llog_osd_next_block,
1971 .lop_prev_block = llog_osd_prev_block,
1972 .lop_read_header = llog_osd_read_header,
1973 .lop_declare_destroy = llog_osd_declare_destroy,
1974 .lop_destroy = llog_osd_destroy,
1975 .lop_setup = llog_osd_setup,
1976 .lop_cleanup = llog_osd_cleanup,
1977 .lop_open = llog_osd_open,
1978 .lop_exist = llog_osd_exist,
1979 .lop_declare_create = llog_osd_declare_create,
1980 .lop_create = llog_osd_create,
1981 .lop_declare_write_rec = llog_osd_declare_write_rec,
1982 .lop_write_rec = llog_osd_write_rec,
1983 .lop_close = llog_osd_close,
1984 .lop_add = llog_cat_add_rec,
1985 .lop_declare_add = llog_cat_declare_add_rec,
1987 EXPORT_SYMBOL(llog_common_cat_ops);
1990 * Read the special file which contains the list of llog catalogs IDs
1992 * This function reads the CATALOGS file which contains the array of llog
1993 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1994 * by OST/MDT number.
1996 * \param[in] env execution environment
1997 * \param[in] d corresponding storage device
1998 * \param[in] idx position to start from, usually OST/MDT index
1999 * \param[in] count how many catalog IDs to read
2000 * \param[out] idarray the buffer for the data. If it is NULL then
2001 * function returns just number of catalog IDs
2003 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2005 * \retval 0 on successful read of catalog IDs
2006 * \retval negative value on error
2007 * \retval positive value which is number of records in
2008 * the file if \a idarray is NULL
2010 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
2011 int idx, int count, struct llog_catid *idarray,
2012 const struct lu_fid *fid)
2014 struct llog_thread_info *lgi = llog_info(env);
2015 struct dt_object *o = NULL;
2023 size = sizeof(*idarray) * count;
2024 lgi->lgi_off = idx * sizeof(*idarray);
2026 lgi->lgi_fid = *fid;
2027 o = dt_locate(env, d, &lgi->lgi_fid);
2031 if (!dt_object_exists(o)) {
2032 th = dt_trans_create(env, d);
2034 GOTO(out, rc = PTR_ERR(th));
2036 lgi->lgi_attr.la_valid = LA_MODE | LA_TYPE;
2037 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2038 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2040 th->th_wait_submit = 1;
2041 /* Make the llog object creation synchronization, so
2042 * it will be reliable to the reference, especially
2043 * for remote reference */
2046 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2049 GOTO(out_trans, rc);
2051 rc = dt_trans_start_local(env, d, th);
2053 GOTO(out_trans, rc);
2055 dt_write_lock(env, o, 0);
2056 if (!dt_object_exists(o))
2057 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2059 dt_write_unlock(env, o);
2061 dt_trans_stop(env, d, th);
2066 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2070 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2071 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2072 o->do_lu.lo_dev->ld_obd->obd_name,
2073 lgi->lgi_attr.la_mode);
2074 GOTO(out, rc = -ENOENT);
2077 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2078 (int)lgi->lgi_attr.la_size, size);
2080 /* return just number of llogs */
2081 if (idarray == NULL) {
2082 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2086 /* read for new ost index or for empty file */
2087 memset(idarray, 0, size);
2088 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2090 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2091 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2093 lgi->lgi_buf.lb_buf = idarray;
2094 lgi->lgi_buf.lb_len = size;
2095 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2096 /* -EFAULT means the llog is a sparse file. This is not an error
2097 * after arbitrary OST index is supported. */
2098 if (rc < 0 && rc != -EFAULT) {
2099 CERROR("%s: error reading CATALOGS: rc = %d\n",
2100 o->do_lu.lo_dev->ld_obd->obd_name, rc);
2106 dt_object_put(env, o);
2109 EXPORT_SYMBOL(llog_osd_get_cat_list);
2112 * Write the special file which contains the list of llog catalogs IDs
2114 * This function writes the CATALOG file which contains the array of llog
2115 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2118 * \param[in] env execution environment
2119 * \param[in] d corresponding storage device
2120 * \param[in] idx position to start from, usually OST/MDT index
2121 * \param[in] count how many catalog IDs to write
2122 * \param[out] idarray the buffer with the data to write.
2123 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2125 * \retval 0 on successful write of catalog IDs
2126 * \retval negative value on error
2128 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2129 int idx, int count, struct llog_catid *idarray,
2130 const struct lu_fid *fid)
2132 struct llog_thread_info *lgi = llog_info(env);
2133 struct dt_object *o = NULL;
2142 size = sizeof(*idarray) * count;
2143 lgi->lgi_off = idx * sizeof(*idarray);
2144 lgi->lgi_fid = *fid;
2146 o = dt_locate(env, d, &lgi->lgi_fid);
2150 if (!dt_object_exists(o))
2151 GOTO(out, rc = -ENOENT);
2153 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2157 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2158 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2159 o->do_lu.lo_dev->ld_obd->obd_name,
2160 lgi->lgi_attr.la_mode);
2161 GOTO(out, rc = -ENOENT);
2164 th = dt_trans_create(env, d);
2166 GOTO(out, rc = PTR_ERR(th));
2168 lgi->lgi_buf.lb_len = size;
2169 lgi->lgi_buf.lb_buf = idarray;
2170 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2172 GOTO(out_trans, rc);
2174 /* For update log, this happens during initialization,
2175 * see lod_sub_prep_llog(), and we need make sure catlog
2176 * file ID is written to catlist file(committed) before
2177 * cross-MDT operation write update records to catlog FILE,
2178 * otherwise, during failover these update records might
2180 if (fid_is_update_log(fid))
2183 rc = dt_trans_start_local(env, d, th);
2185 GOTO(out_trans, rc);
2187 th->th_wait_submit = 1;
2189 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2191 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2194 dt_trans_stop(env, d, th);
2196 dt_object_put(env, o);
2199 EXPORT_SYMBOL(llog_osd_put_cat_list);