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 dt_read_lock(env, o, 0);
226 rc = dt_attr_get(env, o, &lgi->lgi_attr);
230 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
232 if (lgi->lgi_attr.la_size == 0) {
233 CDEBUG(D_HA, "not reading header from 0-byte log\n");
234 GOTO(unlock, rc = LLOG_EEMPTY);
237 flags = handle->lgh_hdr->llh_flags;
240 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
241 lgi->lgi_buf.lb_len = handle->lgh_hdr_size;
242 rc = dt_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
243 llh_hdr = &handle->lgh_hdr->llh_hdr;
244 if (rc < sizeof(*llh_hdr) || rc < llh_hdr->lrh_len) {
245 CERROR("%s: error reading "DFID" log header size %d: rc = %d\n",
246 o->do_lu.lo_dev->ld_obd->obd_name,
247 PFID(lu_object_fid(&o->do_lu)), rc < 0 ? 0 : rc,
256 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
257 lustre_swab_llog_hdr(handle->lgh_hdr);
259 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
260 CERROR("%s: bad log %s "DFID" header magic: %#x "
261 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
262 handle->lgh_name ? handle->lgh_name : "",
263 PFID(lu_object_fid(&o->do_lu)),
264 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
265 GOTO(unlock, rc = -EIO);
266 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
267 llh_hdr->lrh_len > handle->lgh_hdr_size) {
268 CERROR("%s: incorrectly sized log %s "DFID" header: "
269 "%#x (expected at least %#x)\n"
270 "you may need to re-run lconf --write_conf.\n",
271 o->do_lu.lo_dev->ld_obd->obd_name,
272 handle->lgh_name ? handle->lgh_name : "",
273 PFID(lu_object_fid(&o->do_lu)),
274 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
275 GOTO(unlock, rc = -EIO);
276 } else if (LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index >
277 LLOG_HDR_BITMAP_SIZE(handle->lgh_hdr) ||
278 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len !=
280 CERROR("%s: incorrectly sized log %s "DFID" tailer: "
282 o->do_lu.lo_dev->ld_obd->obd_name,
283 handle->lgh_name ? handle->lgh_name : "",
284 PFID(lu_object_fid(&o->do_lu)),
285 LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_len, -EIO);
286 GOTO(unlock, rc = -EIO);
289 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
290 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
294 dt_read_unlock(env, o);
299 * Implementation of the llog_operations::lop_declare_write
301 * This function declares the new record write.
303 * \param[in] env execution environment
304 * \param[in] loghandle llog handle of the current llog
305 * \param[in] rec llog record header. This is a real header of the full
306 * llog record to write. This is the beginning of buffer
307 * to write, the length of buffer is stored in
309 * \param[in] idx index of the llog record. If \a idx == -1 then this is
310 * append case, otherwise \a idx is the index of record
312 * \param[in] th current transaction handle
314 * \retval 0 on successful declaration
315 * \retval negative error if declaration failed
317 static int llog_osd_declare_write_rec(const struct lu_env *env,
318 struct llog_handle *loghandle,
319 struct llog_rec_hdr *rec,
320 int idx, struct thandle *th)
322 struct llog_thread_info *lgi = llog_info(env);
333 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
335 o = loghandle->lgh_obj;
338 chunk_size = loghandle->lgh_ctxt->loc_chunk_size;
339 lgi->lgi_buf.lb_len = chunk_size;
340 lgi->lgi_buf.lb_buf = NULL;
341 /* each time we update header */
342 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0, th);
343 if (rc || idx == 0) /* if error or just header */
347 * the pad record can be inserted so take into account double
348 * record size: pad and the actual record into a new block
350 lgi->lgi_buf.lb_len = rec->lrh_len * 2;
351 lgi->lgi_buf.lb_buf = NULL;
352 /* XXX: implement declared window or multi-chunks approach */
353 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
359 * Implementation of the llog_operations::lop_write
361 * This function writes the new record in the llog or modify the existed one.
363 * \param[in] env execution environment
364 * \param[in] loghandle llog handle of the current llog
365 * \param[in] rec llog record header. This is a real header of
366 * the full llog record to write. This is
367 * the beginning of buffer to write, the length
368 * of buffer is stored in \a rec::lrh_len
369 * \param[in,out] reccookie pointer to the cookie to return back if needed.
370 * It is used for further cancel of this llog
372 * \param[in] idx index of the llog record. If \a idx == -1 then
373 * this is append case, otherwise \a idx is
374 * the index of record to modify
375 * \param[in] th current transaction handle
377 * \retval 0 on successful write && \a reccookie == NULL
378 * 1 on successful write && \a reccookie != NULL
379 * \retval negative error if write failed
381 static int llog_osd_write_rec(const struct lu_env *env,
382 struct llog_handle *loghandle,
383 struct llog_rec_hdr *rec,
384 struct llog_cookie *reccookie,
385 int idx, struct thandle *th)
387 struct llog_thread_info *lgi = llog_info(env);
388 struct llog_log_hdr *llh;
389 int reclen = rec->lrh_len;
391 struct llog_rec_tail *lrt;
399 llh = loghandle->lgh_hdr;
400 o = loghandle->lgh_obj;
402 chunk_size = llh->llh_hdr.lrh_len;
403 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
404 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
406 if (!llog_osd_exist(loghandle))
409 /* record length should not bigger than */
410 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
413 /* sanity check for fixed-records llog */
414 if (idx != LLOG_HEADER_IDX && (llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
415 LASSERT(llh->llh_size != 0);
416 LASSERT(llh->llh_size == reclen);
419 /* return error if osp object is stale */
420 if (idx != LLOG_HEADER_IDX && dt_object_stale(o))
422 rc = dt_attr_get(env, o, &lgi->lgi_attr);
427 * The modification case.
428 * If idx set then the record with that index must be modified.
429 * There are three cases possible:
430 * 1) the common case is the llog header update (idx == 0)
431 * 2) the llog record modification during llog process.
432 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
433 * In that case the \a loghandle::lgh_cur_offset
434 * 3) otherwise this is assumed that llog consist of records of
435 * fixed size, i.e. catalog. The llog header must has llh_size
436 * field equal to record size. The record offset is calculated
437 * just by /a idx value
439 * During modification we don't need extra header update because
440 * the bitmap and record count are not changed. The record header
441 * and tail remains the same too.
443 if (idx != LLOG_NEXT_IDX) {
444 /* llog can be empty only when first record is being written */
445 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
447 if (!test_bit_le(idx, LLOG_HDR_BITMAP(llh))) {
448 CERROR("%s: modify unset record %u\n",
449 o->do_lu.lo_dev->ld_obd->obd_name, idx);
453 if (idx != rec->lrh_index) {
454 CERROR("%s: modify index mismatch %d %u\n",
455 o->do_lu.lo_dev->ld_obd->obd_name, idx,
460 if (idx == LLOG_HEADER_IDX) {
461 /* llog header update */
462 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
466 /* If it does not indicate the bitmap index
467 * (reccookie == NULL), then it means update
468 * the whole update header. Otherwise only
469 * update header and bits needs to be updated,
470 * and in DNE cases, it will signaficantly
471 * shrink the RPC size.
472 * see distribute_txn_cancel_records()*/
473 if (reccookie == NULL) {
474 lgi->lgi_buf.lb_len = reclen;
475 lgi->lgi_buf.lb_buf = rec;
476 rc = dt_record_write(env, o, &lgi->lgi_buf,
481 /* update the header */
482 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
483 lgi->lgi_buf.lb_buf = llh;
484 rc = dt_record_write(env, o, &lgi->lgi_buf,
489 /* update the bitmap */
490 index = reccookie->lgc_index;
491 lgi->lgi_off = llh->llh_bitmap_offset +
492 (index / (sizeof(*bitmap) * 8)) *
494 lgi->lgi_buf.lb_len = sizeof(*bitmap);
495 lgi->lgi_buf.lb_buf =
496 &bitmap[index/(sizeof(*bitmap)*8)];
497 rc = dt_record_write(env, o, &lgi->lgi_buf,
501 } else if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
502 lgi->lgi_off = llh->llh_hdr.lrh_len +
504 } else if (reccookie != NULL && reccookie->lgc_index > 0) {
506 * The lgc_offset can be used only if index is
509 if (idx != reccookie->lgc_index) {
510 CERROR("%s: modify index mismatch %d %d\n",
511 o->do_lu.lo_dev->ld_obd->obd_name, idx,
512 reccookie->lgc_index);
516 lgi->lgi_off = reccookie->lgc_offset;
517 CDEBUG(D_OTHER, "modify record "DFID": idx:%u, "
518 "len:%u offset %llu\n",
519 PLOGID(&loghandle->lgh_id), idx,
520 rec->lrh_len, (long long)lgi->lgi_off);
522 /* This can be result of lgh_cur_idx is not set during
523 * llog processing or llh_size is not set to proper
524 * record size for fixed records llog. Therefore it is
525 * impossible to get record offset. */
526 CERROR("%s: can't get record offset, idx:%d, "
527 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
532 /* update only data, header and tail remain the same */
533 lgi->lgi_off += sizeof(struct llog_rec_hdr);
534 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
535 lgi->lgi_buf.lb_buf = REC_DATA(rec);
536 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
537 if (rc == 0 && reccookie) {
538 reccookie->lgc_lgl = loghandle->lgh_id;
539 reccookie->lgc_index = idx;
547 * The most common case of using llog. The new index is assigned to
548 * the new record, new bit is set in llog bitmap and llog count is
551 * Make sure that records don't cross a chunk boundary, so we can
552 * process them page-at-a-time if needed. If it will cross a chunk
553 * boundary, write in a fake (but referenced) entry to pad the chunk.
557 /* simulate ENOSPC when new plain llog is being added to the
559 if (OBD_FAIL_CHECK(OBD_FAIL_MDS_LLOG_CREATE_FAILED2) &&
560 llh->llh_flags & LLOG_F_IS_CAT)
563 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
564 orig_last_idx = loghandle->lgh_last_idx;
565 lgi->lgi_off = lgi->lgi_attr.la_size;
567 if (loghandle->lgh_max_size > 0 &&
568 lgi->lgi_off >= loghandle->lgh_max_size) {
569 CDEBUG(D_OTHER, "llog is getting too large (%u > %u) at %u "
570 DFID"\n", (unsigned)lgi->lgi_off,
571 loghandle->lgh_max_size, (int)loghandle->lgh_last_idx,
572 PLOGID(&loghandle->lgh_id));
573 /* this is to signal that this llog is full */
574 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
578 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
579 /* NOTE: padding is a record, but no bit is set */
580 if (left != 0 && left != reclen &&
581 left < (reclen + LLOG_MIN_REC_SIZE)) {
582 index = loghandle->lgh_last_idx + 1;
583 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
587 loghandle->lgh_last_idx++; /* for pad rec */
590 /* if it's the last idx in log file, then return -ENOSPC
591 * or wrap around if a catalog */
592 if (llog_is_full(loghandle) ||
593 unlikely(llh->llh_flags & LLOG_F_IS_CAT &&
594 OBD_FAIL_PRECHECK(OBD_FAIL_CAT_RECORDS) &&
595 loghandle->lgh_last_idx >= cfs_fail_val)) {
596 if (llh->llh_flags & LLOG_F_IS_CAT)
597 loghandle->lgh_last_idx = 0;
602 down_write(&loghandle->lgh_last_sem);
603 /* increment the last_idx along with llh_tail index, they should
604 * be equal for a llog lifetime */
605 if (OBD_FAIL_CHECK(OBD_FAIL_LLOG_ADD_GAP) && --cfs_fail_val == 0)
606 loghandle->lgh_last_idx++;
607 loghandle->lgh_last_idx++;
608 index = loghandle->lgh_last_idx;
609 LLOG_HDR_TAIL(llh)->lrt_index = index;
611 * NB: the caller should make sure only 1 process access
612 * the lgh_last_idx, e.g. append should be exclusive.
613 * Otherwise it might hit the assert.
615 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
616 rec->lrh_index = index;
618 lrt->lrt_len = rec->lrh_len;
619 lrt->lrt_index = rec->lrh_index;
621 /* the lgh_hdr_mutex protects llog header data from concurrent
622 * update/cancel, the llh_count and llh_bitmap are protected */
623 mutex_lock(&loghandle->lgh_hdr_mutex);
624 if (__test_and_set_bit_le(index, LLOG_HDR_BITMAP(llh))) {
625 CERROR("%s: index %u already set in llog bitmap "DFID"\n",
626 o->do_lu.lo_dev->ld_obd->obd_name, index,
627 PFID(lu_object_fid(&o->do_lu)));
628 mutex_unlock(&loghandle->lgh_hdr_mutex);
629 LBUG(); /* should never happen */
633 if (!(llh->llh_flags & LLOG_F_IS_FIXSIZE)) {
634 /* Update the minimum size of the llog record */
635 if (llh->llh_size == 0)
636 llh->llh_size = reclen;
637 else if (reclen < llh->llh_size)
638 llh->llh_size = reclen;
642 * readers (e.g. llog_osd_read_header()) must not find
643 * llog updated partially (bitmap/counter claims record,
644 * but a record hasn't been added yet) as this results
647 dt_write_lock(env, o, 0);
649 if (lgi->lgi_attr.la_size == 0) {
651 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
652 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
653 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
655 GOTO(out_unlock, rc);
657 __u32 *bitmap = LLOG_HDR_BITMAP(llh);
659 /* Note: If this is not initialization (size == 0), then do not
660 * write the whole header (8k bytes), only update header/tail
661 * and bits needs to be updated. Because this update might be
662 * part of cross-MDT operation, which needs to write these
663 * updates into the update log(32KB limit) and also pack inside
664 * the RPC (1MB limit), if we write 8K for each operation, which
665 * will cost a lot space, and keep us adding more updates to one
668 lgi->lgi_buf.lb_len = llh->llh_bitmap_offset;
669 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
670 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
672 GOTO(out_unlock, rc);
674 lgi->lgi_off = llh->llh_bitmap_offset +
675 (index / (sizeof(*bitmap) * 8)) * sizeof(*bitmap);
676 lgi->lgi_buf.lb_len = sizeof(*bitmap);
677 lgi->lgi_buf.lb_buf = &bitmap[index/(sizeof(*bitmap)*8)];
678 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
680 GOTO(out_unlock, rc);
682 lgi->lgi_off = (unsigned long)LLOG_HDR_TAIL(llh) -
684 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
685 lgi->lgi_buf.lb_buf = LLOG_HDR_TAIL(llh);
686 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
688 GOTO(out_unlock, rc);
690 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PAUSE_AFTER_PAD) && pad) {
691 /* a window for concurrent llog reader, see LU-12577 */
692 OBD_FAIL_TIMEOUT(OBD_FAIL_LLOG_PAUSE_AFTER_PAD,
697 /* unlock here for remote object */
698 mutex_unlock(&loghandle->lgh_hdr_mutex);
700 dt_write_unlock(env, o);
704 if (OBD_FAIL_PRECHECK(OBD_FAIL_LLOG_PROCESS_TIMEOUT) &&
705 cfs_fail_val == (unsigned int)(loghandle->lgh_id.lgl_oi.oi.oi_id &
707 OBD_RACE(OBD_FAIL_LLOG_PROCESS_TIMEOUT);
708 msleep(1 * MSEC_PER_SEC);
710 /* computed index can be used to determine offset for fixed-size
711 * records. This also allows to handle Catalog wrap around case */
712 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
713 lgi->lgi_off = llh->llh_hdr.lrh_len + (index - 1) * reclen;
715 rc = dt_attr_get(env, o, &lgi->lgi_attr);
717 dt_write_unlock(env, o);
721 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
722 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size,
726 lgi->lgi_buf.lb_len = reclen;
727 lgi->lgi_buf.lb_buf = rec;
728 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
730 dt_write_unlock(env, o);
734 up_write(&loghandle->lgh_last_sem);
736 CDEBUG(D_HA, "added record "DFID".%u, %u off%llu\n",
737 PFID(lu_object_fid(&o->do_lu)), index, rec->lrh_len,
739 if (reccookie != NULL) {
740 reccookie->lgc_lgl = loghandle->lgh_id;
741 reccookie->lgc_index = index;
742 if ((rec->lrh_type == MDS_UNLINK_REC) ||
743 (rec->lrh_type == MDS_SETATTR64_REC))
744 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
745 else if (rec->lrh_type == OST_SZ_REC)
746 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
748 reccookie->lgc_subsys = -1;
753 /* cleanup llog for error case */
754 mutex_lock(&loghandle->lgh_hdr_mutex);
755 clear_bit_le(index, LLOG_HDR_BITMAP(llh));
757 mutex_unlock(&loghandle->lgh_hdr_mutex);
759 /* restore llog last_idx */
760 if (dt_object_remote(o)) {
761 loghandle->lgh_last_idx = orig_last_idx;
762 } else if (--loghandle->lgh_last_idx == 0 &&
763 (llh->llh_flags & LLOG_F_IS_CAT) && llh->llh_cat_idx != 0) {
764 /* catalog had just wrap-around case */
765 loghandle->lgh_last_idx = LLOG_HDR_BITMAP_SIZE(llh) - 1;
768 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
769 up_write(&loghandle->lgh_last_sem);
775 * We can skip reading at least as many log blocks as the number of
776 * minimum sized log records we are skipping. If it turns out
777 * that we are not far enough along the log (because the
778 * actual records are larger than minimum size) we just skip
781 * Note: in llog_process_thread, it will use bitmap offset as
782 * the index to locate the record, which also includs some pad
783 * records, whose record size is very small, and it also does not
784 * consider pad record when recording minimum record size (otherwise
785 * min_record size might be too small), so in some rare cases,
786 * it might skip too much record for @goal, see llog_osd_next_block().
788 * When force_mini_rec is true, it means we have to use LLOG_MIN_REC_SIZE
789 * as the min record size to skip over, usually because in the previous
790 * try, it skip too much record, see loog_osd_next(prev)_block().
792 static inline void llog_skip_over(struct llog_handle *lgh, __u64 *off,
793 int curr, int goal, __u32 chunk_size,
796 struct llog_log_hdr *llh = lgh->lgh_hdr;
798 /* Goal should not bigger than the record count */
799 if (goal > lgh->lgh_last_idx)
800 goal = lgh->lgh_last_idx;
803 if (llh->llh_flags & LLOG_F_IS_FIXSIZE) {
804 *off = chunk_size + (goal - 1) * llh->llh_size;
806 __u64 min_rec_size = LLOG_MIN_REC_SIZE;
808 if (llh->llh_size > 0 && !force_mini_rec)
809 min_rec_size = llh->llh_size;
811 *off = *off + (goal - curr - 1) * min_rec_size;
814 /* always align with lower chunk boundary*/
815 *off &= ~(chunk_size - 1);
819 * Remove optional fields that the client doesn't expect.
820 * This is typically in order to ensure compatibility with older clients.
821 * It is assumed that since we exclusively remove fields, the block will be
822 * big enough to handle the remapped records. It is also assumed that records
823 * of a block have the same format (i.e.: the same features enabled).
825 * \param[in,out] hdr Header of the block of records to remap.
826 * \param[in,out] last_hdr Last header, don't read past this point.
827 * \param[in] flags Flags describing the fields to keep.
828 * \param[in] extra_flags Flags describing the extra fields to keep.
830 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
831 struct llog_rec_hdr *last_hdr,
832 struct llog_handle *loghandle)
834 enum changelog_rec_flags flags = CLF_SUPPORTED;
835 enum changelog_rec_extra_flags extra_flags = CLFE_SUPPORTED;
837 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_XATTR))
838 extra_flags &= ~CLFE_XATTR;
839 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_OMODE))
840 extra_flags &= ~CLFE_OPEN;
841 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_NID))
842 extra_flags &= ~CLFE_NID;
843 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_X_UIDGID))
844 extra_flags &= ~CLFE_UIDGID;
845 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_EXTRA_FLAGS))
846 flags &= ~CLF_EXTRA_FLAGS;
847 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
850 if (flags == CLF_SUPPORTED && extra_flags == CLFE_SUPPORTED)
853 if (hdr->lrh_type != CHANGELOG_REC)
857 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
858 enum changelog_rec_extra_flags xflag = CLFE_INVALID;
860 if (flags & CLF_EXTRA_FLAGS &&
861 rec->cr_flags & CLF_EXTRA_FLAGS) {
862 xflag = changelog_rec_extra_flags(rec)->cr_extra_flags &
866 if (unlikely(hdr->lrh_len == 0)) {
867 /* It is corruption case, we cannot know the next rec,
868 * jump to the last one directly to avoid dead loop. */
869 LCONSOLE(D_WARNING, "Hit invalid llog record: "
870 "idx %u, type %u, id %u\n",
871 hdr->lrh_index, hdr->lrh_type, hdr->lrh_id);
872 hdr = llog_rec_hdr_next(last_hdr);
873 if (unlikely(hdr == last_hdr))
874 LCONSOLE(D_WARNING, "The last record crashed: "
875 "idx %u, type %u, id %u\n",
876 hdr->lrh_index, hdr->lrh_type,
881 changelog_remap_rec(rec, rec->cr_flags & flags, xflag);
882 hdr = llog_rec_hdr_next(hdr);
883 /* Yield CPU to avoid soft-lockup if there are too many records
886 } while ((char *)hdr <= (char *)last_hdr);
890 * Implementation of the llog_operations::lop_next_block
892 * This function finds the the next llog block to return which contains
893 * record with required index. It is main part of llog processing.
895 * \param[in] env execution environment
896 * \param[in] loghandle llog handle of the current llog
897 * \param[in,out] cur_idx index preceeding cur_offset
898 * \param[in] next_idx target index to find
899 * \param[in,out] cur_offset furtherst point read in the file
900 * \param[in] buf pointer to data buffer to fill
901 * \param[in] len required len to read, it is
902 * usually llog chunk_size.
904 * \retval 0 on successful buffer read
905 * \retval negative value on error
907 static int llog_osd_next_block(const struct lu_env *env,
908 struct llog_handle *loghandle, int *cur_idx,
909 int next_idx, __u64 *cur_offset, void *buf,
912 struct llog_thread_info *lgi = llog_info(env);
914 struct dt_device *dt;
917 int last_idx = *cur_idx;
918 __u64 last_offset = *cur_offset;
919 bool force_mini_rec = !next_idx;
926 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
927 if (len == 0 || len & (chunk_size - 1))
931 LASSERT(loghandle->lgh_ctxt);
933 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_CHANGELOG_DEL) &&
934 cfs_fail_val == ((unsigned long)loghandle & 0xFFFFFFFF)) {
935 OBD_RACE(OBD_FAIL_MDS_CHANGELOG_DEL);
936 msleep(MSEC_PER_SEC >> 2);
939 o = loghandle->lgh_obj;
941 dt_read_lock(env, o, 0);
942 if (!llog_osd_exist(loghandle))
943 GOTO(out, rc = -ESTALE); //object was destroyed
945 dt = lu2dt_dev(o->do_lu.lo_dev);
948 rc = dt_attr_get(env, o, &lgi->lgi_attr);
953 "looking for log index %u (cur idx %u off %llu), size %llu\n",
955 *cur_offset, lgi->lgi_attr.la_size);
957 while (*cur_offset < lgi->lgi_attr.la_size) {
958 struct llog_rec_hdr *rec, *last_rec;
959 struct llog_rec_tail *tail;
961 llog_skip_over(loghandle, cur_offset, *cur_idx,
962 next_idx, chunk_size, force_mini_rec);
964 /* read up to next llog chunk_size block */
965 lgi->lgi_buf.lb_len = chunk_size -
966 (*cur_offset & (chunk_size - 1));
967 lgi->lgi_buf.lb_buf = buf;
969 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
972 /* no goal is valid case */
978 CERROR("%s: can't read llog block from log "DFID
979 " offset %llu: rc = %d\n",
980 o->do_lu.lo_dev->ld_obd->obd_name,
981 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
987 /* signal the end of the valid buffer to
989 memset(buf + rc, 0, len - rc);
992 if (rc == 0) { /* end of file, nothing to do */
998 if (rc < sizeof(*tail)) {
1002 CERROR("%s: invalid llog block at log id "DFID" offset %llu\n",
1003 o->do_lu.lo_dev->ld_obd->obd_name,
1004 PLOGID(&loghandle->lgh_id), *cur_offset);
1005 GOTO(out, rc = -EINVAL);
1009 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1010 lustre_swab_llog_rec(rec);
1011 tail = (struct llog_rec_tail *)((char *)buf + rc -
1012 sizeof(struct llog_rec_tail));
1014 /* caller handles bad records if any */
1015 if (llog_verify_record(loghandle, rec))
1018 /* get the last record in block */
1019 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1022 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1023 lustre_swab_llog_rec(last_rec);
1025 if (last_rec->lrh_index != tail->lrt_index) {
1026 CERROR("%s: invalid llog tail at log id "DFID" offset %llu last_rec idx %u tail idx %u lrt len %u read_size %d\n",
1027 o->do_lu.lo_dev->ld_obd->obd_name,
1028 PLOGID(&loghandle->lgh_id), *cur_offset,
1029 last_rec->lrh_index, tail->lrt_index,
1031 GOTO(out, rc = -EINVAL);
1034 *cur_idx = tail->lrt_index;
1036 /* this shouldn't happen */
1037 if (tail->lrt_index == 0) {
1038 CERROR("%s: invalid llog tail at log id "DFID"offset %llu bytes %d\n",
1039 o->do_lu.lo_dev->ld_obd->obd_name,
1040 PLOGID(&loghandle->lgh_id), *cur_offset, rc);
1041 GOTO(out, rc = -EINVAL);
1043 if (tail->lrt_index < next_idx) {
1044 last_idx = *cur_idx;
1045 last_offset = *cur_offset;
1049 /* sanity check that the start of the new buffer is no farther
1050 * than the record that we wanted. This shouldn't happen. */
1051 if (next_idx && rec->lrh_index > next_idx) {
1052 if (!force_mini_rec && next_idx > last_idx)
1055 CERROR("%s: missed desired record? %u > %u\n",
1056 o->do_lu.lo_dev->ld_obd->obd_name,
1057 rec->lrh_index, next_idx);
1058 GOTO(out, rc = -ENOENT);
1061 /* Trim unsupported extensions for compat w/ older clients */
1062 changelog_block_trim_ext(rec, last_rec, loghandle);
1067 /* Note: because there are some pad records in the
1068 * llog, so llog_skip_over() might skip too much
1069 * records, let's try skip again with minimum record */
1070 force_mini_rec = true;
1071 *cur_offset = last_offset;
1072 *cur_idx = last_idx;
1074 /* being here means we reach end of llog but didn't find needed idx
1075 * normally could happen while processing remote llog, return -EBADR
1076 * to indicate access beyond end of file like dt_read() does and to
1077 * distunguish this situation from real IO or network issues.
1079 GOTO(out, rc = -EBADR);
1081 dt_read_unlock(env, o);
1086 * Implementation of the llog_operations::lop_prev_block
1088 * This function finds the llog block to return which contains
1089 * record with required index but in reverse order - from end of llog
1091 * It is main part of reverse llog processing.
1093 * \param[in] env execution environment
1094 * \param[in] loghandle llog handle of the current llog
1095 * \param[in] prev_idx target index to find
1096 * \param[in] buf pointer to data buffer to fill
1097 * \param[in] len required len to read, it is llog_chunk_size usually.
1099 * \retval 0 on successful buffer read
1100 * \retval negative value on error
1102 static int llog_osd_prev_block(const struct lu_env *env,
1103 struct llog_handle *loghandle,
1104 int prev_idx, void *buf, int len)
1106 struct llog_thread_info *lgi = llog_info(env);
1107 struct dt_object *o;
1108 struct dt_device *dt;
1115 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
1116 if (len == 0 || len & (chunk_size - 1))
1119 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
1122 LASSERT(loghandle->lgh_ctxt);
1124 o = loghandle->lgh_obj;
1126 dt_read_lock(env, o, 0);
1127 if (!llog_osd_exist(loghandle))
1128 GOTO(out, rc = -ESTALE);
1130 dt = lu2dt_dev(o->do_lu.lo_dev);
1133 /* Let's only use mini record size for previous block read
1135 cur_offset = chunk_size;
1136 llog_skip_over(loghandle, &cur_offset, 0, prev_idx,
1139 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1143 while (cur_offset < lgi->lgi_attr.la_size) {
1144 struct llog_rec_hdr *rec, *last_rec;
1145 struct llog_rec_tail *tail;
1147 lgi->lgi_buf.lb_len = len;
1148 lgi->lgi_buf.lb_buf = buf;
1149 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
1151 CERROR("%s: can't read llog block from log "DFID
1152 " offset %llu: rc = %d\n",
1153 o->do_lu.lo_dev->ld_obd->obd_name,
1154 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
1158 if (rc == 0) /* end of file, nothing to do */
1161 if (rc < sizeof(*tail)) {
1162 CERROR("%s: invalid llog block at log id "DFID" offset %llu\n",
1163 o->do_lu.lo_dev->ld_obd->obd_name,
1164 PLOGID(&loghandle->lgh_id), cur_offset);
1165 GOTO(out, rc = -EINVAL);
1169 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
1170 lustre_swab_llog_rec(rec);
1172 tail = (struct llog_rec_tail *)((char *)buf + rc -
1173 sizeof(struct llog_rec_tail));
1174 /* get the last record in block */
1175 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
1176 le32_to_cpu(tail->lrt_len));
1178 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
1179 lustre_swab_llog_rec(last_rec);
1180 LASSERT(last_rec->lrh_index == tail->lrt_index);
1182 /* this shouldn't happen */
1183 if (tail->lrt_index == 0) {
1184 CERROR("%s: invalid llog tail at log id "DFID" offset %llu\n",
1185 o->do_lu.lo_dev->ld_obd->obd_name,
1186 PLOGID(&loghandle->lgh_id), cur_offset);
1187 GOTO(out, rc = -EINVAL);
1189 if (tail->lrt_index < prev_idx)
1192 /* sanity check that the start of the new buffer is no farther
1193 * than the record that we wanted. This shouldn't happen. */
1194 if (rec->lrh_index > prev_idx) {
1195 CERROR("%s: missed desired record? %u > %u\n",
1196 o->do_lu.lo_dev->ld_obd->obd_name,
1197 rec->lrh_index, prev_idx);
1198 GOTO(out, rc = -ENOENT);
1201 /* Trim unsupported extensions for compat w/ older clients */
1202 changelog_block_trim_ext(rec, last_rec, loghandle);
1206 GOTO(out, rc = -EIO);
1208 dt_read_unlock(env, o);
1213 * This is helper function to get llog directory object. It is used by named
1214 * llog operations to find/insert/delete llog entry from llog directory.
1216 * \param[in] env execution environment
1217 * \param[in] ctxt llog context
1219 * \retval dt_object of llog directory
1220 * \retval ERR_PTR of negative value on error
1222 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
1223 struct llog_ctxt *ctxt)
1225 struct dt_device *dt;
1226 struct dt_thread_info *dti = dt_info(env);
1227 struct dt_object *dir;
1230 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1231 if (ctxt->loc_dir == NULL) {
1232 rc = dt_root_get(env, dt, &dti->dti_fid);
1235 dir = dt_locate(env, dt, &dti->dti_fid);
1237 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir, true)) {
1238 dt_object_put(env, dir);
1239 return ERR_PTR(-ENOTDIR);
1242 lu_object_get(&ctxt->loc_dir->do_lu);
1243 dir = ctxt->loc_dir;
1250 * Implementation of the llog_operations::lop_open
1252 * This function opens the llog by its logid or by name, it may open also
1253 * non existent llog and assing then new id to it.
1254 * The llog_open/llog_close pair works similar to lu_object_find/put,
1255 * the object may not exist prior open. The result of open is just dt_object
1256 * in the llog header.
1258 * \param[in] env execution environment
1259 * \param[in] handle llog handle of the current llog
1260 * \param[in] logid logid of llog to open (nameless llog)
1261 * \param[in] name name of llog to open (named llog)
1262 * \param[in] open_param
1263 * LLOG_OPEN_NEW - new llog, may not exist
1264 * LLOG_OPEN_EXIST - old llog, must exist
1266 * \retval 0 on successful open, llog_handle::lgh_obj
1267 * contains the dt_object of the llog.
1268 * \retval negative value on error
1270 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
1271 struct llog_logid *logid, char *name,
1272 enum llog_open_param open_param)
1274 struct llog_thread_info *lgi = llog_info(env);
1275 struct llog_ctxt *ctxt = handle->lgh_ctxt;
1276 struct dt_object *o;
1277 struct dt_device *dt;
1278 struct ls_device *ls;
1279 struct local_oid_storage *los = NULL;
1281 bool new_id = false;
1287 LASSERT(ctxt->loc_exp);
1288 LASSERT(ctxt->loc_exp->exp_obd);
1289 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1291 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1292 struct lu_object_conf conf = { 0 };
1293 if (logid != NULL) {
1294 logid_to_fid(logid, &lgi->lgi_fid);
1296 /* If logid == NULL, then it means the caller needs
1297 * to allocate new FID (llog_cat_declare_add_rec()). */
1298 rc = dt_fid_alloc(env, dt, &lgi->lgi_fid, NULL, NULL);
1302 conf.loc_flags = LOC_F_NEW;
1305 o = dt_locate_at(env, dt, &lgi->lgi_fid,
1306 dt->dd_lu_dev.ld_site->ls_top_dev, &conf);
1313 ls = ls_device_get(dt);
1315 RETURN(PTR_ERR(ls));
1317 mutex_lock(&ls->ls_los_mutex);
1318 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1319 mutex_unlock(&ls->ls_los_mutex);
1321 ls_device_put(env, ls);
1325 if (logid != NULL) {
1326 logid_to_fid(logid, &lgi->lgi_fid);
1328 struct dt_object *llog_dir;
1330 llog_dir = llog_osd_dir_get(env, ctxt);
1331 if (IS_ERR(llog_dir))
1332 GOTO(out, rc = PTR_ERR(llog_dir));
1333 dt_read_lock(env, llog_dir, 0);
1334 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1335 dt_read_unlock(env, llog_dir);
1336 dt_object_put(env, llog_dir);
1337 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1338 /* generate fid for new llog */
1339 rc = local_object_fid_generate(env, los,
1345 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1346 if (handle->lgh_name)
1347 strcpy(handle->lgh_name, name);
1349 GOTO(out, rc = -ENOMEM);
1351 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1352 /* generate fid for new llog */
1354 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1359 if (OBD_FAIL_PRECHECK(OBD_FAIL_MDS_LLOG_UMOUNT_RACE) &&
1360 cfs_fail_val == 1) {
1362 OBD_RACE(OBD_FAIL_MDS_LLOG_UMOUNT_RACE);
1363 msleep(MSEC_PER_SEC);
1365 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1367 GOTO(out_name, rc = PTR_ERR(o));
1369 if (dt_object_exists(o) && new_id) {
1370 /* llog exists with just generated ID, e.g. some old llog file
1371 * still is in use or is orphan, drop a warn and skip it. */
1372 CDEBUG(D_INFO, "%s: llog exists with the same FID: "DFID
1374 o->do_lu.lo_dev->ld_obd->obd_name,
1375 PFID(lu_object_fid(&o->do_lu)));
1376 dt_object_put(env, o);
1377 /* just skip this llog ID, we shouldn't delete it because we
1378 * don't know exactly what is its purpose and state. */
1383 /* No new llog is expected but doesn't exist */
1384 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o)) {
1385 CDEBUG(D_INFO, "%s: llog FID: "DFID" obj %p doesn`t exist\n",
1386 o->do_lu.lo_dev->ld_obd->obd_name,
1387 PFID(lu_object_fid(&o->do_lu)), o);
1388 GOTO(out_put, rc = -ENOENT);
1390 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1391 handle->lgh_obj = o;
1392 handle->private_data = los;
1393 LASSERT(handle->lgh_ctxt);
1398 dt_object_put(env, o);
1400 if (handle->lgh_name != NULL)
1401 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1409 * Get dir for regular fid log object
1411 * Get directory for regular fid log object, and these regular fid log
1412 * object will be inserted under this directory, to satisfy the FS
1413 * consistency check, e2fsck etc.
1415 * \param [in] env execution environment
1416 * \param [in] dto llog object
1418 * \retval pointer to the directory if it is found.
1419 * \retval ERR_PTR(negative errno) if it fails.
1421 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1422 struct dt_object *dto)
1424 struct llog_thread_info *lgi = llog_info(env);
1425 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1426 struct lu_seq_range *range = &lgi->lgi_range;
1427 struct lu_fid *dir_fid = &lgi->lgi_fid;
1428 struct dt_object *dir;
1432 fld_range_set_any(range);
1433 LASSERT(ss != NULL);
1434 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1435 fid_seq(lu_object_fid(&dto->do_lu)), range);
1437 RETURN(ERR_PTR(rc));
1439 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1440 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1444 if (!dt_try_as_dir(env, dir, true)) {
1445 dt_object_put(env, dir);
1446 RETURN(ERR_PTR(-ENOTDIR));
1453 * Add llog object with regular FID to name entry
1455 * Add llog object with regular FID to name space, and each llog
1456 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1457 * so to satisfy the namespace consistency check, e2fsck etc.
1459 * \param [in] env execution environment
1460 * \param [in] dto llog object
1461 * \param [in] th thandle
1462 * \param [in] declare if it is declare or execution
1464 * \retval 0 if insertion succeeds.
1465 * \retval negative errno if insertion fails.
1468 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1469 struct dt_object *dto,
1470 struct thandle *th, bool declare)
1472 struct llog_thread_info *lgi = llog_info(env);
1473 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1474 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1475 struct dt_object *dir;
1476 char *name = lgi->lgi_name;
1480 if (!fid_is_norm(fid))
1483 dir = llog_osd_get_regular_fid_dir(env, dto);
1485 RETURN(PTR_ERR(dir));
1488 rec->rec_type = S_IFREG;
1489 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1490 dt_write_lock(env, dir, 0);
1492 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1493 (struct dt_key *)name, th);
1495 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1496 (struct dt_key *)name, th);
1498 dt_write_unlock(env, dir);
1500 dt_object_put(env, dir);
1506 * Implementation of the llog_operations::lop_declare_create
1508 * This function declares the llog create. It declares also name insert
1509 * into llog directory in case of named llog.
1511 * \param[in] env execution environment
1512 * \param[in] res llog handle of the current llog
1513 * \param[in] th current transaction handle
1515 * \retval 0 on successful create declaration
1516 * \retval negative value on error
1518 static int llog_osd_declare_create(const struct lu_env *env,
1519 struct llog_handle *res, struct thandle *th)
1521 struct llog_thread_info *lgi = llog_info(env);
1522 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1523 struct local_oid_storage *los;
1524 struct dt_object *o;
1529 LASSERT(res->lgh_obj);
1532 /* object can be created by another thread */
1534 if (dt_object_exists(o))
1537 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1538 struct llog_thread_info *lgi = llog_info(env);
1540 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1541 lgi->lgi_attr.la_size = 0;
1542 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1543 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1545 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1551 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1555 los = res->private_data;
1558 rc = llog_osd_declare_new_object(env, los, o, th);
1562 /* do not declare header initialization here as it's declared
1563 * in llog_osd_declare_write_rec() which is always called */
1565 if (res->lgh_name) {
1566 struct dt_object *llog_dir;
1568 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1569 if (IS_ERR(llog_dir))
1570 RETURN(PTR_ERR(llog_dir));
1571 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1572 rec->rec_fid = &lgi->lgi_fid;
1573 rec->rec_type = S_IFREG;
1574 rc = dt_declare_insert(env, llog_dir,
1575 (struct dt_rec *)rec,
1576 (struct dt_key *)res->lgh_name, th);
1577 dt_object_put(env, llog_dir);
1579 CERROR("%s: can't declare named llog %s: rc = %d\n",
1580 o->do_lu.lo_dev->ld_obd->obd_name,
1587 * Implementation of the llog_operations::lop_create
1589 * This function creates the llog according with llog_handle::lgh_obj
1590 * and llog_handle::lgh_name.
1592 * \param[in] env execution environment
1593 * \param[in] res llog handle of the current llog
1594 * \param[in] th current transaction handle
1596 * \retval 0 on successful create
1597 * \retval negative value on error
1599 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1602 struct llog_thread_info *lgi = llog_info(env);
1603 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1604 struct local_oid_storage *los;
1605 struct dt_object *o;
1614 /* llog can be already created */
1615 if (dt_object_exists(o))
1618 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1619 struct llog_thread_info *lgi = llog_info(env);
1621 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1622 lgi->lgi_attr.la_size = 0;
1623 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1624 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1626 dt_write_lock(env, o, 0);
1627 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1629 dt_write_unlock(env, o);
1633 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1638 los = res->private_data;
1641 dt_write_lock(env, o, 0);
1642 if (!dt_object_exists(o))
1643 rc = llog_osd_create_new_object(env, los, o, th);
1647 dt_write_unlock(env, o);
1651 if (res->lgh_name) {
1652 struct dt_object *llog_dir;
1654 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1655 if (IS_ERR(llog_dir))
1656 RETURN(PTR_ERR(llog_dir));
1658 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1659 rec->rec_fid = &lgi->lgi_fid;
1660 rec->rec_type = S_IFREG;
1661 dt_read_lock(env, llog_dir, 0);
1662 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1663 (struct dt_key *)res->lgh_name, th);
1664 dt_read_unlock(env, llog_dir);
1665 dt_object_put(env, llog_dir);
1667 CERROR("%s: can't create named llog %s: rc = %d\n",
1668 o->do_lu.lo_dev->ld_obd->obd_name,
1675 * Implementation of the llog_operations::lop_close
1677 * This function closes the llog. It just put llog object and referenced
1680 * \param[in] env execution environment
1681 * \param[in] handle llog handle of the current llog
1683 * \retval 0 on successful llog close
1684 * \retval negative value on error
1686 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1688 struct local_oid_storage *los;
1693 LASSERT(handle->lgh_obj);
1695 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1696 /* Remove the object from the cache, otherwise it may
1697 * hold LOD being released during cleanup process */
1698 dt_object_put_nocache(env, handle->lgh_obj);
1699 LASSERT(handle->private_data == NULL);
1702 dt_object_put(env, handle->lgh_obj);
1704 los = handle->private_data;
1708 if (handle->lgh_name)
1709 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1715 * delete llog object name entry
1717 * Delete llog object (with regular FID) from name space (under
1720 * \param [in] env execution environment
1721 * \param [in] dto llog object
1722 * \param [in] th thandle
1723 * \param [in] declare if it is declare or execution
1725 * \retval 0 if deletion succeeds.
1726 * \retval negative errno if deletion fails.
1729 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1730 struct dt_object *dto,
1731 struct thandle *th, bool declare)
1733 struct llog_thread_info *lgi = llog_info(env);
1734 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1735 struct dt_object *dir;
1736 char *name = lgi->lgi_name;
1740 if (!fid_is_norm(fid))
1743 dir = llog_osd_get_regular_fid_dir(env, dto);
1745 RETURN(PTR_ERR(dir));
1747 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1748 dt_write_lock(env, dir, 0);
1750 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1753 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1755 dt_write_unlock(env, dir);
1757 dt_object_put(env, dir);
1762 * Implementation of the llog_operations::lop_declare_destroy
1764 * This function declare destroys the llog and deletes also entry in the
1765 * llog directory in case of named llog. Llog should be opened prior that.
1767 * \param[in] env execution environment
1768 * \param[in] loghandle llog handle of the current llog
1770 * \retval 0 on successful destroy
1771 * \retval negative value on error
1773 static int llog_osd_declare_destroy(const struct lu_env *env,
1774 struct llog_handle *loghandle,
1777 struct llog_ctxt *ctxt;
1778 struct dt_object *o, *llog_dir = NULL;
1783 ctxt = loghandle->lgh_ctxt;
1786 o = loghandle->lgh_obj;
1789 if (loghandle->lgh_name) {
1790 llog_dir = llog_osd_dir_get(env, ctxt);
1791 if (IS_ERR(llog_dir))
1792 RETURN(PTR_ERR(llog_dir));
1794 rc = dt_declare_delete(env, llog_dir,
1795 (struct dt_key *)loghandle->lgh_name,
1801 rc = dt_declare_ref_del(env, o, th);
1805 rc = dt_declare_destroy(env, o, th);
1809 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1810 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1816 if (!(IS_ERR_OR_NULL(llog_dir)))
1817 dt_object_put(env, llog_dir);
1824 * Implementation of the llog_operations::lop_destroy
1826 * This function destroys the llog and deletes also entry in the
1827 * llog directory in case of named llog. Llog should be opened prior that.
1828 * Destroy method is not part of external transaction and does everything
1831 * \param[in] env execution environment
1832 * \param[in] loghandle llog handle of the current llog
1834 * \retval 0 on successful destroy
1835 * \retval negative value on error
1837 static int llog_osd_destroy(const struct lu_env *env,
1838 struct llog_handle *loghandle, struct thandle *th)
1840 struct llog_ctxt *ctxt;
1841 struct dt_object *o, *llog_dir = NULL;
1846 ctxt = loghandle->lgh_ctxt;
1847 LASSERT(ctxt != NULL);
1849 o = loghandle->lgh_obj;
1852 dt_write_lock(env, o, 0);
1853 if (!llog_osd_exist(loghandle))
1854 GOTO(out_unlock, rc = 0);
1856 if (loghandle->lgh_name) {
1857 llog_dir = llog_osd_dir_get(env, ctxt);
1858 if (IS_ERR(llog_dir))
1859 GOTO(out_unlock, rc = PTR_ERR(llog_dir));
1861 dt_read_lock(env, llog_dir, 0);
1862 rc = dt_delete(env, llog_dir,
1863 (struct dt_key *)loghandle->lgh_name,
1865 dt_read_unlock(env, llog_dir);
1867 CERROR("%s: can't remove llog %s: rc = %d\n",
1868 o->do_lu.lo_dev->ld_obd->obd_name,
1869 loghandle->lgh_name, rc);
1870 GOTO(out_unlock, rc);
1874 dt_ref_del(env, o, th);
1875 rc = dt_destroy(env, o, th);
1877 GOTO(out_unlock, rc);
1879 loghandle->lgh_destroyed = true;
1880 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1881 rc = llog_osd_regular_fid_del_name_entry(env, o, th, false);
1883 GOTO(out_unlock, rc);
1887 dt_write_unlock(env, o);
1888 if (!(IS_ERR_OR_NULL(llog_dir)))
1889 dt_object_put(env, llog_dir);
1894 * Implementation of the llog_operations::lop_setup
1896 * This function setup the llog on local storage.
1898 * \param[in] env execution environment
1899 * \param[in] obd obd device the llog belongs to
1900 * \param[in] olg the llog group, it is always zero group now.
1901 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1902 * Every new llog type have to use own index.
1903 * \param[in] disk_obd the storage obd, where llog is stored.
1905 * \retval 0 on successful llog setup
1906 * \retval negative value on error
1908 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1909 struct obd_llog_group *olg, int ctxt_idx,
1910 struct obd_device *disk_obd)
1912 struct llog_thread_info *lgi = llog_info(env);
1913 struct llog_ctxt *ctxt;
1918 LASSERT(olg->olg_ctxts[ctxt_idx]);
1920 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1923 if (disk_obd == NULL)
1926 /* initialize data allowing to generate new fids,
1927 * literally we need a sequece */
1928 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1929 lgi->lgi_fid.f_oid = 1;
1930 lgi->lgi_fid.f_ver = 0;
1931 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1933 &ctxt->loc_los_nameless);
1937 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1938 lgi->lgi_fid.f_oid = 1;
1939 lgi->lgi_fid.f_ver = 0;
1940 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1942 &ctxt->loc_los_named);
1944 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1945 ctxt->loc_los_nameless = NULL;
1951 llog_ctxt_put(ctxt);
1956 * Implementation of the llog_operations::lop_cleanup
1958 * This function cleanups the llog on local storage.
1960 * \param[in] env execution environment
1961 * \param[in] ctxt the llog context
1965 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1967 if (ctxt->loc_los_nameless != NULL) {
1968 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1969 ctxt->loc_los_nameless = NULL;
1972 if (ctxt->loc_los_named != NULL) {
1973 local_oid_storage_fini(env, ctxt->loc_los_named);
1974 ctxt->loc_los_named = NULL;
1980 const struct llog_operations llog_osd_ops = {
1981 .lop_next_block = llog_osd_next_block,
1982 .lop_prev_block = llog_osd_prev_block,
1983 .lop_read_header = llog_osd_read_header,
1984 .lop_declare_destroy = llog_osd_declare_destroy,
1985 .lop_destroy = llog_osd_destroy,
1986 .lop_setup = llog_osd_setup,
1987 .lop_cleanup = llog_osd_cleanup,
1988 .lop_open = llog_osd_open,
1989 .lop_exist = llog_osd_exist,
1990 .lop_declare_create = llog_osd_declare_create,
1991 .lop_create = llog_osd_create,
1992 .lop_declare_write_rec = llog_osd_declare_write_rec,
1993 .lop_write_rec = llog_osd_write_rec,
1994 .lop_close = llog_osd_close,
1996 EXPORT_SYMBOL(llog_osd_ops);
1998 const struct llog_operations llog_common_cat_ops = {
1999 .lop_next_block = llog_osd_next_block,
2000 .lop_prev_block = llog_osd_prev_block,
2001 .lop_read_header = llog_osd_read_header,
2002 .lop_declare_destroy = llog_osd_declare_destroy,
2003 .lop_destroy = llog_osd_destroy,
2004 .lop_setup = llog_osd_setup,
2005 .lop_cleanup = llog_osd_cleanup,
2006 .lop_open = llog_osd_open,
2007 .lop_exist = llog_osd_exist,
2008 .lop_declare_create = llog_osd_declare_create,
2009 .lop_create = llog_osd_create,
2010 .lop_declare_write_rec = llog_osd_declare_write_rec,
2011 .lop_write_rec = llog_osd_write_rec,
2012 .lop_close = llog_osd_close,
2013 .lop_add = llog_cat_add_rec,
2014 .lop_declare_add = llog_cat_declare_add_rec,
2016 EXPORT_SYMBOL(llog_common_cat_ops);
2019 * Read the special file which contains the list of llog catalogs IDs
2021 * This function reads the CATALOGS file which contains the array of llog
2022 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
2023 * by OST/MDT number.
2025 * \param[in] env execution environment
2026 * \param[in] d corresponding storage device
2027 * \param[in] idx position to start from, usually OST/MDT index
2028 * \param[in] count how many catalog IDs to read
2029 * \param[out] idarray the buffer for the data. If it is NULL then
2030 * function returns just number of catalog IDs
2032 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2034 * \retval 0 on successful read of catalog IDs
2035 * \retval negative value on error
2036 * \retval positive value which is number of records in
2037 * the file if \a idarray is NULL
2039 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
2040 int idx, int count, struct llog_catid *idarray,
2041 const struct lu_fid *fid)
2043 struct llog_thread_info *lgi = llog_info(env);
2044 struct dt_object *o = NULL;
2052 size = sizeof(*idarray) * count;
2053 lgi->lgi_off = idx * sizeof(*idarray);
2055 lgi->lgi_fid = *fid;
2056 o = dt_locate(env, d, &lgi->lgi_fid);
2060 if (!dt_object_exists(o)) {
2061 th = dt_trans_create(env, d);
2063 GOTO(out, rc = PTR_ERR(th));
2065 lgi->lgi_attr.la_valid = LA_MODE | LA_TYPE;
2066 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
2067 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
2069 th->th_wait_submit = 1;
2070 /* Make the llog object creation synchronization, so
2071 * it will be reliable to the reference, especially
2072 * for remote reference */
2075 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
2078 GOTO(out_trans, rc);
2080 rc = dt_trans_start_local(env, d, th);
2082 GOTO(out_trans, rc);
2084 dt_write_lock(env, o, 0);
2085 if (!dt_object_exists(o))
2086 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
2088 dt_write_unlock(env, o);
2090 dt_trans_stop(env, d, th);
2095 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2099 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2100 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2101 o->do_lu.lo_dev->ld_obd->obd_name,
2102 lgi->lgi_attr.la_mode);
2103 GOTO(out, rc = -ENOENT);
2106 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
2107 (int)lgi->lgi_attr.la_size, size);
2109 /* return just number of llogs */
2110 if (idarray == NULL) {
2111 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
2115 /* read for new ost index or for empty file */
2116 memset(idarray, 0, size);
2117 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
2119 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
2120 size = lgi->lgi_attr.la_size - lgi->lgi_off;
2122 lgi->lgi_buf.lb_buf = idarray;
2123 lgi->lgi_buf.lb_len = size;
2124 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
2125 /* -EFAULT means the llog is a sparse file. This is not an error
2126 * after arbitrary OST index is supported. */
2127 if (rc < 0 && rc != -EFAULT) {
2128 CERROR("%s: error reading CATALOGS: rc = %d\n",
2129 o->do_lu.lo_dev->ld_obd->obd_name, rc);
2135 dt_object_put(env, o);
2138 EXPORT_SYMBOL(llog_osd_get_cat_list);
2141 * Write the special file which contains the list of llog catalogs IDs
2143 * This function writes the CATALOG file which contains the array of llog
2144 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
2147 * \param[in] env execution environment
2148 * \param[in] d corresponding storage device
2149 * \param[in] idx position to start from, usually OST/MDT index
2150 * \param[in] count how many catalog IDs to write
2151 * \param[out] idarray the buffer with the data to write.
2152 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
2154 * \retval 0 on successful write of catalog IDs
2155 * \retval negative value on error
2157 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
2158 int idx, int count, struct llog_catid *idarray,
2159 const struct lu_fid *fid)
2161 struct llog_thread_info *lgi = llog_info(env);
2162 struct dt_object *o = NULL;
2171 size = sizeof(*idarray) * count;
2172 lgi->lgi_off = idx * sizeof(*idarray);
2173 lgi->lgi_fid = *fid;
2175 o = dt_locate(env, d, &lgi->lgi_fid);
2179 if (!dt_object_exists(o))
2180 GOTO(out, rc = -ENOENT);
2182 rc = dt_attr_get(env, o, &lgi->lgi_attr);
2186 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
2187 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
2188 o->do_lu.lo_dev->ld_obd->obd_name,
2189 lgi->lgi_attr.la_mode);
2190 GOTO(out, rc = -ENOENT);
2193 th = dt_trans_create(env, d);
2195 GOTO(out, rc = PTR_ERR(th));
2197 lgi->lgi_buf.lb_len = size;
2198 lgi->lgi_buf.lb_buf = idarray;
2199 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
2201 GOTO(out_trans, rc);
2203 /* For update log, this happens during initialization,
2204 * see lod_sub_prep_llog(), and we need make sure catlog
2205 * file ID is written to catlist file(committed) before
2206 * cross-MDT operation write update records to catlog FILE,
2207 * otherwise, during failover these update records might
2209 if (fid_is_update_log(fid))
2212 rc = dt_trans_start_local(env, d, th);
2214 GOTO(out_trans, rc);
2216 th->th_wait_submit = 1;
2218 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
2220 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
2223 dt_trans_stop(env, d, th);
2225 dt_object_put(env, o);
2228 EXPORT_SYMBOL(llog_osd_put_cat_list);