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 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_DEL) &&
907 cfs_fail_val == ((unsigned long)loghandle & 0xFFFFFFFF)) {
908 OBD_RACE(OBD_FAIL_MDS_CHANGELOG_DEL);
909 msleep(MSEC_PER_SEC >> 2);
912 o = loghandle->lgh_obj;
914 dt_read_lock(env, o, 0);
915 if (!llog_osd_exist(loghandle))
916 GOTO(out, rc = -ESTALE); //object was destroyed
918 dt = lu2dt_dev(o->do_lu.lo_dev);
921 rc = dt_attr_get(env, o, &lgi->lgi_attr);
926 "looking for log index %u (cur idx %u off %llu), size %llu\n",
928 *cur_offset, lgi->lgi_attr.la_size);
930 while (*cur_offset < lgi->lgi_attr.la_size) {
931 struct llog_rec_hdr *rec, *last_rec;
932 struct llog_rec_tail *tail;
934 llog_skip_over(loghandle, cur_offset, *cur_idx,
935 next_idx, chunk_size, force_mini_rec);
937 /* read up to next llog chunk_size block */
938 lgi->lgi_buf.lb_len = chunk_size -
939 (*cur_offset & (chunk_size - 1));
940 lgi->lgi_buf.lb_buf = buf;
942 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
944 if (rc == -EBADR && !force_mini_rec)
947 CERROR("%s: can't read llog block from log "DFID
948 " offset %llu: rc = %d\n",
949 o->do_lu.lo_dev->ld_obd->obd_name,
950 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
956 /* signal the end of the valid buffer to
958 memset(buf + rc, 0, len - rc);
961 if (rc == 0) { /* end of file, nothing to do */
967 if (rc < sizeof(*tail)) {
971 CERROR("%s: invalid llog block at log id "DFID":%x "
973 o->do_lu.lo_dev->ld_obd->obd_name,
974 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
975 loghandle->lgh_id.lgl_ogen, *cur_offset);
976 GOTO(out, rc = -EINVAL);
980 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
981 lustre_swab_llog_rec(rec);
982 tail = (struct llog_rec_tail *)((char *)buf + rc -
983 sizeof(struct llog_rec_tail));
985 if (llog_verify_record(loghandle, rec)) {
987 * the block seems corrupted. make a pad record so the
988 * caller can skip the block and try with the next one
991 rec->lrh_index = next_idx;
992 rec->lrh_type = LLOG_PAD_MAGIC;
994 tail = rec_tail(rec);
996 tail->lrt_index = next_idx;
1001 /* get the last record in block */
1002 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1005 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1006 lustre_swab_llog_rec(last_rec);
1008 if (last_rec->lrh_index != tail->lrt_index) {
1009 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",
1010 o->do_lu.lo_dev->ld_obd->obd_name,
1011 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1012 loghandle->lgh_id.lgl_ogen, *cur_offset,
1013 last_rec->lrh_index, tail->lrt_index,
1015 GOTO(out, rc = -EINVAL);
1018 *cur_idx = tail->lrt_index;
1020 /* this shouldn't happen */
1021 if (tail->lrt_index == 0) {
1022 CERROR("%s: invalid llog tail at log id "DFID":%x "
1023 "offset %llu bytes %d\n",
1024 o->do_lu.lo_dev->ld_obd->obd_name,
1025 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1026 loghandle->lgh_id.lgl_ogen, *cur_offset, rc);
1027 GOTO(out, rc = -EINVAL);
1029 if (tail->lrt_index < next_idx) {
1030 last_idx = *cur_idx;
1031 last_offset = *cur_offset;
1035 /* sanity check that the start of the new buffer is no farther
1036 * than the record that we wanted. This shouldn't happen. */
1037 if (next_idx && rec->lrh_index > next_idx) {
1038 if (!force_mini_rec && next_idx > last_idx)
1041 CERROR("%s: missed desired record? %u > %u\n",
1042 o->do_lu.lo_dev->ld_obd->obd_name,
1043 rec->lrh_index, next_idx);
1044 GOTO(out, rc = -ENOENT);
1047 /* Trim unsupported extensions for compat w/ older clients */
1048 changelog_block_trim_ext(rec, last_rec, loghandle);
1053 /* Note: because there are some pad records in the
1054 * llog, so llog_skip_over() might skip too much
1055 * records, let's try skip again with minimum record */
1056 force_mini_rec = true;
1057 *cur_offset = last_offset;
1058 *cur_idx = last_idx;
1060 GOTO(out, rc = -EIO);
1062 dt_read_unlock(env, o);
1067 * Implementation of the llog_operations::lop_prev_block
1069 * This function finds the llog block to return which contains
1070 * record with required index but in reverse order - from end of llog
1072 * It is main part of reverse llog processing.
1074 * \param[in] env execution environment
1075 * \param[in] loghandle llog handle of the current llog
1076 * \param[in] prev_idx target index to find
1077 * \param[in] buf pointer to data buffer to fill
1078 * \param[in] len required len to read, it is llog_chunk_size usually.
1080 * \retval 0 on successful buffer read
1081 * \retval negative value on error
1083 static int llog_osd_prev_block(const struct lu_env *env,
1084 struct llog_handle *loghandle,
1085 int prev_idx, void *buf, int len)
1087 struct llog_thread_info *lgi = llog_info(env);
1088 struct dt_object *o;
1089 struct dt_device *dt;
1096 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1097 if (len == 0 || len & (chunk_size - 1))
1100 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1103 LASSERT(loghandle->lgh_ctxt);
1105 o = loghandle->lgh_obj;
1107 dt_read_lock(env, o, 0);
1108 if (!llog_osd_exist(loghandle))
1109 GOTO(out, rc = -ESTALE);
1111 dt = lu2dt_dev(o->do_lu.lo_dev);
1114 /* Let's only use mini record size for previous block read
1116 cur_offset = chunk_size;
1117 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1120 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1124 while (cur_offset < lgi->lgi_attr.la_size) {
1125 struct llog_rec_hdr *rec, *last_rec;
1126 struct llog_rec_tail *tail;
1128 lgi->lgi_buf.lb_len = len;
1129 lgi->lgi_buf.lb_buf = buf;
1130 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1132 CERROR("%s: can't read llog block from log "DFID
1133 " offset %llu: rc = %d\n",
1134 o->do_lu.lo_dev->ld_obd->obd_name,
1135 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1139 if (rc == 0) /* end of file, nothing to do */
1142 if (rc < sizeof(*tail)) {
1143 CERROR("%s: invalid llog block at log id "DFID":%x "
1145 o->do_lu.lo_dev->ld_obd->obd_name,
1146 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1147 loghandle->lgh_id.lgl_ogen, cur_offset);
1148 GOTO(out, rc = -EINVAL);
1152 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1153 lustre_swab_llog_rec(rec);
1155 tail = (struct llog_rec_tail *)((char *)buf + rc -
1156 sizeof(struct llog_rec_tail));
1157 /* get the last record in block */
1158 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1159 le32_to_cpu(tail->lrt_len));
1161 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1162 lustre_swab_llog_rec(last_rec);
1163 LASSERT(last_rec->lrh_index == tail->lrt_index);
1165 /* this shouldn't happen */
1166 if (tail->lrt_index == 0) {
1167 CERROR("%s: invalid llog tail at log id "DFID":%x "
1169 o->do_lu.lo_dev->ld_obd->obd_name,
1170 PFID(&loghandle->lgh_id.lgl_oi.oi_fid),
1171 loghandle->lgh_id.lgl_ogen, cur_offset);
1172 GOTO(out, rc = -EINVAL);
1174 if (tail->lrt_index < prev_idx)
1177 /* sanity check that the start of the new buffer is no farther
1178 * than the record that we wanted. This shouldn't happen. */
1179 if (rec->lrh_index > prev_idx) {
1180 CERROR("%s: missed desired record? %u > %u\n",
1181 o->do_lu.lo_dev->ld_obd->obd_name,
1182 rec->lrh_index, prev_idx);
1183 GOTO(out, rc = -ENOENT);
1186 /* Trim unsupported extensions for compat w/ older clients */
1187 changelog_block_trim_ext(rec, last_rec, loghandle);
1191 GOTO(out, rc = -EIO);
1193 dt_read_unlock(env, o);
1198 * This is helper function to get llog directory object. It is used by named
1199 * llog operations to find/insert/delete llog entry from llog directory.
1201 * \param[in] env execution environment
1202 * \param[in] ctxt llog context
1204 * \retval dt_object of llog directory
1205 * \retval ERR_PTR of negative value on error
1207 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1208 struct llog_ctxt *ctxt)
1210 struct dt_device *dt;
1211 struct dt_thread_info *dti = dt_info(env);
1212 struct dt_object *dir;
1215 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1216 if (ctxt->loc_dir == NULL) {
1217 rc = dt_root_get(env, dt, &dti->dti_fid);
1220 dir = dt_locate(env, dt, &dti->dti_fid);
1222 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
1223 dt_object_put(env, dir);
1224 return ERR_PTR(-ENOTDIR);
1227 lu_object_get(&ctxt->loc_dir->do_lu);
1228 dir = ctxt->loc_dir;
1235 * Implementation of the llog_operations::lop_open
1237 * This function opens the llog by its logid or by name, it may open also
1238 * non existent llog and assing then new id to it.
1239 * The llog_open/llog_close pair works similar to lu_object_find/put,
1240 * the object may not exist prior open. The result of open is just dt_object
1241 * in the llog header.
1243 * \param[in] env execution environment
1244 * \param[in] handle llog handle of the current llog
1245 * \param[in] logid logid of llog to open (nameless llog)
1246 * \param[in] name name of llog to open (named llog)
1247 * \param[in] open_param
1248 * LLOG_OPEN_NEW - new llog, may not exist
1249 * LLOG_OPEN_EXIST - old llog, must exist
1251 * \retval 0 on successful open, llog_handle::lgh_obj
1252 * contains the dt_object of the llog.
1253 * \retval negative value on error
1255 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1256 struct llog_logid *logid, char *name,
1257 enum llog_open_param open_param)
1259 struct llog_thread_info *lgi = llog_info(env);
1260 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1261 struct dt_object *o;
1262 struct dt_device *dt;
1263 struct ls_device *ls;
1264 struct local_oid_storage *los = NULL;
1266 bool new_id = false;
1272 LASSERT(ctxt->loc_exp);
1273 LASSERT(ctxt->loc_exp->exp_obd);
1274 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1276 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1277 struct lu_object_conf conf = { 0 };
1278 if (logid != NULL) {
1279 logid_to_fid(logid, &lgi->lgi_fid);
1281 /* If logid == NULL, then it means the caller needs
1282 * to allocate new FID (llog_cat_declare_add_rec()). */
1283 rc = dt_fid_alloc(env, dt, &lgi->lgi_fid, NULL, NULL);
1287 conf.loc_flags = LOC_F_NEW;
1290 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1291 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1298 ls = ls_device_get(dt);
1300 RETURN(PTR_ERR(ls));
1302 mutex_lock(&ls->ls_los_mutex);
1303 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1304 mutex_unlock(&ls->ls_los_mutex);
1306 ls_device_put(env, ls);
1310 if (logid != NULL) {
1311 logid_to_fid(logid, &lgi->lgi_fid);
1313 struct dt_object *llog_dir;
1315 llog_dir = llog_osd_dir_get(env, ctxt);
1316 if (IS_ERR(llog_dir))
1317 GOTO(out, rc = PTR_ERR(llog_dir));
1318 dt_read_lock(env, llog_dir, 0);
1319 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1320 dt_read_unlock(env, llog_dir);
1321 dt_object_put(env, llog_dir);
1322 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1323 /* generate fid for new llog */
1324 rc = local_object_fid_generate(env, los,
1330 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1331 if (handle->lgh_name)
1332 strcpy(handle->lgh_name, name);
1334 GOTO(out, rc = -ENOMEM);
1336 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1337 /* generate fid for new llog */
1339 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1344 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1345 cfs_fail_val == 1) {
1347 OBD_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
1348 msleep(MSEC_PER_SEC);
1350 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1352 GOTO(out_name, rc = PTR_ERR(o));
1354 if (dt_object_exists(o) && new_id) {
1355 /* llog exists with just generated ID, e.g. some old llog file
1356 * still is in use or is orphan, drop a warn and skip it. */
1357 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1359 o->do_lu.lo_dev->ld_obd->obd_name,
1360 PFID(lu_object_fid(&o->do_lu)));
1361 dt_object_put(env, o);
1362 /* just skip this llog ID, we shouldn't delete it because we
1363 * don't know exactly what is its purpose and state. */
1368 /* No new llog is expected but doesn't exist */
1369 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1370 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1371 o->do_lu.lo_dev->ld_obd->obd_name,
1372 PFID(lu_object_fid(&o->do_lu)), o);
1373 GOTO(out_put, rc = -ENOENT);
1375 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1376 handle->lgh_obj = o;
1377 handle->private_data = los;
1378 LASSERT(handle->lgh_ctxt);
1383 dt_object_put(env, o);
1385 if (handle->lgh_name != NULL)
1386 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1394 * Get dir for regular fid log object
1396 * Get directory for regular fid log object, and these regular fid log
1397 * object will be inserted under this directory, to satisfy the FS
1398 * consistency check, e2fsck etc.
1400 * \param [in] env execution environment
1401 * \param [in] dto llog object
1403 * \retval pointer to the directory if it is found.
1404 * \retval ERR_PTR(negative errno) if it fails.
1406 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1407 struct dt_object *dto)
1409 struct llog_thread_info *lgi = llog_info(env);
1410 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1411 struct lu_seq_range *range = &lgi->lgi_range;
1412 struct lu_fid *dir_fid = &lgi->lgi_fid;
1413 struct dt_object *dir;
1417 fld_range_set_any(range);
1418 LASSERT(ss != NULL);
1419 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1420 fid_seq(lu_object_fid(&dto->do_lu)), range);
1422 RETURN(ERR_PTR(rc));
1424 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1425 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1429 if (!dt_try_as_dir(env, dir)) {
1430 dt_object_put(env, dir);
1431 RETURN(ERR_PTR(-ENOTDIR));
1438 * Add llog object with regular FID to name entry
1440 * Add llog object with regular FID to name space, and each llog
1441 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1442 * so to satisfy the namespace consistency check, e2fsck etc.
1444 * \param [in] env execution environment
1445 * \param [in] dto llog object
1446 * \param [in] th thandle
1447 * \param [in] declare if it is declare or execution
1449 * \retval 0 if insertion succeeds.
1450 * \retval negative errno if insertion fails.
1453 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1454 struct dt_object *dto,
1455 struct thandle *th, bool declare)
1457 struct llog_thread_info *lgi = llog_info(env);
1458 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1459 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1460 struct dt_object *dir;
1461 char *name = lgi->lgi_name;
1465 if (!fid_is_norm(fid))
1468 dir = llog_osd_get_regular_fid_dir(env, dto);
1470 RETURN(PTR_ERR(dir));
1473 rec->rec_type = S_IFREG;
1474 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1475 dt_write_lock(env, dir, 0);
1477 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1478 (struct dt_key *)name, th);
1480 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1481 (struct dt_key *)name, th);
1483 dt_write_unlock(env, dir);
1485 dt_object_put(env, dir);
1491 * Implementation of the llog_operations::lop_declare_create
1493 * This function declares the llog create. It declares also name insert
1494 * into llog directory in case of named llog.
1496 * \param[in] env execution environment
1497 * \param[in] res llog handle of the current llog
1498 * \param[in] th current transaction handle
1500 * \retval 0 on successful create declaration
1501 * \retval negative value on error
1503 static int llog_osd_declare_create(const struct lu_env *env,
1504 struct llog_handle *res, struct thandle *th)
1506 struct llog_thread_info *lgi = llog_info(env);
1507 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1508 struct local_oid_storage *los;
1509 struct dt_object *o;
1514 LASSERT(res->lgh_obj);
1517 /* object can be created by another thread */
1519 if (dt_object_exists(o))
1522 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1523 struct llog_thread_info *lgi = llog_info(env);
1525 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1526 lgi->lgi_attr.la_size = 0;
1527 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1528 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1530 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1536 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1540 los = res->private_data;
1543 rc = llog_osd_declare_new_object(env, los, o, th);
1547 /* do not declare header initialization here as it's declared
1548 * in llog_osd_declare_write_rec() which is always called */
1550 if (res->lgh_name) {
1551 struct dt_object *llog_dir;
1553 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1554 if (IS_ERR(llog_dir))
1555 RETURN(PTR_ERR(llog_dir));
1556 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1557 rec->rec_fid = &lgi->lgi_fid;
1558 rec->rec_type = S_IFREG;
1559 rc = dt_declare_insert(env, llog_dir,
1560 (struct dt_rec *)rec,
1561 (struct dt_key *)res->lgh_name, th);
1562 dt_object_put(env, llog_dir);
1564 CERROR("%s: can't declare named llog %s: rc = %d\n",
1565 o->do_lu.lo_dev->ld_obd->obd_name,
1572 * Implementation of the llog_operations::lop_create
1574 * This function creates the llog according with llog_handle::lgh_obj
1575 * and llog_handle::lgh_name.
1577 * \param[in] env execution environment
1578 * \param[in] res llog handle of the current llog
1579 * \param[in] th current transaction handle
1581 * \retval 0 on successful create
1582 * \retval negative value on error
1584 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1587 struct llog_thread_info *lgi = llog_info(env);
1588 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1589 struct local_oid_storage *los;
1590 struct dt_object *o;
1599 /* llog can be already created */
1600 if (dt_object_exists(o))
1603 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1604 struct llog_thread_info *lgi = llog_info(env);
1606 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1607 lgi->lgi_attr.la_size = 0;
1608 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1609 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1611 dt_write_lock(env, o, 0);
1612 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1614 dt_write_unlock(env, o);
1618 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1623 los = res->private_data;
1626 dt_write_lock(env, o, 0);
1627 if (!dt_object_exists(o))
1628 rc = llog_osd_create_new_object(env, los, o, th);
1632 dt_write_unlock(env, o);
1636 if (res->lgh_name) {
1637 struct dt_object *llog_dir;
1639 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1640 if (IS_ERR(llog_dir))
1641 RETURN(PTR_ERR(llog_dir));
1643 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1644 rec->rec_fid = &lgi->lgi_fid;
1645 rec->rec_type = S_IFREG;
1646 dt_read_lock(env, llog_dir, 0);
1647 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1648 (struct dt_key *)res->lgh_name, th);
1649 dt_read_unlock(env, llog_dir);
1650 dt_object_put(env, llog_dir);
1652 CERROR("%s: can't create named llog %s: rc = %d\n",
1653 o->do_lu.lo_dev->ld_obd->obd_name,
1660 * Implementation of the llog_operations::lop_close
1662 * This function closes the llog. It just put llog object and referenced
1665 * \param[in] env execution environment
1666 * \param[in] handle llog handle of the current llog
1668 * \retval 0 on successful llog close
1669 * \retval negative value on error
1671 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1673 struct local_oid_storage *los;
1678 LASSERT(handle->lgh_obj);
1680 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1681 /* Remove the object from the cache, otherwise it may
1682 * hold LOD being released during cleanup process */
1683 dt_object_put_nocache(env, handle->lgh_obj);
1684 LASSERT(handle->private_data == NULL);
1687 dt_object_put(env, handle->lgh_obj);
1689 los = handle->private_data;
1693 if (handle->lgh_name)
1694 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1700 * delete llog object name entry
1702 * Delete llog object (with regular FID) from name space (under
1705 * \param [in] env execution environment
1706 * \param [in] dto llog object
1707 * \param [in] th thandle
1708 * \param [in] declare if it is declare or execution
1710 * \retval 0 if deletion succeeds.
1711 * \retval negative errno if deletion fails.
1714 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1715 struct dt_object *dto,
1716 struct thandle *th, bool declare)
1718 struct llog_thread_info *lgi = llog_info(env);
1719 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1720 struct dt_object *dir;
1721 char *name = lgi->lgi_name;
1725 if (!fid_is_norm(fid))
1728 dir = llog_osd_get_regular_fid_dir(env, dto);
1730 RETURN(PTR_ERR(dir));
1732 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1733 dt_write_lock(env, dir, 0);
1735 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1738 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1740 dt_write_unlock(env, dir);
1742 dt_object_put(env, dir);
1747 * Implementation of the llog_operations::lop_declare_destroy
1749 * This function declare destroys the llog and deletes also entry in the
1750 * llog directory in case of named llog. Llog should be opened prior that.
1752 * \param[in] env execution environment
1753 * \param[in] loghandle llog handle of the current llog
1755 * \retval 0 on successful destroy
1756 * \retval negative value on error
1758 static int llog_osd_declare_destroy(const struct lu_env *env,
1759 struct llog_handle *loghandle,
1762 struct llog_ctxt *ctxt;
1763 struct dt_object *o, *llog_dir = NULL;
1768 ctxt = loghandle->lgh_ctxt;
1771 o = loghandle->lgh_obj;
1774 if (loghandle->lgh_name) {
1775 llog_dir = llog_osd_dir_get(env, ctxt);
1776 if (IS_ERR(llog_dir))
1777 RETURN(PTR_ERR(llog_dir));
1779 rc = dt_declare_delete(env, llog_dir,
1780 (struct dt_key *)loghandle->lgh_name,
1786 rc = dt_declare_ref_del(env, o, th);
1790 rc = dt_declare_destroy(env, o, th);
1794 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1795 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1801 if (!(IS_ERR_OR_NULL(llog_dir)))
1802 dt_object_put(env, llog_dir);
1809 * Implementation of the llog_operations::lop_destroy
1811 * This function destroys the llog and deletes also entry in the
1812 * llog directory in case of named llog. Llog should be opened prior that.
1813 * Destroy method is not part of external transaction and does everything
1816 * \param[in] env execution environment
1817 * \param[in] loghandle llog handle of the current llog
1819 * \retval 0 on successful destroy
1820 * \retval negative value on error
1822 static int llog_osd_destroy(const struct lu_env *env,
1823 struct llog_handle *loghandle, struct thandle *th)
1825 struct llog_ctxt *ctxt;
1826 struct dt_object *o, *llog_dir = NULL;
1831 ctxt = loghandle->lgh_ctxt;
1832 LASSERT(ctxt != NULL);
1834 o = loghandle->lgh_obj;
1837 dt_write_lock(env, o, 0);
1838 if (!llog_osd_exist(loghandle))
1839 GOTO(out_unlock, rc = 0);
1841 if (loghandle->lgh_name) {
1842 llog_dir = llog_osd_dir_get(env, ctxt);
1843 if (IS_ERR(llog_dir))
1844 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1846 dt_read_lock(env, llog_dir, 0);
1847 rc = dt_delete(env, llog_dir,
1848 (struct dt_key *)loghandle->lgh_name,
1850 dt_read_unlock(env, llog_dir);
1852 CERROR("%s: can't remove llog %s: rc = %d\n",
1853 o->do_lu.lo_dev->ld_obd->obd_name,
1854 loghandle->lgh_name, rc);
1855 GOTO(out_unlock, rc);
1859 dt_ref_del(env, o, th);
1860 rc = dt_destroy(env, o, th);
1862 GOTO(out_unlock, rc);
1864 loghandle->lgh_destroyed = true;
1865 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1866 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1868 GOTO(out_unlock, rc);
1872 dt_write_unlock(env, o);
1873 if (!(IS_ERR_OR_NULL(llog_dir)))
1874 dt_object_put(env, llog_dir);
1879 * Implementation of the llog_operations::lop_setup
1881 * This function setup the llog on local storage.
1883 * \param[in] env execution environment
1884 * \param[in] obd obd device the llog belongs to
1885 * \param[in] olg the llog group, it is always zero group now.
1886 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1887 * Every new llog type have to use own index.
1888 * \param[in] disk_obd the storage obd, where llog is stored.
1890 * \retval 0 on successful llog setup
1891 * \retval negative value on error
1893 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1894 struct obd_llog_group *olg, int ctxt_idx,
1895 struct obd_device *disk_obd)
1897 struct llog_thread_info *lgi = llog_info(env);
1898 struct llog_ctxt *ctxt;
1903 LASSERT(olg->olg_ctxts[ctxt_idx]);
1905 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1908 if (disk_obd == NULL)
1911 /* initialize data allowing to generate new fids,
1912 * literally we need a sequece */
1913 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1914 lgi->lgi_fid.f_oid = 1;
1915 lgi->lgi_fid.f_ver = 0;
1916 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1918 &ctxt->loc_los_nameless);
1922 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1923 lgi->lgi_fid.f_oid = 1;
1924 lgi->lgi_fid.f_ver = 0;
1925 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1927 &ctxt->loc_los_named);
1929 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1930 ctxt->loc_los_nameless = NULL;
1936 llog_ctxt_put(ctxt);
1941 * Implementation of the llog_operations::lop_cleanup
1943 * This function cleanups the llog on local storage.
1945 * \param[in] env execution environment
1946 * \param[in] ctxt the llog context
1950 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1952 if (ctxt->loc_los_nameless != NULL) {
1953 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1954 ctxt->loc_los_nameless = NULL;
1957 if (ctxt->loc_los_named != NULL) {
1958 local_oid_storage_fini(env, ctxt->loc_los_named);
1959 ctxt->loc_los_named = NULL;
1965 const struct llog_operations llog_osd_ops = {
1966 .lop_next_block = llog_osd_next_block,
1967 .lop_prev_block = llog_osd_prev_block,
1968 .lop_read_header = llog_osd_read_header,
1969 .lop_declare_destroy = llog_osd_declare_destroy,
1970 .lop_destroy = llog_osd_destroy,
1971 .lop_setup = llog_osd_setup,
1972 .lop_cleanup = llog_osd_cleanup,
1973 .lop_open = llog_osd_open,
1974 .lop_exist = llog_osd_exist,
1975 .lop_declare_create = llog_osd_declare_create,
1976 .lop_create = llog_osd_create,
1977 .lop_declare_write_rec = llog_osd_declare_write_rec,
1978 .lop_write_rec = llog_osd_write_rec,
1979 .lop_close = llog_osd_close,
1981 EXPORT_SYMBOL(llog_osd_ops);
1983 const struct llog_operations llog_common_cat_ops = {
1984 .lop_next_block = llog_osd_next_block,
1985 .lop_prev_block = llog_osd_prev_block,
1986 .lop_read_header = llog_osd_read_header,
1987 .lop_declare_destroy = llog_osd_declare_destroy,
1988 .lop_destroy = llog_osd_destroy,
1989 .lop_setup = llog_osd_setup,
1990 .lop_cleanup = llog_osd_cleanup,
1991 .lop_open = llog_osd_open,
1992 .lop_exist = llog_osd_exist,
1993 .lop_declare_create = llog_osd_declare_create,
1994 .lop_create = llog_osd_create,
1995 .lop_declare_write_rec = llog_osd_declare_write_rec,
1996 .lop_write_rec = llog_osd_write_rec,
1997 .lop_close = llog_osd_close,
1998 .lop_add = llog_cat_add_rec,
1999 .lop_declare_add = llog_cat_declare_add_rec,
2001 EXPORT_SYMBOL(llog_common_cat_ops);
2004 * Read the special file which contains the list of llog catalogs IDs
2006 * This function reads the CATALOGS file which contains the array of llog
2007 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
2008 * by OST/MDT number.
2010 * \param[in] env execution environment
2011 * \param[in] d corresponding storage device
2012 * \param[in] idx position to start from, usually OST/MDT index
2013 * \param[in] count how many catalog IDs to read
2014 * \param[out] idarray the buffer for the data. If it is NULL then
2015 * function returns just number of catalog IDs
2017 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2019 * \retval 0 on successful read of catalog IDs
2020 * \retval negative value on error
2021 * \retval positive value which is number of records in
2022 * the file if \a idarray is NULL
2024 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
2025 int idx, int count, struct llog_catid *idarray,
2026 const struct lu_fid *fid)
2028 struct llog_thread_info *lgi = llog_info(env);
2029 struct dt_object *o = NULL;
2037 size = sizeof(*idarray) * count;
2038 lgi->lgi_off = idx * sizeof(*idarray);
2040 lgi->lgi_fid = *fid;
2041 o = dt_locate(env, d, &lgi->lgi_fid);
2045 if (!dt_object_exists(o)) {
2046 th = dt_trans_create(env, d);
2048 GOTO(out, rc = PTR_ERR(th));
2050 lgi->lgi_attr.la_valid = LA_MODE | LA_TYPE;
2051 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2052 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2054 th->th_wait_submit = 1;
2055 /* Make the llog object creation synchronization, so
2056 * it will be reliable to the reference, especially
2057 * for remote reference */
2060 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2063 GOTO(out_trans, rc);
2065 rc = dt_trans_start_local(env, d, th);
2067 GOTO(out_trans, rc);
2069 dt_write_lock(env, o, 0);
2070 if (!dt_object_exists(o))
2071 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2073 dt_write_unlock(env, o);
2075 dt_trans_stop(env, d, th);
2080 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2084 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2085 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2086 o->do_lu.lo_dev->ld_obd->obd_name,
2087 lgi->lgi_attr.la_mode);
2088 GOTO(out, rc = -ENOENT);
2091 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2092 (int)lgi->lgi_attr.la_size, size);
2094 /* return just number of llogs */
2095 if (idarray == NULL) {
2096 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2100 /* read for new ost index or for empty file */
2101 memset(idarray, 0, size);
2102 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2104 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2105 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2107 lgi->lgi_buf.lb_buf = idarray;
2108 lgi->lgi_buf.lb_len = size;
2109 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2110 /* -EFAULT means the llog is a sparse file. This is not an error
2111 * after arbitrary OST index is supported. */
2112 if (rc < 0 && rc != -EFAULT) {
2113 CERROR("%s: error reading CATALOGS: rc = %d\n",
2114 o->do_lu.lo_dev->ld_obd->obd_name, rc);
2120 dt_object_put(env, o);
2123 EXPORT_SYMBOL(llog_osd_get_cat_list);
2126 * Write the special file which contains the list of llog catalogs IDs
2128 * This function writes the CATALOG file which contains the array of llog
2129 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2132 * \param[in] env execution environment
2133 * \param[in] d corresponding storage device
2134 * \param[in] idx position to start from, usually OST/MDT index
2135 * \param[in] count how many catalog IDs to write
2136 * \param[out] idarray the buffer with the data to write.
2137 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2139 * \retval 0 on successful write of catalog IDs
2140 * \retval negative value on error
2142 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2143 int idx, int count, struct llog_catid *idarray,
2144 const struct lu_fid *fid)
2146 struct llog_thread_info *lgi = llog_info(env);
2147 struct dt_object *o = NULL;
2156 size = sizeof(*idarray) * count;
2157 lgi->lgi_off = idx * sizeof(*idarray);
2158 lgi->lgi_fid = *fid;
2160 o = dt_locate(env, d, &lgi->lgi_fid);
2164 if (!dt_object_exists(o))
2165 GOTO(out, rc = -ENOENT);
2167 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2171 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2172 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2173 o->do_lu.lo_dev->ld_obd->obd_name,
2174 lgi->lgi_attr.la_mode);
2175 GOTO(out, rc = -ENOENT);
2178 th = dt_trans_create(env, d);
2180 GOTO(out, rc = PTR_ERR(th));
2182 lgi->lgi_buf.lb_len = size;
2183 lgi->lgi_buf.lb_buf = idarray;
2184 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2186 GOTO(out_trans, rc);
2188 /* For update log, this happens during initialization,
2189 * see lod_sub_prep_llog(), and we need make sure catlog
2190 * file ID is written to catlist file(committed) before
2191 * cross-MDT operation write update records to catlog FILE,
2192 * otherwise, during failover these update records might
2194 if (fid_is_update_log(fid))
2197 rc = dt_trans_start_local(env, d, th);
2199 GOTO(out_trans, rc);
2201 th->th_wait_submit = 1;
2203 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2205 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2208 dt_trans_stop(env, d, th);
2210 dt_object_put(env, o);
2213 EXPORT_SYMBOL(llog_osd_put_cat_list);