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.sun.com/software/products/lustre/docs/GPLv2.pdf
20 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21 * CA 95054 USA or visit www.sun.com if you need additional information or
27 * Copyright (c) 2009, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/obdclass/llog_osd.c - low level llog routines on top of OSD API
38 * Author: Alexey Zhuravlev <alexey.zhuravlev@intel.com>
39 * Author: Mikhail Pershin <mike.pershin@intel.com>
42 #define DEBUG_SUBSYSTEM S_LOG
49 #include <obd_class.h>
50 #include <lustre_fid.h>
51 #include <dt_object.h>
53 #include "llog_internal.h"
54 #include "local_storage.h"
57 * - multi-chunks or big-declaration approach
58 * - use unique sequence instead of llog sb tracking unique ids
59 * - re-use existing environment
60 * - named llog support (can be used for testing only at the present)
61 * - llog_origin_connect() work with OSD API
64 static int llog_osd_declare_new_object(const struct lu_env *env,
65 struct local_oid_storage *los,
69 struct llog_thread_info *lgi = llog_info(env);
71 lgi->lgi_attr.la_valid = LA_MODE;
72 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
73 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
75 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
79 static int llog_osd_create_new_object(const struct lu_env *env,
80 struct local_oid_storage *los,
84 struct llog_thread_info *lgi = llog_info(env);
86 lgi->lgi_attr.la_valid = LA_MODE;
87 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
88 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
90 return local_object_create(env, los, o, &lgi->lgi_attr,
94 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
95 loff_t *off, int len, int index, struct thandle *th)
97 struct llog_thread_info *lgi = llog_info(env);
104 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
106 lgi->lgi_tail.lrt_len = lgi->lgi_lrh.lrh_len = len;
107 lgi->lgi_tail.lrt_index = lgi->lgi_lrh.lrh_index = index;
108 lgi->lgi_lrh.lrh_type = LLOG_PAD_MAGIC;
110 lgi->lgi_buf.lb_buf = &lgi->lgi_lrh;
111 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_lrh);
112 dt_write_lock(env, o, 0);
113 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
115 CERROR("%s: error writing padding record: rc = %d\n",
116 o->do_lu.lo_dev->ld_obd->obd_name, rc);
120 lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
121 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
122 *off += len - sizeof(lgi->lgi_lrh) - sizeof(lgi->lgi_tail);
123 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
125 CERROR("%s: error writing padding record: rc = %d\n",
126 o->do_lu.lo_dev->ld_obd->obd_name, rc);
128 dt_write_unlock(env, o);
132 static int llog_osd_write_blob(const struct lu_env *env, struct dt_object *o,
133 struct llog_rec_hdr *rec, void *buf,
134 loff_t *off, struct thandle *th)
136 struct llog_thread_info *lgi = llog_info(env);
137 int buflen = rec->lrh_len;
146 CWARN("0-length record\n");
148 CDEBUG(D_OTHER, "write blob with type %x, buf %p/%u at off %llu\n",
149 rec->lrh_type, buf, buflen, *off);
151 lgi->lgi_attr.la_valid = LA_SIZE;
152 lgi->lgi_attr.la_size = *off;
155 lgi->lgi_buf.lb_len = buflen;
156 lgi->lgi_buf.lb_buf = rec;
157 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
159 CERROR("%s: error writing log record: rc = %d\n",
160 o->do_lu.lo_dev->ld_obd->obd_name, rc);
165 /* protect the following 3 writes from concurrent read */
166 dt_write_lock(env, o, 0);
167 rec->lrh_len = sizeof(*rec) + buflen + sizeof(lgi->lgi_tail);
168 lgi->lgi_buf.lb_len = sizeof(*rec);
169 lgi->lgi_buf.lb_buf = rec;
170 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
172 CERROR("%s: error writing log hdr: rc = %d\n",
173 o->do_lu.lo_dev->ld_obd->obd_name, rc);
174 GOTO(out_unlock, rc);
177 lgi->lgi_buf.lb_len = buflen;
178 lgi->lgi_buf.lb_buf = buf;
179 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
181 CERROR("%s: error writing log buffer: rc = %d\n",
182 o->do_lu.lo_dev->ld_obd->obd_name, rc);
183 GOTO(out_unlock, rc);
186 lgi->lgi_tail.lrt_len = rec->lrh_len;
187 lgi->lgi_tail.lrt_index = rec->lrh_index;
188 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
189 lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
190 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
192 CERROR("%s: error writing log tail: rc = %d\n",
193 o->do_lu.lo_dev->ld_obd->obd_name, rc);
196 dt_write_unlock(env, o);
199 /* cleanup the content written above */
201 dt_punch(env, o, lgi->lgi_attr.la_size, OBD_OBJECT_EOF, th,
203 dt_attr_set(env, o, &lgi->lgi_attr, th, BYPASS_CAPA);
209 static int llog_osd_read_header(const struct lu_env *env,
210 struct llog_handle *handle)
212 struct llog_rec_hdr *llh_hdr;
214 struct llog_thread_info *lgi;
219 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
224 lgi = llog_info(env);
226 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
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");
238 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
239 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
241 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
243 CERROR("%s: error reading log header from "DFID": rc = %d\n",
244 o->do_lu.lo_dev->ld_obd->obd_name,
245 PFID(lu_object_fid(&o->do_lu)), rc);
249 llh_hdr = &handle->lgh_hdr->llh_hdr;
250 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
251 lustre_swab_llog_hdr(handle->lgh_hdr);
253 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
254 CERROR("%s: bad log %s "DFID" header magic: %#x "
255 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
256 handle->lgh_name ? handle->lgh_name : "",
257 PFID(lu_object_fid(&o->do_lu)),
258 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
260 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
261 CERROR("%s: incorrectly sized log %s "DFID" header: "
262 "%#x (expected %#x)\n"
263 "you may need to re-run lconf --write_conf.\n",
264 o->do_lu.lo_dev->ld_obd->obd_name,
265 handle->lgh_name ? handle->lgh_name : "",
266 PFID(lu_object_fid(&o->do_lu)),
267 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
271 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
276 static int llog_osd_declare_write_rec(const struct lu_env *env,
277 struct llog_handle *loghandle,
278 struct llog_rec_hdr *rec,
279 int idx, struct thandle *th)
281 struct llog_thread_info *lgi = llog_info(env);
291 o = loghandle->lgh_obj;
294 /* each time we update header */
295 rc = dt_declare_record_write(env, o, sizeof(struct llog_log_hdr), 0,
297 if (rc || idx == 0) /* if error or just header */
300 if (dt_object_exists(o)) {
301 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
302 lgi->lgi_off = lgi->lgi_attr.la_size;
303 LASSERT(ergo(rc == 0, lgi->lgi_attr.la_valid & LA_SIZE));
307 rc = dt_declare_punch(env, o, lgi->lgi_off, OBD_OBJECT_EOF, th);
314 /* XXX: implement declared window or multi-chunks approach */
315 rc = dt_declare_record_write(env, o, 32 * 1024, lgi->lgi_off, th);
320 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
321 /* appends if idx == -1, otherwise overwrites record idx. */
322 static int llog_osd_write_rec(const struct lu_env *env,
323 struct llog_handle *loghandle,
324 struct llog_rec_hdr *rec,
325 struct llog_cookie *reccookie, int cookiecount,
326 void *buf, int idx, struct thandle *th)
328 struct llog_thread_info *lgi = llog_info(env);
329 struct llog_log_hdr *llh;
330 int reclen = rec->lrh_len;
331 int index, rc, old_tail_idx;
332 struct llog_rec_tail *lrt;
339 llh = loghandle->lgh_hdr;
341 o = loghandle->lgh_obj;
345 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
346 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
348 /* record length should not bigger than LLOG_CHUNK_SIZE */
350 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
351 sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
353 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
357 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
362 /* write_blob adds header and tail to lrh_len. */
363 reclen = sizeof(*rec) + rec->lrh_len +
364 sizeof(struct llog_rec_tail);
367 /* no header: only allowed to insert record 1 */
368 if (idx != 1 && lgi->lgi_attr.la_size == 0)
371 if (idx && llh->llh_size && llh->llh_size != rec->lrh_len)
374 if (!ext2_test_bit(idx, llh->llh_bitmap))
375 CERROR("%s: modify unset record %u\n",
376 o->do_lu.lo_dev->ld_obd->obd_name, idx);
377 if (idx != rec->lrh_index)
378 CERROR("%s: index mismatch %d %u\n",
379 o->do_lu.lo_dev->ld_obd->obd_name, idx,
383 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
385 /* we are done if we only write the header or on error */
390 /* We assume that caller has set lgh_cur_* */
391 lgi->lgi_off = loghandle->lgh_cur_offset;
393 "modify record "DOSTID": idx:%d/%u/%d, len:%u "
395 POSTID(&loghandle->lgh_id.lgl_oi), idx,
397 loghandle->lgh_cur_idx, rec->lrh_len,
398 (long long)(lgi->lgi_off - sizeof(*llh)));
399 if (rec->lrh_index != loghandle->lgh_cur_idx) {
400 CERROR("%s: modify idx mismatch %u/%d\n",
401 o->do_lu.lo_dev->ld_obd->obd_name, idx,
402 loghandle->lgh_cur_idx);
406 /* Assumes constant lrh_len */
407 lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
410 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
411 if (rc == 0 && reccookie) {
412 reccookie->lgc_lgl = loghandle->lgh_id;
413 reccookie->lgc_index = idx;
419 /* Make sure that records don't cross a chunk boundary, so we can
420 * process them page-at-a-time if needed. If it will cross a chunk
421 * boundary, write in a fake (but referenced) entry to pad the chunk.
423 * We know that llog_current_log() will return a loghandle that is
424 * big enough to hold reclen, so all we care about is padding here.
426 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
427 lgi->lgi_off = lgi->lgi_attr.la_size;
428 left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
429 /* NOTE: padding is a record, but no bit is set */
430 if (left != 0 && left != reclen &&
431 left < (reclen + LLOG_MIN_REC_SIZE)) {
432 index = loghandle->lgh_last_idx + 1;
433 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
436 loghandle->lgh_last_idx++; /*for pad rec*/
438 /* if it's the last idx in log file, then return -ENOSPC */
439 if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
442 loghandle->lgh_last_idx++;
443 index = loghandle->lgh_last_idx;
444 LASSERT(index < LLOG_BITMAP_SIZE(llh));
445 rec->lrh_index = index;
447 lrt = (struct llog_rec_tail *)((char *)rec + rec->lrh_len -
449 lrt->lrt_len = rec->lrh_len;
450 lrt->lrt_index = rec->lrh_index;
452 /* The caller should make sure only 1 process access the lgh_last_idx,
453 * Otherwise it might hit the assert.*/
454 LASSERT(index < LLOG_BITMAP_SIZE(llh));
455 spin_lock(&loghandle->lgh_hdr_lock);
456 if (ext2_set_bit(index, llh->llh_bitmap)) {
457 CERROR("%s: index %u already set in log bitmap\n",
458 o->do_lu.lo_dev->ld_obd->obd_name, index);
459 spin_unlock(&loghandle->lgh_hdr_lock);
460 LBUG(); /* should never happen */
463 spin_unlock(&loghandle->lgh_hdr_lock);
464 old_tail_idx = llh->llh_tail.lrt_index;
465 llh->llh_tail.lrt_index = index;
468 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL, &lgi->lgi_off,
473 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
477 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
478 lgi->lgi_off = lgi->lgi_attr.la_size;
480 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
483 /* cleanup llog for error case */
485 spin_lock(&loghandle->lgh_hdr_lock);
486 ext2_clear_bit(index, llh->llh_bitmap);
488 spin_unlock(&loghandle->lgh_hdr_lock);
490 /* restore the header */
491 loghandle->lgh_last_idx--;
492 llh->llh_tail.lrt_index = old_tail_idx;
494 llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
498 CDEBUG(D_RPCTRACE, "added record "DOSTID": idx: %u, %u\n",
499 POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
500 if (rc == 0 && reccookie) {
501 reccookie->lgc_lgl = loghandle->lgh_id;
502 reccookie->lgc_index = index;
503 if ((rec->lrh_type == MDS_UNLINK_REC) ||
504 (rec->lrh_type == MDS_SETATTR64_REC))
505 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
506 else if (rec->lrh_type == OST_SZ_REC)
507 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
509 reccookie->lgc_subsys = -1;
515 /* We can skip reading at least as many log blocks as the number of
516 * minimum sized log records we are skipping. If it turns out
517 * that we are not far enough along the log (because the
518 * actual records are larger than minimum size) we just skip
521 static void llog_skip_over(__u64 *off, int curr, int goal)
525 *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
526 ~(LLOG_CHUNK_SIZE - 1);
530 * - cur_offset to the furthest point read in the log file
531 * - cur_idx to the log index preceeding cur_offset
532 * returns -EIO/-EINVAL on error
534 static int llog_osd_next_block(const struct lu_env *env,
535 struct llog_handle *loghandle, int *cur_idx,
536 int next_idx, __u64 *cur_offset, void *buf,
539 struct llog_thread_info *lgi = llog_info(env);
541 struct dt_device *dt;
549 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
552 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
553 next_idx, *cur_idx, *cur_offset);
556 LASSERT(loghandle->lgh_ctxt);
558 o = loghandle->lgh_obj;
560 LASSERT(dt_object_exists(o));
561 dt = lu2dt_dev(o->do_lu.lo_dev);
564 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
568 while (*cur_offset < lgi->lgi_attr.la_size) {
569 struct llog_rec_hdr *rec, *last_rec;
570 struct llog_rec_tail *tail;
572 llog_skip_over(cur_offset, *cur_idx, next_idx);
574 /* read up to next LLOG_CHUNK_SIZE block */
575 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
576 (*cur_offset & (LLOG_CHUNK_SIZE - 1));
577 lgi->lgi_buf.lb_buf = buf;
579 /* Note: read lock is not needed around la_size get above at
580 * the time of dt_attr_get(). There are only two cases that
581 * matter. Either la_size == cur_offset, in which case the
582 * entire read is skipped, or la_size > cur_offset and the loop
583 * is entered and this thread is blocked at dt_read_lock()
584 * until the write is completed. When the write completes, then
585 * the dt_read() will be done with the full length, and will
588 dt_read_lock(env, o, 0);
589 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
590 dt_read_unlock(env, o);
592 CERROR("%s: can't read llog block from log "DFID
593 " offset "LPU64": rc = %d\n",
594 o->do_lu.lo_dev->ld_obd->obd_name,
595 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
601 /* signal the end of the valid buffer to
603 memset(buf + rc, 0, len - rc);
606 if (rc == 0) /* end of file, nothing to do */
609 if (rc < sizeof(*tail)) {
610 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
612 o->do_lu.lo_dev->ld_obd->obd_name,
613 POSTID(&loghandle->lgh_id.lgl_oi),
614 loghandle->lgh_id.lgl_ogen, *cur_offset);
615 GOTO(out, rc = -EINVAL);
619 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
620 lustre_swab_llog_rec(rec);
622 tail = (struct llog_rec_tail *)((char *)buf + rc -
623 sizeof(struct llog_rec_tail));
624 /* get the last record in block */
625 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
626 le32_to_cpu(tail->lrt_len));
628 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
629 lustre_swab_llog_rec(last_rec);
630 LASSERT(last_rec->lrh_index == tail->lrt_index);
632 *cur_idx = tail->lrt_index;
634 /* this shouldn't happen */
635 if (tail->lrt_index == 0) {
636 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
638 o->do_lu.lo_dev->ld_obd->obd_name,
639 POSTID(&loghandle->lgh_id.lgl_oi),
640 loghandle->lgh_id.lgl_ogen, *cur_offset);
641 GOTO(out, rc = -EINVAL);
643 if (tail->lrt_index < next_idx)
646 /* sanity check that the start of the new buffer is no farther
647 * than the record that we wanted. This shouldn't happen. */
648 if (rec->lrh_index > next_idx) {
649 CERROR("%s: missed desired record? %u > %u\n",
650 o->do_lu.lo_dev->ld_obd->obd_name,
651 rec->lrh_index, next_idx);
652 GOTO(out, rc = -ENOENT);
656 GOTO(out, rc = -EIO);
661 static int llog_osd_prev_block(const struct lu_env *env,
662 struct llog_handle *loghandle,
663 int prev_idx, void *buf, int len)
665 struct llog_thread_info *lgi = llog_info(env);
667 struct dt_device *dt;
673 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
676 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
679 LASSERT(loghandle->lgh_ctxt);
681 o = loghandle->lgh_obj;
683 LASSERT(dt_object_exists(o));
684 dt = lu2dt_dev(o->do_lu.lo_dev);
687 cur_offset = LLOG_CHUNK_SIZE;
688 llog_skip_over(&cur_offset, 0, prev_idx);
690 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
694 while (cur_offset < lgi->lgi_attr.la_size) {
695 struct llog_rec_hdr *rec, *last_rec;
696 struct llog_rec_tail *tail;
698 lgi->lgi_buf.lb_len = len;
699 lgi->lgi_buf.lb_buf = buf;
700 /* It is OK to have locking around dt_read() only, see
701 * comment in llog_osd_next_block for details
703 dt_read_lock(env, o, 0);
704 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
705 dt_read_unlock(env, o);
707 CERROR("%s: can't read llog block from log "DFID
708 " offset "LPU64": rc = %d\n",
709 o->do_lu.lo_dev->ld_obd->obd_name,
710 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
714 if (rc == 0) /* end of file, nothing to do */
717 if (rc < sizeof(*tail)) {
718 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
720 o->do_lu.lo_dev->ld_obd->obd_name,
721 POSTID(&loghandle->lgh_id.lgl_oi),
722 loghandle->lgh_id.lgl_ogen, cur_offset);
723 GOTO(out, rc = -EINVAL);
727 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
728 lustre_swab_llog_rec(rec);
730 tail = (struct llog_rec_tail *)((char *)buf + rc -
731 sizeof(struct llog_rec_tail));
732 /* get the last record in block */
733 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
734 le32_to_cpu(tail->lrt_len));
736 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
737 lustre_swab_llog_rec(last_rec);
738 LASSERT(last_rec->lrh_index == tail->lrt_index);
740 /* this shouldn't happen */
741 if (tail->lrt_index == 0) {
742 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
744 o->do_lu.lo_dev->ld_obd->obd_name,
745 POSTID(&loghandle->lgh_id.lgl_oi),
746 loghandle->lgh_id.lgl_ogen, cur_offset);
747 GOTO(out, rc = -EINVAL);
749 if (tail->lrt_index < prev_idx)
752 /* sanity check that the start of the new buffer is no farther
753 * than the record that we wanted. This shouldn't happen. */
754 if (rec->lrh_index > prev_idx) {
755 CERROR("%s: missed desired record? %u > %u\n",
756 o->do_lu.lo_dev->ld_obd->obd_name,
757 rec->lrh_index, prev_idx);
758 GOTO(out, rc = -ENOENT);
762 GOTO(out, rc = -EIO);
767 struct dt_object *llog_osd_dir_get(const struct lu_env *env,
768 struct llog_ctxt *ctxt)
770 struct dt_device *dt;
771 struct dt_thread_info *dti = dt_info(env);
772 struct dt_object *dir;
775 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
776 if (ctxt->loc_dir == NULL) {
777 rc = dt_root_get(env, dt, &dti->dti_fid);
780 dir = dt_locate(env, dt, &dti->dti_fid);
782 lu_object_get(&ctxt->loc_dir->do_lu);
789 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
790 struct llog_logid *logid, char *name,
791 enum llog_open_param open_param)
793 struct llog_thread_info *lgi = llog_info(env);
794 struct llog_ctxt *ctxt = handle->lgh_ctxt;
796 struct dt_device *dt;
797 struct ls_device *ls;
798 struct local_oid_storage *los;
805 LASSERT(ctxt->loc_exp);
806 LASSERT(ctxt->loc_exp->exp_obd);
807 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
810 ls = ls_device_get(dt);
814 mutex_lock(&ls->ls_los_mutex);
815 los = dt_los_find(ls, FID_SEQ_LLOG);
816 mutex_unlock(&ls->ls_los_mutex);
818 ls_device_put(env, ls);
823 logid_to_fid(logid, &lgi->lgi_fid);
825 struct dt_object *llog_dir;
827 llog_dir = llog_osd_dir_get(env, ctxt);
828 if (IS_ERR(llog_dir))
829 GOTO(out, rc = PTR_ERR(llog_dir));
830 dt_read_lock(env, llog_dir, 0);
831 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
832 dt_read_unlock(env, llog_dir);
833 lu_object_put(env, &llog_dir->do_lu);
834 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
835 /* generate fid for new llog */
836 rc = local_object_fid_generate(env, los,
841 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
842 if (handle->lgh_name)
843 strcpy(handle->lgh_name, name);
845 GOTO(out, rc = -ENOMEM);
847 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
848 /* generate fid for new llog */
849 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
854 o = ls_locate(env, ls, &lgi->lgi_fid);
856 GOTO(out_name, rc = PTR_ERR(o));
858 /* No new llog is expected but doesn't exist */
859 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
860 GOTO(out_put, rc = -ENOENT);
862 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
864 handle->private_data = los;
865 LASSERT(handle->lgh_ctxt);
870 lu_object_put(env, &o->do_lu);
872 if (handle->lgh_name != NULL)
873 OBD_FREE(handle->lgh_name, strlen(name) + 1);
879 static int llog_osd_exist(struct llog_handle *handle)
881 LASSERT(handle->lgh_obj);
882 return (dt_object_exists(handle->lgh_obj) &&
883 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
886 static int llog_osd_declare_create(const struct lu_env *env,
887 struct llog_handle *res, struct thandle *th)
889 struct llog_thread_info *lgi = llog_info(env);
890 struct local_oid_storage *los;
896 LASSERT(res->lgh_obj);
899 /* object can be created by another thread */
901 if (dt_object_exists(o))
904 los = res->private_data;
907 rc = llog_osd_declare_new_object(env, los, o, th);
911 rc = dt_declare_record_write(env, o, LLOG_CHUNK_SIZE, 0, th);
916 struct dt_object *llog_dir;
918 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
919 if (IS_ERR(llog_dir))
920 RETURN(PTR_ERR(llog_dir));
921 dt_declare_ref_add(env, o, th);
922 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
923 rc = dt_declare_insert(env, llog_dir,
924 (struct dt_rec *)&lgi->lgi_fid,
925 (struct dt_key *)res->lgh_name, th);
926 lu_object_put(env, &llog_dir->do_lu);
928 CERROR("%s: can't declare named llog %s: rc = %d\n",
929 o->do_lu.lo_dev->ld_obd->obd_name,
935 /* This is a callback from the llog_* functions.
936 * Assumes caller has already pushed us into the kernel context. */
937 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
940 struct llog_thread_info *lgi = llog_info(env);
941 struct local_oid_storage *los;
951 /* llog can be already created */
952 if (dt_object_exists(o))
955 los = res->private_data;
958 dt_write_lock(env, o, 0);
959 if (!dt_object_exists(o))
960 rc = llog_osd_create_new_object(env, los, o, th);
964 dt_ref_add(env, o, th);
965 dt_write_unlock(env, o);
970 struct dt_object *llog_dir;
972 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
973 if (IS_ERR(llog_dir))
974 RETURN(PTR_ERR(llog_dir));
976 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
977 dt_read_lock(env, llog_dir, 0);
978 rc = dt_insert(env, llog_dir,
979 (struct dt_rec *)&lgi->lgi_fid,
980 (struct dt_key *)res->lgh_name,
982 dt_read_unlock(env, llog_dir);
983 lu_object_put(env, &llog_dir->do_lu);
985 CERROR("%s: can't create named llog %s: rc = %d\n",
986 o->do_lu.lo_dev->ld_obd->obd_name,
992 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
994 struct local_oid_storage *los;
999 LASSERT(handle->lgh_obj);
1001 lu_object_put(env, &handle->lgh_obj->do_lu);
1003 los = handle->private_data;
1007 if (handle->lgh_name)
1008 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
1013 static int llog_osd_destroy(const struct lu_env *env,
1014 struct llog_handle *loghandle)
1016 struct llog_thread_info *lgi = llog_info(env);
1017 struct llog_ctxt *ctxt;
1018 struct dt_object *o, *llog_dir = NULL;
1019 struct dt_device *d;
1026 ctxt = loghandle->lgh_ctxt;
1029 o = loghandle->lgh_obj;
1032 d = lu2dt_dev(o->do_lu.lo_dev);
1034 LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1036 th = dt_trans_create(env, d);
1038 RETURN(PTR_ERR(th));
1040 if (loghandle->lgh_name) {
1041 llog_dir = llog_osd_dir_get(env, ctxt);
1042 if (IS_ERR(llog_dir))
1043 GOTO(out_trans, rc = PTR_ERR(llog_dir));
1045 dt_declare_ref_del(env, o, th);
1046 name = loghandle->lgh_name;
1047 rc = dt_declare_delete(env, llog_dir,
1048 (struct dt_key *)name, th);
1050 GOTO(out_trans, rc);
1053 dt_declare_ref_del(env, o, th);
1055 rc = dt_declare_destroy(env, o, th);
1057 GOTO(out_trans, rc);
1059 rc = dt_trans_start_local(env, d, th);
1061 GOTO(out_trans, rc);
1063 dt_write_lock(env, o, 0);
1064 if (dt_object_exists(o)) {
1066 dt_ref_del(env, o, th);
1067 dt_read_lock(env, llog_dir, 0);
1068 rc = dt_delete(env, llog_dir,
1069 (struct dt_key *) name,
1071 dt_read_unlock(env, llog_dir);
1073 CERROR("%s: can't remove llog %s: rc = %d\n",
1074 o->do_lu.lo_dev->ld_obd->obd_name,
1076 GOTO(out_unlock, rc);
1080 * XXX: compatibility bits
1081 * on old filesystems llogs are referenced by the name
1082 * on the new ones they are referenced by OI and by
1085 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
1087 GOTO(out_unlock, rc);
1088 LASSERT(lgi->lgi_attr.la_nlink < 2);
1089 if (lgi->lgi_attr.la_nlink == 1)
1090 dt_ref_del(env, o, th);
1091 rc = dt_destroy(env, o, th);
1093 GOTO(out_unlock, rc);
1096 dt_write_unlock(env, o);
1098 dt_trans_stop(env, d, th);
1099 if (llog_dir != NULL)
1100 lu_object_put(env, &llog_dir->do_lu);
1104 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1105 struct obd_llog_group *olg, int ctxt_idx,
1106 struct obd_device *disk_obd)
1108 struct local_oid_storage *los;
1109 struct llog_thread_info *lgi = llog_info(env);
1110 struct llog_ctxt *ctxt;
1116 LASSERT(olg->olg_ctxts[ctxt_idx]);
1118 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1121 /* initialize data allowing to generate new fids,
1122 * literally we need a sequece */
1123 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1124 lgi->lgi_fid.f_oid = 1;
1125 lgi->lgi_fid.f_ver = 0;
1126 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1127 &lgi->lgi_fid, &los);
1128 llog_ctxt_put(ctxt);
1132 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1134 struct dt_device *dt;
1135 struct ls_device *ls;
1136 struct local_oid_storage *los;
1138 LASSERT(ctxt->loc_exp->exp_obd);
1139 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1140 ls = ls_device_get(dt);
1142 RETURN(PTR_ERR(ls));
1144 mutex_lock(&ls->ls_los_mutex);
1145 los = dt_los_find(ls, FID_SEQ_LLOG);
1146 mutex_unlock(&ls->ls_los_mutex);
1149 local_oid_storage_fini(env, los);
1151 ls_device_put(env, ls);
1155 struct llog_operations llog_osd_ops = {
1156 .lop_next_block = llog_osd_next_block,
1157 .lop_prev_block = llog_osd_prev_block,
1158 .lop_read_header = llog_osd_read_header,
1159 .lop_destroy = llog_osd_destroy,
1160 .lop_setup = llog_osd_setup,
1161 .lop_cleanup = llog_osd_cleanup,
1162 .lop_open = llog_osd_open,
1163 .lop_exist = llog_osd_exist,
1164 .lop_declare_create = llog_osd_declare_create,
1165 .lop_create = llog_osd_create,
1166 .lop_declare_write_rec = llog_osd_declare_write_rec,
1167 .lop_write_rec = llog_osd_write_rec,
1168 .lop_close = llog_osd_close,
1170 EXPORT_SYMBOL(llog_osd_ops);
1172 /* reads the catalog list */
1173 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1174 int idx, int count, struct llog_catid *idarray)
1176 struct llog_thread_info *lgi = llog_info(env);
1177 struct dt_object *o = NULL;
1185 size = sizeof(*idarray) * count;
1186 lgi->lgi_off = idx * sizeof(*idarray);
1188 lu_local_obj_fid(&lgi->lgi_fid, LLOG_CATALOGS_OID);
1190 o = dt_locate(env, d, &lgi->lgi_fid);
1194 if (!dt_object_exists(o)) {
1195 th = dt_trans_create(env, d);
1197 GOTO(out, rc = PTR_ERR(th));
1199 lgi->lgi_attr.la_valid = LA_MODE;
1200 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1201 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1203 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1206 GOTO(out_trans, rc);
1208 rc = dt_trans_start_local(env, d, th);
1210 GOTO(out_trans, rc);
1212 dt_write_lock(env, o, 0);
1213 if (!dt_object_exists(o))
1214 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1216 dt_write_unlock(env, o);
1218 dt_trans_stop(env, d, th);
1223 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1227 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1228 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1229 o->do_lu.lo_dev->ld_obd->obd_name,
1230 lgi->lgi_attr.la_mode);
1231 GOTO(out, rc = -ENOENT);
1234 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1235 (int)lgi->lgi_attr.la_size, size);
1237 /* return just number of llogs */
1238 if (idarray == NULL) {
1239 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1243 /* read for new ost index or for empty file */
1244 memset(idarray, 0, size);
1245 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1247 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1248 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1250 lgi->lgi_buf.lb_buf = idarray;
1251 lgi->lgi_buf.lb_len = size;
1252 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1254 CERROR("%s: error reading CATALOGS: rc = %d\n",
1255 o->do_lu.lo_dev->ld_obd->obd_name, rc);
1261 lu_object_put(env, &o->do_lu);
1264 EXPORT_SYMBOL(llog_osd_get_cat_list);
1266 /* writes the cat list */
1267 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1268 int idx, int count, struct llog_catid *idarray)
1270 struct llog_thread_info *lgi = llog_info(env);
1271 struct dt_object *o = NULL;
1280 size = sizeof(*idarray) * count;
1281 lgi->lgi_off = idx * sizeof(*idarray);
1283 lu_local_obj_fid(&lgi->lgi_fid, LLOG_CATALOGS_OID);
1285 o = dt_locate(env, d, &lgi->lgi_fid);
1289 if (!dt_object_exists(o))
1290 GOTO(out, rc = -ENOENT);
1292 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1296 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1297 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1298 o->do_lu.lo_dev->ld_obd->obd_name,
1299 lgi->lgi_attr.la_mode);
1300 GOTO(out, rc = -ENOENT);
1303 th = dt_trans_create(env, d);
1305 GOTO(out, rc = PTR_ERR(th));
1307 rc = dt_declare_record_write(env, o, size, lgi->lgi_off, th);
1311 rc = dt_trans_start_local(env, d, th);
1313 GOTO(out_trans, rc);
1315 lgi->lgi_buf.lb_buf = idarray;
1316 lgi->lgi_buf.lb_len = size;
1317 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1319 CDEBUG(D_INODE, "error writeing CATALOGS: rc = %d\n", rc);
1321 dt_trans_stop(env, d, th);
1323 lu_object_put(env, &o->do_lu);
1326 EXPORT_SYMBOL(llog_osd_put_cat_list);