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, 2014 Intel Corporation.
29 * This file is part of Lustre, http://www.lustre.org/
30 * Lustre is a trademark of Sun Microsystems, Inc.
33 * lustre/obdclass/llog_osd.c
35 * Low level llog routines on top of OSD API
37 * This file provides set of methods for llog operations on top of
38 * dt_device. It contains all supported llog_operations interfaces and
39 * supplimental functions.
41 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
42 * Author: Mikhail Pershin <mike.pershin@intel.com>
45 #define DEBUG_SUBSYSTEM S_LOG
48 #include <obd_class.h>
49 #include <lustre_fid.h>
50 #include <dt_object.h>
52 #include "llog_internal.h"
53 #include "local_storage.h"
56 * Implementation of the llog_operations::lop_declare_create
58 * This function is a wrapper over local_storage API function
59 * local_object_declare_create().
61 * \param[in] env execution environment
62 * \param[in] los local_storage for bottom storage device
63 * \param[in] o dt_object to create
64 * \param[in] th current transaction handle
66 * \retval 0 on successful declaration of the new object
67 * \retval negative error if declaration was failed
69 static int llog_osd_declare_new_object(const struct lu_env *env,
70 struct local_oid_storage *los,
74 struct llog_thread_info *lgi = llog_info(env);
76 lgi->lgi_attr.la_valid = LA_MODE;
77 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
78 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
80 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
85 * Implementation of the llog_operations::lop_create
87 * This function is a wrapper over local_storage API function
88 * local_object_create().
90 * \param[in] env execution environment
91 * \param[in] los local_storage for bottom storage device
92 * \param[in] o dt_object to create
93 * \param[in] th current transaction handle
95 * \retval 0 on successful creation of the new object
96 * \retval negative error if creation was failed
98 static int llog_osd_create_new_object(const struct lu_env *env,
99 struct local_oid_storage *los,
103 struct llog_thread_info *lgi = llog_info(env);
105 lgi->lgi_attr.la_valid = LA_MODE;
106 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
107 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
109 return local_object_create(env, los, o, &lgi->lgi_attr,
114 * Write a padding record to the llog
116 * This function writes a padding record to the end of llog. That may
117 * be needed if llog contains records of variable size, e.g. config logs
119 * The padding record just aligns llog to the llog chunk_size boundary if
120 * the current record doesn't fit in the remaining space.
122 * It allocates full length to avoid two separate writes for header and tail.
123 * Such 2-steps scheme needs extra protection and complex error handling.
125 * \param[in] env execution environment
126 * \param[in] o dt_object to create
127 * \param[in,out] off pointer to the padding start offset
128 * \param[in] len padding length
129 * \param[in] index index of the padding record in a llog
130 * \param[in] th current transaction handle
132 * \retval 0 on successful padding write
133 * \retval negative error if write failed
135 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
136 loff_t *off, int len, int index, struct thandle *th)
138 struct llog_thread_info *lgi = llog_info(env);
139 struct llog_rec_hdr *rec;
140 struct llog_rec_tail *tail;
147 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
154 rec->lrh_index = index;
155 rec->lrh_type = LLOG_PAD_MAGIC;
157 tail = rec_tail(rec);
159 tail->lrt_index = index;
161 lgi->lgi_buf.lb_buf = rec;
162 lgi->lgi_buf.lb_len = len;
163 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
165 CERROR("%s: error writing padding record: rc = %d\n",
166 o->do_lu.lo_dev->ld_obd->obd_name, rc);
173 * Implementation of the llog_operations::lop_read_header
175 * This function reads the current llog header from the bottom storage
178 * \param[in] env execution environment
179 * \param[in] handle llog handle of the current llog
181 * \retval 0 on successful header read
182 * \retval negative error if read failed
184 static int llog_osd_read_header(const struct lu_env *env,
185 struct llog_handle *handle)
187 struct llog_rec_hdr *llh_hdr;
189 struct llog_thread_info *lgi;
190 enum llog_flag flags;
192 __u32 max_size = handle->lgh_hdr_size;
199 lgi = llog_info(env);
201 rc = dt_attr_get(env, o, &lgi->lgi_attr);
205 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
207 if (lgi->lgi_attr.la_size == 0) {
208 CDEBUG(D_HA, "not reading header from 0-byte log\n");
212 flags = handle->lgh_hdr->llh_flags;
215 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
216 lgi->lgi_buf.lb_len = max_size;
217 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
219 CERROR("%s: error reading log header from "DFID": rc = %d\n",
220 o->do_lu.lo_dev->ld_obd->obd_name,
221 PFID(lu_object_fid(&o->do_lu)), rc);
225 llh_hdr = &handle->lgh_hdr->llh_hdr;
226 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
227 lustre_swab_llog_hdr(handle->lgh_hdr);
229 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
230 CERROR("%s: bad log %s "DFID" header magic: %#x "
231 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
232 handle->lgh_name ? handle->lgh_name : "",
233 PFID(lu_object_fid(&o->do_lu)),
234 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
236 } else if (llh_hdr->lrh_len < LLOG_MIN_CHUNK_SIZE ||
237 llh_hdr->lrh_len > max_size) {
238 CERROR("%s: incorrectly sized log %s "DFID" header: "
239 "%#x (expected at least %#x)\n"
240 "you may need to re-run lconf --write_conf.\n",
241 o->do_lu.lo_dev->ld_obd->obd_name,
242 handle->lgh_name ? handle->lgh_name : "",
243 PFID(lu_object_fid(&o->do_lu)),
244 llh_hdr->lrh_len, LLOG_MIN_CHUNK_SIZE);
248 handle->lgh_hdr->llh_flags |= (flags & LLOG_F_EXT_MASK);
249 handle->lgh_last_idx = LLOG_HDR_TAIL(handle->lgh_hdr)->lrt_index;
255 * Implementation of the llog_operations::lop_declare_write
257 * This function declares the new record write.
259 * \param[in] env execution environment
260 * \param[in] loghandle llog handle of the current llog
261 * \param[in] rec llog record header. This is a real header of the full
262 * llog record to write. This is the beginning of buffer
263 * to write, the length of buffer is stored in
265 * \param[in] idx index of the llog record. If \a idx == -1 then this is
266 * append case, otherwise \a idx is the index of record
268 * \param[in] th current transaction handle
270 * \retval 0 on successful declaration
271 * \retval negative error if declaration failed
273 static int llog_osd_declare_write_rec(const struct lu_env *env,
274 struct llog_handle *loghandle,
275 struct llog_rec_hdr *rec,
276 int idx, struct thandle *th)
278 struct llog_thread_info *lgi = llog_info(env);
288 LASSERT(rec->lrh_len <= loghandle->lgh_ctxt->loc_chunk_size);
290 o = loghandle->lgh_obj;
293 lgi->lgi_buf.lb_len = sizeof(struct llog_log_hdr);
294 lgi->lgi_buf.lb_buf = NULL;
295 /* each time we update header */
296 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, 0,
298 if (rc || idx == 0) /* if error or just header */
302 * the pad record can be inserted so take into account double
305 lgi->lgi_buf.lb_len = rec->lrh_len * 2;
306 lgi->lgi_buf.lb_buf = NULL;
307 /* XXX: implement declared window or multi-chunks approach */
308 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, -1, th);
314 * Implementation of the llog_operations::lop_write
316 * This function writes the new record in the llog or modify the existed one.
318 * \param[in] env execution environment
319 * \param[in] loghandle llog handle of the current llog
320 * \param[in] rec llog record header. This is a real header of
321 * the full llog record to write. This is
322 * the beginning of buffer to write, the length
323 * of buffer is stored in \a rec::lrh_len
324 * \param[out] reccookie pointer to the cookie to return back if needed.
325 * It is used for further cancel of this llog
327 * \param[in] idx index of the llog record. If \a idx == -1 then
328 * this is append case, otherwise \a idx is
329 * the index of record to modify
330 * \param[in] th current transaction handle
332 * \retval 0 on successful write && \a reccookie == NULL
333 * 1 on successful write && \a reccookie != NULL
334 * \retval negative error if write failed
336 static int llog_osd_write_rec(const struct lu_env *env,
337 struct llog_handle *loghandle,
338 struct llog_rec_hdr *rec,
339 struct llog_cookie *reccookie,
340 int idx, struct thandle *th)
342 struct llog_thread_info *lgi = llog_info(env);
343 struct llog_log_hdr *llh;
344 int reclen = rec->lrh_len;
346 struct llog_rec_tail *lrt;
354 llh = loghandle->lgh_hdr;
356 o = loghandle->lgh_obj;
360 chunk_size = llh->llh_hdr.lrh_len;
361 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
362 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
364 /* record length should not bigger than */
365 if (reclen > loghandle->lgh_hdr->llh_hdr.lrh_len)
368 rc = dt_attr_get(env, o, &lgi->lgi_attr);
373 * The modification case.
374 * If idx set then the record with that index must be modified.
375 * There are three cases possible:
376 * 1) the common case is the llog header update (idx == 0)
377 * 2) the llog record modification during llog process.
378 * This is indicated by the \a loghandle::lgh_cur_idx > 0.
379 * In that case the \a loghandle::lgh_cur_offset
380 * 3) otherwise this is assumed that llog consist of records of
381 * fixed size, i.e. catalog. The llog header must has llh_size
382 * field equal to record size. The record offset is calculated
383 * just by /a idx value
385 * During modification we don't need extra header update because
386 * the bitmap and record count are not changed. The record header
387 * and tail remains the same too.
389 if (idx != LLOG_NEXT_IDX) {
390 /* llog can be empty only when first record is being written */
391 LASSERT(ergo(idx > 0, lgi->lgi_attr.la_size > 0));
393 if (!ext2_test_bit(idx, LLOG_HDR_BITMAP(llh))) {
394 CERROR("%s: modify unset record %u\n",
395 o->do_lu.lo_dev->ld_obd->obd_name, idx);
399 if (idx != rec->lrh_index) {
400 CERROR("%s: modify index mismatch %d %u\n",
401 o->do_lu.lo_dev->ld_obd->obd_name, idx,
406 if (idx == LLOG_HEADER_IDX) {
407 /* llog header update */
408 LASSERT(reclen >= sizeof(struct llog_log_hdr));
409 LASSERT(rec == &llh->llh_hdr);
412 lgi->lgi_buf.lb_len = reclen;
413 lgi->lgi_buf.lb_buf = rec;
414 rc = dt_record_write(env, o, &lgi->lgi_buf,
417 } else if (loghandle->lgh_cur_idx > 0) {
419 * The lgh_cur_offset can be used only if index is
422 if (idx != loghandle->lgh_cur_idx) {
423 CERROR("%s: modify index mismatch %d %d\n",
424 o->do_lu.lo_dev->ld_obd->obd_name, idx,
425 loghandle->lgh_cur_idx);
429 lgi->lgi_off = loghandle->lgh_cur_offset;
430 CDEBUG(D_OTHER, "modify record "DOSTID": idx:%d, "
431 "len:%u offset %llu\n",
432 POSTID(&loghandle->lgh_id.lgl_oi), idx,
433 rec->lrh_len, (long long)lgi->lgi_off);
434 } else if (llh->llh_size > 0) {
435 if (llh->llh_size != rec->lrh_len) {
436 CERROR("%s: wrong record size, llh_size is %u"
437 " but record size is %u\n",
438 o->do_lu.lo_dev->ld_obd->obd_name,
439 llh->llh_size, rec->lrh_len);
442 lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
444 /* This can be result of lgh_cur_idx is not set during
445 * llog processing or llh_size is not set to proper
446 * record size for fixed records llog. Therefore it is
447 * impossible to get record offset. */
448 CERROR("%s: can't get record offset, idx:%d, "
449 "len:%u.\n", o->do_lu.lo_dev->ld_obd->obd_name,
454 /* update only data, header and tail remain the same */
455 lgi->lgi_off += sizeof(struct llog_rec_hdr);
456 lgi->lgi_buf.lb_len = REC_DATA_LEN(rec);
457 lgi->lgi_buf.lb_buf = REC_DATA(rec);
458 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
459 if (rc == 0 && reccookie) {
460 reccookie->lgc_lgl = loghandle->lgh_id;
461 reccookie->lgc_index = idx;
469 * The most common case of using llog. The new index is assigned to
470 * the new record, new bit is set in llog bitmap and llog count is
473 * Make sure that records don't cross a chunk boundary, so we can
474 * process them page-at-a-time if needed. If it will cross a chunk
475 * boundary, write in a fake (but referenced) entry to pad the chunk.
477 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
478 lgi->lgi_off = lgi->lgi_attr.la_size;
479 left = chunk_size - (lgi->lgi_off & (chunk_size - 1));
480 /* NOTE: padding is a record, but no bit is set */
481 if (left != 0 && left != reclen &&
482 left < (reclen + LLOG_MIN_REC_SIZE)) {
483 index = loghandle->lgh_last_idx + 1;
484 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
487 loghandle->lgh_last_idx++; /* for pad rec */
489 /* if it's the last idx in log file, then return -ENOSPC */
490 if (loghandle->lgh_last_idx >= LLOG_HDR_BITMAP_SIZE(llh) - 1)
493 /* increment the last_idx along with llh_tail index, they should
494 * be equal for a llog lifetime */
495 loghandle->lgh_last_idx++;
496 index = loghandle->lgh_last_idx;
497 LLOG_HDR_TAIL(llh)->lrt_index = index;
499 * NB: the caller should make sure only 1 process access
500 * the lgh_last_idx, e.g. append should be exclusive.
501 * Otherwise it might hit the assert.
503 LASSERT(index < LLOG_HDR_BITMAP_SIZE(llh));
504 rec->lrh_index = index;
506 lrt->lrt_len = rec->lrh_len;
507 lrt->lrt_index = rec->lrh_index;
509 /* the lgh_hdr_lock protects llog header data from concurrent
510 * update/cancel, the llh_count and llh_bitmap are protected */
511 down_write(&loghandle->lgh_hdr_lock);
512 if (ext2_set_bit(index, LLOG_HDR_BITMAP(llh))) {
513 CERROR("%s: index %u already set in log bitmap\n",
514 o->do_lu.lo_dev->ld_obd->obd_name, index);
515 up_write(&loghandle->lgh_hdr_lock);
516 LBUG(); /* should never happen */
520 /* XXX It is a bit tricky here, if the log object is local,
521 * we do not need lock during write here, because if there is
522 * race, the transaction(jbd2, what about ZFS?) will make sure the
523 * conflicts will all committed in the same transaction group.
524 * But for remote object, we need lock the whole process, so to
525 * set the version of the remote transaction to make sure they
526 * are being sent in order. (see osp_md_write()) */
527 if (!dt_object_remote(o))
528 up_write(&loghandle->lgh_hdr_lock);
530 if (lgi->lgi_attr.la_size == 0) {
532 lgi->lgi_buf.lb_len = llh->llh_hdr.lrh_len;
533 lgi->lgi_buf.lb_buf = &llh->llh_hdr;
534 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
536 GOTO(out_remote_unlock, rc);
538 /* Note: If this is not initialization (size == 0), then do not
539 * write the whole header (8k bytes), only update header/tail
540 * and bits needs to be updated. Because this update might be
541 * part of cross-MDT operation, which needs to write these
542 * updates into the update log(32KB limit) and also pack inside
543 * the RPC (1MB limit), if we write 8K for each operation, which
544 * will cost a lot space, and keep us adding more updates to one
546 lgi->lgi_off = offsetof(typeof(*llh), llh_count);
547 lgi->lgi_buf.lb_len = sizeof(llh->llh_count);
548 lgi->lgi_buf.lb_buf = &llh->llh_count;
549 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
551 GOTO(out_remote_unlock, rc);
553 lgi->lgi_off = offsetof(typeof(*llh),
554 llh_bitmap[index / (sizeof(*llh->llh_bitmap) * 8)]);
555 lgi->lgi_buf.lb_len = sizeof(*llh->llh_bitmap);
556 lgi->lgi_buf.lb_buf =
557 &llh->llh_bitmap[index/(sizeof(*llh->llh_bitmap)*8)];
558 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
560 GOTO(out_remote_unlock, rc);
562 lgi->lgi_off = offsetof(typeof(*llh), llh_tail);
563 lgi->lgi_buf.lb_len = sizeof(llh->llh_tail);
564 lgi->lgi_buf.lb_buf = &llh->llh_tail;
565 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
567 GOTO(out_remote_unlock, rc);
571 /* unlock here for remote object */
572 if (dt_object_remote(o))
573 up_write(&loghandle->lgh_hdr_lock);
577 rc = dt_attr_get(env, o, &lgi->lgi_attr);
581 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
582 lgi->lgi_off = max_t(__u64, lgi->lgi_attr.la_size, lgi->lgi_off);
583 lgi->lgi_buf.lb_len = reclen;
584 lgi->lgi_buf.lb_buf = rec;
585 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
589 CDEBUG(D_OTHER, "added record "DOSTID": idx: %u, %u\n",
590 POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
591 if (reccookie != NULL) {
592 reccookie->lgc_lgl = loghandle->lgh_id;
593 reccookie->lgc_index = index;
594 if ((rec->lrh_type == MDS_UNLINK_REC) ||
595 (rec->lrh_type == MDS_SETATTR64_REC))
596 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
597 else if (rec->lrh_type == OST_SZ_REC)
598 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
600 reccookie->lgc_subsys = -1;
605 /* cleanup llog for error case */
606 down_write(&loghandle->lgh_hdr_lock);
607 ext2_clear_bit(index, LLOG_HDR_BITMAP(llh));
609 up_write(&loghandle->lgh_hdr_lock);
611 /* restore llog last_idx */
612 loghandle->lgh_last_idx--;
613 LLOG_HDR_TAIL(llh)->lrt_index = loghandle->lgh_last_idx;
619 * We can skip reading at least as many log blocks as the number of
620 * minimum sized log records we are skipping. If it turns out
621 * that we are not far enough along the log (because the
622 * actual records are larger than minimum size) we just skip
625 static inline void llog_skip_over(__u64 *off, int curr, int goal,
630 *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
635 * Remove optional fields that the client doesn't expect.
636 * This is typically in order to ensure compatibility with older clients.
637 * It is assumed that since we exclusively remove fields, the block will be
638 * big enough to handle the remapped records. It is also assumed that records
639 * of a block have the same format (i.e.: the same features enabled).
641 * \param[in,out] hdr Header of the block of records to remap.
642 * \param[in,out] last_hdr Last header, don't read past this point.
643 * \param[in] flags Flags describing the fields to keep.
645 static void changelog_block_trim_ext(struct llog_rec_hdr *hdr,
646 struct llog_rec_hdr *last_hdr,
647 enum changelog_rec_flags flags)
649 if (hdr->lrh_type != CHANGELOG_REC)
653 struct changelog_rec *rec = (struct changelog_rec *)(hdr + 1);
655 changelog_remap_rec(rec, rec->cr_flags & flags);
656 hdr = llog_rec_hdr_next(hdr);
657 } while ((char *)hdr <= (char *)last_hdr);
661 * Implementation of the llog_operations::lop_next_block
663 * This function finds the the next llog block to return which contains
664 * record with required index. It is main part of llog processing.
666 * \param[in] env execution environment
667 * \param[in] loghandle llog handle of the current llog
668 * \param[in,out] cur_idx index preceeding cur_offset
669 * \param[in] next_idx target index to find
670 * \param[in,out] cur_offset furtherst point read in the file
671 * \param[in] buf pointer to data buffer to fill
672 * \param[in] len required len to read, it is
673 * usually llog chunk_size.
675 * \retval 0 on successful buffer read
676 * \retval negative value on error
678 static int llog_osd_next_block(const struct lu_env *env,
679 struct llog_handle *loghandle, int *cur_idx,
680 int next_idx, __u64 *cur_offset, void *buf,
683 struct llog_thread_info *lgi = llog_info(env);
685 struct dt_device *dt;
694 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
695 if (len == 0 || len & (chunk_size - 1))
698 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
699 next_idx, *cur_idx, *cur_offset);
702 LASSERT(loghandle->lgh_ctxt);
704 o = loghandle->lgh_obj;
706 LASSERT(dt_object_exists(o));
707 dt = lu2dt_dev(o->do_lu.lo_dev);
710 rc = dt_attr_get(env, o, &lgi->lgi_attr);
714 while (*cur_offset < lgi->lgi_attr.la_size) {
715 struct llog_rec_hdr *rec, *last_rec;
716 struct llog_rec_tail *tail;
718 llog_skip_over(cur_offset, *cur_idx, next_idx, chunk_size);
720 /* read up to next llog chunk_size block */
721 lgi->lgi_buf.lb_len = chunk_size -
722 (*cur_offset & (chunk_size - 1));
723 lgi->lgi_buf.lb_buf = buf;
725 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
727 CERROR("%s: can't read llog block from log "DFID
728 " offset "LPU64": rc = %d\n",
729 o->do_lu.lo_dev->ld_obd->obd_name,
730 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
736 /* signal the end of the valid buffer to
738 memset(buf + rc, 0, len - rc);
741 if (rc == 0) /* end of file, nothing to do */
744 if (rc < sizeof(*tail)) {
745 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
747 o->do_lu.lo_dev->ld_obd->obd_name,
748 POSTID(&loghandle->lgh_id.lgl_oi),
749 loghandle->lgh_id.lgl_ogen, *cur_offset);
750 GOTO(out, rc = -EINVAL);
754 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
755 lustre_swab_llog_rec(rec);
757 tail = (struct llog_rec_tail *)((char *)buf + rc -
758 sizeof(struct llog_rec_tail));
759 /* get the last record in block */
760 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
763 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
764 lustre_swab_llog_rec(last_rec);
765 LASSERT(last_rec->lrh_index == tail->lrt_index);
767 *cur_idx = tail->lrt_index;
769 /* this shouldn't happen */
770 if (tail->lrt_index == 0) {
771 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
773 o->do_lu.lo_dev->ld_obd->obd_name,
774 POSTID(&loghandle->lgh_id.lgl_oi),
775 loghandle->lgh_id.lgl_ogen, *cur_offset);
776 GOTO(out, rc = -EINVAL);
778 if (tail->lrt_index < next_idx)
781 /* sanity check that the start of the new buffer is no farther
782 * than the record that we wanted. This shouldn't happen. */
783 if (rec->lrh_index > next_idx) {
784 CERROR("%s: missed desired record? %u > %u\n",
785 o->do_lu.lo_dev->ld_obd->obd_name,
786 rec->lrh_index, next_idx);
787 GOTO(out, rc = -ENOENT);
790 /* Trim unsupported extensions for compat w/ older clients */
791 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
792 changelog_block_trim_ext(rec, last_rec,
793 CLF_VERSION | CLF_RENAME);
797 GOTO(out, rc = -EIO);
803 * Implementation of the llog_operations::lop_prev_block
805 * This function finds the llog block to return which contains
806 * record with required index but in reverse order - from end of llog
808 * It is main part of reverse llog processing.
810 * \param[in] env execution environment
811 * \param[in] loghandle llog handle of the current llog
812 * \param[in] prev_idx target index to find
813 * \param[in] buf pointer to data buffer to fill
814 * \param[in] len required len to read, it is llog_chunk_size usually.
816 * \retval 0 on successful buffer read
817 * \retval negative value on error
819 static int llog_osd_prev_block(const struct lu_env *env,
820 struct llog_handle *loghandle,
821 int prev_idx, void *buf, int len)
823 struct llog_thread_info *lgi = llog_info(env);
825 struct dt_device *dt;
832 chunk_size = loghandle->lgh_hdr->llh_hdr.lrh_len;
833 if (len == 0 || len & (chunk_size - 1))
836 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
839 LASSERT(loghandle->lgh_ctxt);
841 o = loghandle->lgh_obj;
843 LASSERT(dt_object_exists(o));
844 dt = lu2dt_dev(o->do_lu.lo_dev);
847 cur_offset = chunk_size;
848 llog_skip_over(&cur_offset, 0, prev_idx, chunk_size);
850 rc = dt_attr_get(env, o, &lgi->lgi_attr);
854 while (cur_offset < lgi->lgi_attr.la_size) {
855 struct llog_rec_hdr *rec, *last_rec;
856 struct llog_rec_tail *tail;
858 lgi->lgi_buf.lb_len = len;
859 lgi->lgi_buf.lb_buf = buf;
860 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
862 CERROR("%s: can't read llog block from log "DFID
863 " offset "LPU64": rc = %d\n",
864 o->do_lu.lo_dev->ld_obd->obd_name,
865 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
869 if (rc == 0) /* end of file, nothing to do */
872 if (rc < sizeof(*tail)) {
873 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
875 o->do_lu.lo_dev->ld_obd->obd_name,
876 POSTID(&loghandle->lgh_id.lgl_oi),
877 loghandle->lgh_id.lgl_ogen, cur_offset);
878 GOTO(out, rc = -EINVAL);
882 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
883 lustre_swab_llog_rec(rec);
885 tail = (struct llog_rec_tail *)((char *)buf + rc -
886 sizeof(struct llog_rec_tail));
887 /* get the last record in block */
888 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
889 le32_to_cpu(tail->lrt_len));
891 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
892 lustre_swab_llog_rec(last_rec);
893 LASSERT(last_rec->lrh_index == tail->lrt_index);
895 /* this shouldn't happen */
896 if (tail->lrt_index == 0) {
897 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
899 o->do_lu.lo_dev->ld_obd->obd_name,
900 POSTID(&loghandle->lgh_id.lgl_oi),
901 loghandle->lgh_id.lgl_ogen, cur_offset);
902 GOTO(out, rc = -EINVAL);
904 if (tail->lrt_index < prev_idx)
907 /* sanity check that the start of the new buffer is no farther
908 * than the record that we wanted. This shouldn't happen. */
909 if (rec->lrh_index > prev_idx) {
910 CERROR("%s: missed desired record? %u > %u\n",
911 o->do_lu.lo_dev->ld_obd->obd_name,
912 rec->lrh_index, prev_idx);
913 GOTO(out, rc = -ENOENT);
916 /* Trim unsupported extensions for compat w/ older clients */
917 if (!(loghandle->lgh_hdr->llh_flags & LLOG_F_EXT_JOBID))
918 changelog_block_trim_ext(rec, last_rec,
919 CLF_VERSION | CLF_RENAME);
923 GOTO(out, rc = -EIO);
929 * This is helper function to get llog directory object. It is used by named
930 * llog operations to find/insert/delete llog entry from llog directory.
932 * \param[in] env execution environment
933 * \param[in] ctxt llog context
935 * \retval dt_object of llog directory
936 * \retval ERR_PTR of negative value on error
938 static struct dt_object *llog_osd_dir_get(const struct lu_env *env,
939 struct llog_ctxt *ctxt)
941 struct dt_device *dt;
942 struct dt_thread_info *dti = dt_info(env);
943 struct dt_object *dir;
946 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
947 if (ctxt->loc_dir == NULL) {
948 rc = dt_root_get(env, dt, &dti->dti_fid);
951 dir = dt_locate(env, dt, &dti->dti_fid);
953 if (!IS_ERR(dir) && !dt_try_as_dir(env, dir)) {
954 lu_object_put(env, &dir->do_lu);
955 return ERR_PTR(-ENOTDIR);
958 lu_object_get(&ctxt->loc_dir->do_lu);
966 * Implementation of the llog_operations::lop_open
968 * This function opens the llog by its logid or by name, it may open also
969 * non existent llog and assing then new id to it.
970 * The llog_open/llog_close pair works similar to lu_object_find/put,
971 * the object may not exist prior open. The result of open is just dt_object
972 * in the llog header.
974 * \param[in] env execution environment
975 * \param[in] handle llog handle of the current llog
976 * \param[in] logid logid of llog to open (nameless llog)
977 * \param[in] name name of llog to open (named llog)
978 * \param[in] open_param
979 * LLOG_OPEN_NEW - new llog, may not exist
980 * LLOG_OPEN_EXIST - old llog, must exist
982 * \retval 0 on successful open, llog_handle::lgh_obj
983 * contains the dt_object of the llog.
984 * \retval negative value on error
986 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
987 struct llog_logid *logid, char *name,
988 enum llog_open_param open_param)
990 struct llog_thread_info *lgi = llog_info(env);
991 struct llog_ctxt *ctxt = handle->lgh_ctxt;
993 struct dt_device *dt;
994 struct ls_device *ls;
995 struct local_oid_storage *los = NULL;
1002 LASSERT(ctxt->loc_exp);
1003 LASSERT(ctxt->loc_exp->exp_obd);
1004 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1006 if (ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1007 if (logid != NULL) {
1008 logid_to_fid(logid, &lgi->lgi_fid);
1010 /* If logid == NULL, then it means the caller needs
1011 * to allocate new FID (llog_cat_declare_add_rec()). */
1012 rc = obd_fid_alloc(env, ctxt->loc_exp,
1013 &lgi->lgi_fid, NULL);
1019 o = dt_locate(env, dt, &lgi->lgi_fid);
1026 ls = ls_device_get(dt);
1028 RETURN(PTR_ERR(ls));
1030 mutex_lock(&ls->ls_los_mutex);
1031 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
1032 mutex_unlock(&ls->ls_los_mutex);
1034 ls_device_put(env, ls);
1038 if (logid != NULL) {
1039 logid_to_fid(logid, &lgi->lgi_fid);
1041 struct dt_object *llog_dir;
1043 llog_dir = llog_osd_dir_get(env, ctxt);
1044 if (IS_ERR(llog_dir))
1045 GOTO(out, rc = PTR_ERR(llog_dir));
1046 dt_read_lock(env, llog_dir, 0);
1047 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
1048 dt_read_unlock(env, llog_dir);
1049 lu_object_put(env, &llog_dir->do_lu);
1050 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
1051 /* generate fid for new llog */
1052 rc = local_object_fid_generate(env, los,
1057 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
1058 if (handle->lgh_name)
1059 strcpy(handle->lgh_name, name);
1061 GOTO(out, rc = -ENOMEM);
1063 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
1064 /* generate fid for new llog */
1065 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
1070 o = ls_locate(env, ls, &lgi->lgi_fid, NULL);
1072 GOTO(out_name, rc = PTR_ERR(o));
1075 /* No new llog is expected but doesn't exist */
1076 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
1077 GOTO(out_put, rc = -ENOENT);
1079 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
1080 handle->lgh_obj = o;
1081 handle->private_data = los;
1082 LASSERT(handle->lgh_ctxt);
1087 lu_object_put(env, &o->do_lu);
1089 if (handle->lgh_name != NULL)
1090 OBD_FREE(handle->lgh_name, strlen(name) + 1);
1098 * Implementation of the llog_operations::lop_exist
1100 * This function checks that llog exists on storage.
1102 * \param[in] handle llog handle of the current llog
1104 * \retval true if llog object exists and is not just destroyed
1105 * \retval false if llog doesn't exist or just destroyed
1107 static int llog_osd_exist(struct llog_handle *handle)
1109 LASSERT(handle->lgh_obj);
1110 return (dt_object_exists(handle->lgh_obj) &&
1111 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
1115 * Get dir for regular fid log object
1117 * Get directory for regular fid log object, and these regular fid log
1118 * object will be inserted under this directory, to satisfy the FS
1119 * consistency check, e2fsck etc.
1121 * \param [in] env execution environment
1122 * \param [in] dto llog object
1124 * \retval pointer to the directory if it is found.
1125 * \retval ERR_PTR(negative errno) if it fails.
1127 struct dt_object *llog_osd_get_regular_fid_dir(const struct lu_env *env,
1128 struct dt_object *dto)
1130 struct llog_thread_info *lgi = llog_info(env);
1131 struct seq_server_site *ss = dto->do_lu.lo_dev->ld_site->ld_seq_site;
1132 struct lu_seq_range *range = &lgi->lgi_range;
1133 struct lu_fid *dir_fid = &lgi->lgi_fid;
1134 struct dt_object *dir;
1138 fld_range_set_any(range);
1139 LASSERT(ss != NULL);
1140 rc = ss->ss_server_fld->lsf_seq_lookup(env, ss->ss_server_fld,
1141 fid_seq(lu_object_fid(&dto->do_lu)), range);
1143 RETURN(ERR_PTR(rc));
1145 lu_update_log_dir_fid(dir_fid, range->lsr_index);
1146 dir = dt_locate(env, lu2dt_dev(dto->do_lu.lo_dev), dir_fid);
1150 if (!dt_try_as_dir(env, dir)) {
1151 lu_object_put(env, &dir->do_lu);
1152 RETURN(ERR_PTR(-ENOTDIR));
1159 * Add llog object with regular FID to name entry
1161 * Add llog object with regular FID to name space, and each llog
1162 * object on each MDT will be /update_log_dir/[seq:oid:ver],
1163 * so to satisfy the namespace consistency check, e2fsck etc.
1165 * \param [in] env execution environment
1166 * \param [in] dto llog object
1167 * \param [in] th thandle
1168 * \param [in] declare if it is declare or execution
1170 * \retval 0 if insertion succeeds.
1171 * \retval negative errno if insertion fails.
1174 llog_osd_regular_fid_add_name_entry(const struct lu_env *env,
1175 struct dt_object *dto,
1176 struct thandle *th, bool declare)
1178 struct llog_thread_info *lgi = llog_info(env);
1179 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1180 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1181 struct dt_object *dir;
1182 char *name = lgi->lgi_name;
1186 if (!fid_is_norm(fid))
1189 dir = llog_osd_get_regular_fid_dir(env, dto);
1191 RETURN(PTR_ERR(dir));
1194 rec->rec_type = S_IFREG;
1195 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1196 dt_write_lock(env, dir, 0);
1198 rc = dt_declare_insert(env, dir, (struct dt_rec *)rec,
1199 (struct dt_key *)name, th);
1201 rc = dt_insert(env, dir, (struct dt_rec *)rec,
1202 (struct dt_key *)name, th, 1);
1204 dt_write_unlock(env, dir);
1206 lu_object_put(env, &dir->do_lu);
1212 * Implementation of the llog_operations::lop_declare_create
1214 * This function declares the llog create. It declares also name insert
1215 * into llog directory in case of named llog.
1217 * \param[in] env execution environment
1218 * \param[in] res llog handle of the current llog
1219 * \param[in] th current transaction handle
1221 * \retval 0 on successful create declaration
1222 * \retval negative value on error
1224 static int llog_osd_declare_create(const struct lu_env *env,
1225 struct llog_handle *res, struct thandle *th)
1227 struct llog_thread_info *lgi = llog_info(env);
1228 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1229 struct local_oid_storage *los;
1230 struct dt_object *o;
1235 LASSERT(res->lgh_obj);
1238 /* object can be created by another thread */
1240 if (dt_object_exists(o))
1243 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1244 struct llog_thread_info *lgi = llog_info(env);
1246 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE;
1247 lgi->lgi_attr.la_size = 0;
1248 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1249 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1251 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1257 rc = llog_osd_regular_fid_add_name_entry(env, o, th, true);
1261 los = res->private_data;
1264 rc = llog_osd_declare_new_object(env, los, o, th);
1268 /* do not declare header initialization here as it's declared
1269 * in llog_osd_declare_write_rec() which is always called */
1271 if (res->lgh_name) {
1272 struct dt_object *llog_dir;
1274 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1275 if (IS_ERR(llog_dir))
1276 RETURN(PTR_ERR(llog_dir));
1277 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1278 rec->rec_fid = &lgi->lgi_fid;
1279 rec->rec_type = S_IFREG;
1280 rc = dt_declare_insert(env, llog_dir,
1281 (struct dt_rec *)rec,
1282 (struct dt_key *)res->lgh_name, th);
1283 lu_object_put(env, &llog_dir->do_lu);
1285 CERROR("%s: can't declare named llog %s: rc = %d\n",
1286 o->do_lu.lo_dev->ld_obd->obd_name,
1293 * Implementation of the llog_operations::lop_create
1295 * This function creates the llog according with llog_handle::lgh_obj
1296 * and llog_handle::lgh_name.
1298 * \param[in] env execution environment
1299 * \param[in] res llog handle of the current llog
1300 * \param[in] th current transaction handle
1302 * \retval 0 on successful create
1303 * \retval negative value on error
1305 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
1308 struct llog_thread_info *lgi = llog_info(env);
1309 struct dt_insert_rec *rec = &lgi->lgi_dt_rec;
1310 struct local_oid_storage *los;
1311 struct dt_object *o;
1320 /* llog can be already created */
1321 if (dt_object_exists(o))
1324 if (res->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1325 struct llog_thread_info *lgi = llog_info(env);
1327 lgi->lgi_attr.la_valid = LA_MODE | LA_SIZE | LA_TYPE;
1328 lgi->lgi_attr.la_size = 0;
1329 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1330 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1332 dt_write_lock(env, o, 0);
1333 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1335 dt_write_unlock(env, o);
1339 rc = llog_osd_regular_fid_add_name_entry(env, o, th, false);
1344 los = res->private_data;
1347 dt_write_lock(env, o, 0);
1348 if (!dt_object_exists(o))
1349 rc = llog_osd_create_new_object(env, los, o, th);
1353 dt_write_unlock(env, o);
1357 if (res->lgh_name) {
1358 struct dt_object *llog_dir;
1360 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
1361 if (IS_ERR(llog_dir))
1362 RETURN(PTR_ERR(llog_dir));
1364 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
1365 rec->rec_fid = &lgi->lgi_fid;
1366 rec->rec_type = S_IFREG;
1367 dt_read_lock(env, llog_dir, 0);
1368 rc = dt_insert(env, llog_dir, (struct dt_rec *)rec,
1369 (struct dt_key *)res->lgh_name,
1371 dt_read_unlock(env, llog_dir);
1372 lu_object_put(env, &llog_dir->do_lu);
1374 CERROR("%s: can't create named llog %s: rc = %d\n",
1375 o->do_lu.lo_dev->ld_obd->obd_name,
1382 * Implementation of the llog_operations::lop_close
1384 * This function closes the llog. It just put llog object and referenced
1387 * \param[in] env execution environment
1388 * \param[in] handle llog handle of the current llog
1390 * \retval 0 on successful llog close
1391 * \retval negative value on error
1393 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
1395 struct local_oid_storage *los;
1400 LASSERT(handle->lgh_obj);
1402 if (handle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1403 /* Remove the object from the cache, otherwise it may
1404 * hold LOD being released during cleanup process */
1405 lu_object_put_nocache(env, &handle->lgh_obj->do_lu);
1406 LASSERT(handle->private_data == NULL);
1409 lu_object_put(env, &handle->lgh_obj->do_lu);
1411 los = handle->private_data;
1415 if (handle->lgh_name)
1416 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1422 * delete llog object name entry
1424 * Delete llog object (with regular FID) from name space (under
1427 * \param [in] env execution environment
1428 * \param [in] dto llog object
1429 * \param [in] th thandle
1430 * \param [in] declare if it is declare or execution
1432 * \retval 0 if deletion succeeds.
1433 * \retval negative errno if deletion fails.
1436 llog_osd_regular_fid_del_name_entry(const struct lu_env *env,
1437 struct dt_object *dto,
1438 struct thandle *th, bool declare)
1440 struct llog_thread_info *lgi = llog_info(env);
1441 const struct lu_fid *fid = lu_object_fid(&dto->do_lu);
1442 struct dt_object *dir;
1443 char *name = lgi->lgi_name;
1447 if (!fid_is_norm(fid))
1450 dir = llog_osd_get_regular_fid_dir(env, dto);
1452 RETURN(PTR_ERR(dir));
1454 snprintf(name, sizeof(lgi->lgi_name), DFID, PFID(fid));
1455 dt_write_lock(env, dir, 0);
1457 rc = dt_declare_delete(env, dir, (struct dt_key *)name,
1460 rc = dt_delete(env, dir, (struct dt_key *)name, th);
1462 dt_write_unlock(env, dir);
1464 lu_object_put(env, &dir->do_lu);
1470 * Implementation of the llog_operations::lop_destroy
1472 * This function destroys the llog and deletes also entry in the
1473 * llog directory in case of named llog. Llog should be opened prior that.
1474 * Destroy method is not part of external transaction and does everything
1477 * \param[in] env execution environment
1478 * \param[in] loghandle llog handle of the current llog
1480 * \retval 0 on successful destroy
1481 * \retval negative value on error
1483 static int llog_osd_destroy(const struct lu_env *env,
1484 struct llog_handle *loghandle)
1486 struct llog_ctxt *ctxt;
1487 struct dt_object *o, *llog_dir = NULL;
1488 struct dt_device *d;
1495 ctxt = loghandle->lgh_ctxt;
1498 o = loghandle->lgh_obj;
1501 d = lu2dt_dev(o->do_lu.lo_dev);
1503 LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1505 th = dt_trans_create(env, d);
1507 RETURN(PTR_ERR(th));
1509 if (loghandle->lgh_name) {
1510 llog_dir = llog_osd_dir_get(env, ctxt);
1511 if (IS_ERR(llog_dir))
1512 GOTO(out_trans, rc = PTR_ERR(llog_dir));
1514 name = loghandle->lgh_name;
1515 rc = dt_declare_delete(env, llog_dir,
1516 (struct dt_key *)name, th);
1518 GOTO(out_trans, rc);
1521 rc = dt_declare_ref_del(env, o, th);
1523 GOTO(out_trans, rc);
1525 rc = dt_declare_destroy(env, o, th);
1527 GOTO(out_trans, rc);
1529 if (loghandle->lgh_ctxt->loc_flags & LLOG_CTXT_FLAG_NORMAL_FID) {
1530 rc = llog_osd_regular_fid_del_name_entry(env, o, th, true);
1532 GOTO(out_trans, rc);
1535 rc = dt_trans_start_local(env, d, th);
1537 GOTO(out_trans, rc);
1539 th->th_wait_submit = 1;
1541 dt_write_lock(env, o, 0);
1542 if (dt_object_exists(o)) {
1544 dt_read_lock(env, llog_dir, 0);
1545 rc = dt_delete(env, llog_dir,
1546 (struct dt_key *) name,
1548 dt_read_unlock(env, llog_dir);
1550 CERROR("%s: can't remove llog %s: rc = %d\n",
1551 o->do_lu.lo_dev->ld_obd->obd_name,
1553 GOTO(out_unlock, rc);
1556 dt_ref_del(env, o, th);
1557 rc = dt_destroy(env, o, th);
1559 GOTO(out_unlock, rc);
1561 if (loghandle->lgh_ctxt->loc_flags &
1562 LLOG_CTXT_FLAG_NORMAL_FID) {
1563 rc = llog_osd_regular_fid_del_name_entry(env, o, th,
1566 GOTO(out_unlock, rc);
1570 dt_write_unlock(env, o);
1572 dt_trans_stop(env, d, th);
1573 if (!(IS_ERR_OR_NULL(llog_dir)))
1574 lu_object_put(env, &llog_dir->do_lu);
1579 * Implementation of the llog_operations::lop_setup
1581 * This function setup the llog on local storage.
1583 * \param[in] env execution environment
1584 * \param[in] obd obd device the llog belongs to
1585 * \param[in] olg the llog group, it is always zero group now.
1586 * \param[in] ctxt_idx the llog index, it defines the purpose of this llog.
1587 * Every new llog type have to use own index.
1588 * \param[in] disk_obd the storage obd, where llog is stored.
1590 * \retval 0 on successful llog setup
1591 * \retval negative value on error
1593 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1594 struct obd_llog_group *olg, int ctxt_idx,
1595 struct obd_device *disk_obd)
1597 struct llog_thread_info *lgi = llog_info(env);
1598 struct llog_ctxt *ctxt;
1603 LASSERT(olg->olg_ctxts[ctxt_idx]);
1605 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1608 if (disk_obd == NULL)
1611 /* initialize data allowing to generate new fids,
1612 * literally we need a sequece */
1613 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1614 lgi->lgi_fid.f_oid = 1;
1615 lgi->lgi_fid.f_ver = 0;
1616 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1618 &ctxt->loc_los_nameless);
1622 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1623 lgi->lgi_fid.f_oid = 1;
1624 lgi->lgi_fid.f_ver = 0;
1625 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1627 &ctxt->loc_los_named);
1629 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1630 ctxt->loc_los_nameless = NULL;
1636 llog_ctxt_put(ctxt);
1641 * Implementation of the llog_operations::lop_cleanup
1643 * This function cleanups the llog on local storage.
1645 * \param[in] env execution environment
1646 * \param[in] ctxt the llog context
1650 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1652 if (ctxt->loc_los_nameless != NULL) {
1653 local_oid_storage_fini(env, ctxt->loc_los_nameless);
1654 ctxt->loc_los_nameless = NULL;
1657 if (ctxt->loc_los_named != NULL) {
1658 local_oid_storage_fini(env, ctxt->loc_los_named);
1659 ctxt->loc_los_named = NULL;
1665 struct llog_operations llog_osd_ops = {
1666 .lop_next_block = llog_osd_next_block,
1667 .lop_prev_block = llog_osd_prev_block,
1668 .lop_read_header = llog_osd_read_header,
1669 .lop_destroy = llog_osd_destroy,
1670 .lop_setup = llog_osd_setup,
1671 .lop_cleanup = llog_osd_cleanup,
1672 .lop_open = llog_osd_open,
1673 .lop_exist = llog_osd_exist,
1674 .lop_declare_create = llog_osd_declare_create,
1675 .lop_create = llog_osd_create,
1676 .lop_declare_write_rec = llog_osd_declare_write_rec,
1677 .lop_write_rec = llog_osd_write_rec,
1678 .lop_close = llog_osd_close,
1680 EXPORT_SYMBOL(llog_osd_ops);
1682 struct llog_operations llog_common_cat_ops = {
1683 .lop_next_block = llog_osd_next_block,
1684 .lop_prev_block = llog_osd_prev_block,
1685 .lop_read_header = llog_osd_read_header,
1686 .lop_destroy = llog_osd_destroy,
1687 .lop_setup = llog_osd_setup,
1688 .lop_cleanup = llog_osd_cleanup,
1689 .lop_open = llog_osd_open,
1690 .lop_exist = llog_osd_exist,
1691 .lop_declare_create = llog_osd_declare_create,
1692 .lop_create = llog_osd_create,
1693 .lop_declare_write_rec = llog_osd_declare_write_rec,
1694 .lop_write_rec = llog_osd_write_rec,
1695 .lop_close = llog_osd_close,
1696 .lop_add = llog_cat_add_rec,
1697 .lop_declare_add = llog_cat_declare_add_rec,
1699 EXPORT_SYMBOL(llog_common_cat_ops);
1702 * Read the special file which contains the list of llog catalogs IDs
1704 * This function reads the CATALOGS file which contains the array of llog
1705 * catalogs IDs. The main purpose of this file is to store OSP llogs indexed
1706 * by OST/MDT number.
1708 * \param[in] env execution environment
1709 * \param[in] d corresponding storage device
1710 * \param[in] idx position to start from, usually OST/MDT index
1711 * \param[in] count how many catalog IDs to read
1712 * \param[out] idarray the buffer for the data. If it is NULL then
1713 * function returns just number of catalog IDs
1715 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1717 * \retval 0 on successful read of catalog IDs
1718 * \retval negative value on error
1719 * \retval positive value which is number of records in
1720 * the file if \a idarray is NULL
1722 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1723 int idx, int count, struct llog_catid *idarray,
1724 const struct lu_fid *fid)
1726 struct llog_thread_info *lgi = llog_info(env);
1727 struct dt_object *o = NULL;
1735 size = sizeof(*idarray) * count;
1736 lgi->lgi_off = idx * sizeof(*idarray);
1738 lgi->lgi_fid = *fid;
1739 o = dt_locate(env, d, &lgi->lgi_fid);
1743 if (!dt_object_exists(o)) {
1744 th = dt_trans_create(env, d);
1746 GOTO(out, rc = PTR_ERR(th));
1748 lgi->lgi_attr.la_valid = LA_MODE;
1749 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1750 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1752 th->th_wait_submit = 1;
1753 /* Make the llog object creation synchronization, so
1754 * it will be reliable to the reference, especially
1755 * for remote reference */
1758 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1761 GOTO(out_trans, rc);
1763 rc = dt_trans_start_local(env, d, th);
1765 GOTO(out_trans, rc);
1767 dt_write_lock(env, o, 0);
1768 if (!dt_object_exists(o))
1769 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1771 dt_write_unlock(env, o);
1773 dt_trans_stop(env, d, th);
1778 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1782 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1783 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1784 o->do_lu.lo_dev->ld_obd->obd_name,
1785 lgi->lgi_attr.la_mode);
1786 GOTO(out, rc = -ENOENT);
1789 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1790 (int)lgi->lgi_attr.la_size, size);
1792 /* return just number of llogs */
1793 if (idarray == NULL) {
1794 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1798 /* read for new ost index or for empty file */
1799 memset(idarray, 0, size);
1800 if (lgi->lgi_attr.la_size <= lgi->lgi_off)
1802 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1803 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1805 lgi->lgi_buf.lb_buf = idarray;
1806 lgi->lgi_buf.lb_len = size;
1807 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1808 /* -EFAULT means the llog is a sparse file. This is not an error
1809 * after arbitrary OST index is supported. */
1810 if (rc < 0 && rc != -EFAULT) {
1811 CERROR("%s: error reading CATALOGS: rc = %d\n",
1812 o->do_lu.lo_dev->ld_obd->obd_name, rc);
1818 lu_object_put(env, &o->do_lu);
1821 EXPORT_SYMBOL(llog_osd_get_cat_list);
1824 * Write the special file which contains the list of llog catalogs IDs
1826 * This function writes the CATALOG file which contains the array of llog
1827 * catalogs IDs. It is used mostly to store OSP llogs indexed by OST/MDT
1830 * \param[in] env execution environment
1831 * \param[in] d corresponding storage device
1832 * \param[in] idx position to start from, usually OST/MDT index
1833 * \param[in] count how many catalog IDs to write
1834 * \param[out] idarray the buffer with the data to write.
1835 * \param[in] fid LLOG_CATALOGS_OID for CATALOG object
1837 * \retval 0 on successful write of catalog IDs
1838 * \retval negative value on error
1840 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1841 int idx, int count, struct llog_catid *idarray,
1842 const struct lu_fid *fid)
1844 struct llog_thread_info *lgi = llog_info(env);
1845 struct dt_object *o = NULL;
1854 size = sizeof(*idarray) * count;
1855 lgi->lgi_off = idx * sizeof(*idarray);
1856 lgi->lgi_fid = *fid;
1858 o = dt_locate(env, d, &lgi->lgi_fid);
1862 if (!dt_object_exists(o))
1863 GOTO(out, rc = -ENOENT);
1865 rc = dt_attr_get(env, o, &lgi->lgi_attr);
1869 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1870 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1871 o->do_lu.lo_dev->ld_obd->obd_name,
1872 lgi->lgi_attr.la_mode);
1873 GOTO(out, rc = -ENOENT);
1876 th = dt_trans_create(env, d);
1878 GOTO(out, rc = PTR_ERR(th));
1880 lgi->lgi_buf.lb_len = size;
1881 lgi->lgi_buf.lb_buf = idarray;
1882 rc = dt_declare_record_write(env, o, &lgi->lgi_buf, lgi->lgi_off, th);
1886 rc = dt_trans_start_local(env, d, th);
1888 GOTO(out_trans, rc);
1890 th->th_wait_submit = 1;
1892 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1894 CDEBUG(D_INODE, "can't write CATALOGS at index %d: rc = %d\n",
1897 dt_trans_stop(env, d, th);
1899 lu_object_put(env, &o->do_lu);
1902 EXPORT_SYMBOL(llog_osd_put_cat_list);