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
45 #include <obd_class.h>
46 #include <lustre_fid.h>
47 #include <dt_object.h>
49 #include "llog_internal.h"
50 #include "local_storage.h"
53 * - multi-chunks or big-declaration approach
54 * - use unique sequence instead of llog sb tracking unique ids
55 * - re-use existing environment
56 * - named llog support (can be used for testing only at the present)
57 * - llog_origin_connect() work with OSD API
60 static int llog_osd_declare_new_object(const struct lu_env *env,
61 struct local_oid_storage *los,
65 struct llog_thread_info *lgi = llog_info(env);
67 lgi->lgi_attr.la_valid = LA_MODE;
68 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
69 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
71 return local_object_declare_create(env, los, o, &lgi->lgi_attr,
75 static int llog_osd_create_new_object(const struct lu_env *env,
76 struct local_oid_storage *los,
80 struct llog_thread_info *lgi = llog_info(env);
82 lgi->lgi_attr.la_valid = LA_MODE;
83 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
84 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
86 return local_object_create(env, los, o, &lgi->lgi_attr,
90 static int llog_osd_pad(const struct lu_env *env, struct dt_object *o,
91 loff_t *off, int len, int index, struct thandle *th)
93 struct llog_thread_info *lgi = llog_info(env);
98 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
100 lgi->lgi_tail.lrt_len = lgi->lgi_lrh.lrh_len = len;
101 lgi->lgi_tail.lrt_index = lgi->lgi_lrh.lrh_index = index;
102 lgi->lgi_lrh.lrh_type = LLOG_PAD_MAGIC;
104 lgi->lgi_buf.lb_buf = &lgi->lgi_lrh;
105 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_lrh);
106 dt_write_lock(env, o, 0);
107 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
109 CERROR("%s: error writing padding record: rc = %d\n",
110 o->do_lu.lo_dev->ld_obd->obd_name, rc);
114 lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
115 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
116 *off += len - sizeof(lgi->lgi_lrh) - sizeof(lgi->lgi_tail);
117 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
119 CERROR("%s: error writing padding record: rc = %d\n",
120 o->do_lu.lo_dev->ld_obd->obd_name, rc);
122 dt_write_unlock(env, o);
126 static int llog_osd_write_blob(const struct lu_env *env, struct dt_object *o,
127 struct llog_rec_hdr *rec, void *buf,
128 loff_t *off, struct thandle *th)
130 struct llog_thread_info *lgi = llog_info(env);
131 int buflen = rec->lrh_len;
138 CWARN("0-length record\n");
140 CDEBUG(D_OTHER, "write blob with type %x, buf %p/%u at off %llu\n",
141 rec->lrh_type, buf, buflen, *off);
143 lgi->lgi_attr.la_valid = LA_SIZE;
144 lgi->lgi_attr.la_size = *off;
147 lgi->lgi_buf.lb_len = buflen;
148 lgi->lgi_buf.lb_buf = rec;
149 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
151 CERROR("%s: error writing log record: rc = %d\n",
152 o->do_lu.lo_dev->ld_obd->obd_name, rc);
157 /* protect the following 3 writes from concurrent read */
158 dt_write_lock(env, o, 0);
159 rec->lrh_len = sizeof(*rec) + buflen + sizeof(lgi->lgi_tail);
160 lgi->lgi_buf.lb_len = sizeof(*rec);
161 lgi->lgi_buf.lb_buf = rec;
162 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
164 CERROR("%s: error writing log hdr: rc = %d\n",
165 o->do_lu.lo_dev->ld_obd->obd_name, rc);
166 GOTO(out_unlock, rc);
169 lgi->lgi_buf.lb_len = buflen;
170 lgi->lgi_buf.lb_buf = buf;
171 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
173 CERROR("%s: error writing log buffer: rc = %d\n",
174 o->do_lu.lo_dev->ld_obd->obd_name, rc);
175 GOTO(out_unlock, rc);
178 lgi->lgi_tail.lrt_len = rec->lrh_len;
179 lgi->lgi_tail.lrt_index = rec->lrh_index;
180 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
181 lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
182 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
184 CERROR("%s: error writing log tail: rc = %d\n",
185 o->do_lu.lo_dev->ld_obd->obd_name, rc);
188 dt_write_unlock(env, o);
191 /* cleanup the content written above */
193 dt_punch(env, o, lgi->lgi_attr.la_size, OBD_OBJECT_EOF, th,
195 dt_attr_set(env, o, &lgi->lgi_attr, th, BYPASS_CAPA);
201 static int llog_osd_read_header(const struct lu_env *env,
202 struct llog_handle *handle)
204 struct llog_rec_hdr *llh_hdr;
206 struct llog_thread_info *lgi;
209 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
214 lgi = llog_info(env);
216 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
220 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
222 if (lgi->lgi_attr.la_size == 0) {
223 CDEBUG(D_HA, "not reading header from 0-byte log\n");
228 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
229 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
231 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
233 CERROR("%s: error reading log header from "DFID": rc = %d\n",
234 o->do_lu.lo_dev->ld_obd->obd_name,
235 PFID(lu_object_fid(&o->do_lu)), rc);
239 llh_hdr = &handle->lgh_hdr->llh_hdr;
240 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
241 lustre_swab_llog_hdr(handle->lgh_hdr);
243 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
244 CERROR("%s: bad log %s "DFID" header magic: %#x "
245 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
246 handle->lgh_name ? handle->lgh_name : "",
247 PFID(lu_object_fid(&o->do_lu)),
248 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
250 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
251 CERROR("%s: incorrectly sized log %s "DFID" header: "
252 "%#x (expected %#x)\n"
253 "you may need to re-run lconf --write_conf.\n",
254 o->do_lu.lo_dev->ld_obd->obd_name,
255 handle->lgh_name ? handle->lgh_name : "",
256 PFID(lu_object_fid(&o->do_lu)),
257 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
261 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
266 static int llog_osd_declare_write_rec(const struct lu_env *env,
267 struct llog_handle *loghandle,
268 struct llog_rec_hdr *rec,
269 int idx, struct thandle *th)
271 struct llog_thread_info *lgi = llog_info(env);
279 o = loghandle->lgh_obj;
282 /* each time we update header */
283 rc = dt_declare_record_write(env, o, sizeof(struct llog_log_hdr), 0,
285 if (rc || idx == 0) /* if error or just header */
288 if (dt_object_exists(o)) {
289 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
290 lgi->lgi_off = lgi->lgi_attr.la_size;
291 LASSERT(ergo(rc == 0, lgi->lgi_attr.la_valid & LA_SIZE));
295 rc = dt_declare_punch(env, o, lgi->lgi_off, OBD_OBJECT_EOF, th);
302 /* XXX: implement declared window or multi-chunks approach */
303 rc = dt_declare_record_write(env, o, 32 * 1024, lgi->lgi_off, th);
308 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
309 /* appends if idx == -1, otherwise overwrites record idx. */
310 static int llog_osd_write_rec(const struct lu_env *env,
311 struct llog_handle *loghandle,
312 struct llog_rec_hdr *rec,
313 struct llog_cookie *reccookie, int cookiecount,
314 void *buf, int idx, struct thandle *th)
316 struct llog_thread_info *lgi = llog_info(env);
317 struct llog_log_hdr *llh;
318 int reclen = rec->lrh_len;
319 int index, rc, old_tail_idx;
320 struct llog_rec_tail *lrt;
325 llh = loghandle->lgh_hdr;
327 o = loghandle->lgh_obj;
331 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
332 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
334 /* record length should not bigger than LLOG_CHUNK_SIZE */
336 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
337 sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
339 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
343 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
348 /* write_blob adds header and tail to lrh_len. */
349 reclen = sizeof(*rec) + rec->lrh_len +
350 sizeof(struct llog_rec_tail);
353 /* no header: only allowed to insert record 1 */
354 if (idx != 1 && lgi->lgi_attr.la_size == 0)
357 if (idx && llh->llh_size && llh->llh_size != rec->lrh_len)
360 if (!ext2_test_bit(idx, llh->llh_bitmap))
361 CERROR("%s: modify unset record %u\n",
362 o->do_lu.lo_dev->ld_obd->obd_name, idx);
363 if (idx != rec->lrh_index)
364 CERROR("%s: index mismatch %d %u\n",
365 o->do_lu.lo_dev->ld_obd->obd_name, idx,
369 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
371 /* we are done if we only write the header or on error */
376 /* We assume that caller has set lgh_cur_* */
377 lgi->lgi_off = loghandle->lgh_cur_offset;
379 "modify record "DOSTID": idx:%d/%u/%d, len:%u "
381 POSTID(&loghandle->lgh_id.lgl_oi), idx,
383 loghandle->lgh_cur_idx, rec->lrh_len,
384 (long long)(lgi->lgi_off - sizeof(*llh)));
385 if (rec->lrh_index != loghandle->lgh_cur_idx) {
386 CERROR("%s: modify idx mismatch %u/%d\n",
387 o->do_lu.lo_dev->ld_obd->obd_name, idx,
388 loghandle->lgh_cur_idx);
392 /* Assumes constant lrh_len */
393 lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
396 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
397 if (rc == 0 && reccookie) {
398 reccookie->lgc_lgl = loghandle->lgh_id;
399 reccookie->lgc_index = idx;
405 /* Make sure that records don't cross a chunk boundary, so we can
406 * process them page-at-a-time if needed. If it will cross a chunk
407 * boundary, write in a fake (but referenced) entry to pad the chunk.
409 * We know that llog_current_log() will return a loghandle that is
410 * big enough to hold reclen, so all we care about is padding here.
412 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
413 lgi->lgi_off = lgi->lgi_attr.la_size;
414 left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
415 /* NOTE: padding is a record, but no bit is set */
416 if (left != 0 && left != reclen &&
417 left < (reclen + LLOG_MIN_REC_SIZE)) {
418 index = loghandle->lgh_last_idx + 1;
419 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
422 loghandle->lgh_last_idx++; /*for pad rec*/
424 /* if it's the last idx in log file, then return -ENOSPC */
425 if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
428 loghandle->lgh_last_idx++;
429 index = loghandle->lgh_last_idx;
430 LASSERT(index < LLOG_BITMAP_SIZE(llh));
431 rec->lrh_index = index;
433 lrt = (struct llog_rec_tail *)((char *)rec + rec->lrh_len -
435 lrt->lrt_len = rec->lrh_len;
436 lrt->lrt_index = rec->lrh_index;
438 /* The caller should make sure only 1 process access the lgh_last_idx,
439 * Otherwise it might hit the assert.*/
440 LASSERT(index < LLOG_BITMAP_SIZE(llh));
441 spin_lock(&loghandle->lgh_hdr_lock);
442 if (ext2_set_bit(index, llh->llh_bitmap)) {
443 CERROR("%s: index %u already set in log bitmap\n",
444 o->do_lu.lo_dev->ld_obd->obd_name, index);
445 spin_unlock(&loghandle->lgh_hdr_lock);
446 LBUG(); /* should never happen */
449 spin_unlock(&loghandle->lgh_hdr_lock);
450 old_tail_idx = llh->llh_tail.lrt_index;
451 llh->llh_tail.lrt_index = index;
454 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL, &lgi->lgi_off,
459 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
463 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
464 lgi->lgi_off = lgi->lgi_attr.la_size;
466 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
469 /* cleanup llog for error case */
471 spin_lock(&loghandle->lgh_hdr_lock);
472 ext2_clear_bit(index, llh->llh_bitmap);
474 spin_unlock(&loghandle->lgh_hdr_lock);
476 /* restore the header */
477 loghandle->lgh_last_idx--;
478 llh->llh_tail.lrt_index = old_tail_idx;
480 llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
484 CDEBUG(D_RPCTRACE, "added record "DOSTID": idx: %u, %u\n",
485 POSTID(&loghandle->lgh_id.lgl_oi), index, rec->lrh_len);
486 if (rc == 0 && reccookie) {
487 reccookie->lgc_lgl = loghandle->lgh_id;
488 reccookie->lgc_index = index;
489 if ((rec->lrh_type == MDS_UNLINK_REC) ||
490 (rec->lrh_type == MDS_SETATTR64_REC))
491 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
492 else if (rec->lrh_type == OST_SZ_REC)
493 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
495 reccookie->lgc_subsys = -1;
501 /* We can skip reading at least as many log blocks as the number of
502 * minimum sized log records we are skipping. If it turns out
503 * that we are not far enough along the log (because the
504 * actual records are larger than minimum size) we just skip
507 static void llog_skip_over(__u64 *off, int curr, int goal)
511 *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
512 ~(LLOG_CHUNK_SIZE - 1);
516 * - cur_offset to the furthest point read in the log file
517 * - cur_idx to the log index preceeding cur_offset
518 * returns -EIO/-EINVAL on error
520 static int llog_osd_next_block(const struct lu_env *env,
521 struct llog_handle *loghandle, int *cur_idx,
522 int next_idx, __u64 *cur_offset, void *buf,
525 struct llog_thread_info *lgi = llog_info(env);
527 struct dt_device *dt;
533 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
536 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
537 next_idx, *cur_idx, *cur_offset);
540 LASSERT(loghandle->lgh_ctxt);
542 o = loghandle->lgh_obj;
544 LASSERT(dt_object_exists(o));
545 dt = lu2dt_dev(o->do_lu.lo_dev);
548 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
552 while (*cur_offset < lgi->lgi_attr.la_size) {
553 struct llog_rec_hdr *rec, *last_rec;
554 struct llog_rec_tail *tail;
556 llog_skip_over(cur_offset, *cur_idx, next_idx);
558 /* read up to next LLOG_CHUNK_SIZE block */
559 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
560 (*cur_offset & (LLOG_CHUNK_SIZE - 1));
561 lgi->lgi_buf.lb_buf = buf;
563 /* Note: read lock is not needed around la_size get above at
564 * the time of dt_attr_get(). There are only two cases that
565 * matter. Either la_size == cur_offset, in which case the
566 * entire read is skipped, or la_size > cur_offset and the loop
567 * is entered and this thread is blocked at dt_read_lock()
568 * until the write is completed. When the write completes, then
569 * the dt_read() will be done with the full length, and will
572 dt_read_lock(env, o, 0);
573 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
574 dt_read_unlock(env, o);
576 CERROR("%s: can't read llog block from log "DFID
577 " offset "LPU64": rc = %d\n",
578 o->do_lu.lo_dev->ld_obd->obd_name,
579 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
585 /* signal the end of the valid buffer to
587 memset(buf + rc, 0, len - rc);
590 if (rc == 0) /* end of file, nothing to do */
593 if (rc < sizeof(*tail)) {
594 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
596 o->do_lu.lo_dev->ld_obd->obd_name,
597 POSTID(&loghandle->lgh_id.lgl_oi),
598 loghandle->lgh_id.lgl_ogen, *cur_offset);
599 GOTO(out, rc = -EINVAL);
603 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
604 lustre_swab_llog_rec(rec);
606 tail = (struct llog_rec_tail *)((char *)buf + rc -
607 sizeof(struct llog_rec_tail));
608 /* get the last record in block */
609 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
610 le32_to_cpu(tail->lrt_len));
612 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
613 lustre_swab_llog_rec(last_rec);
614 LASSERT(last_rec->lrh_index == tail->lrt_index);
616 *cur_idx = tail->lrt_index;
618 /* this shouldn't happen */
619 if (tail->lrt_index == 0) {
620 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
622 o->do_lu.lo_dev->ld_obd->obd_name,
623 POSTID(&loghandle->lgh_id.lgl_oi),
624 loghandle->lgh_id.lgl_ogen, *cur_offset);
625 GOTO(out, rc = -EINVAL);
627 if (tail->lrt_index < next_idx)
630 /* sanity check that the start of the new buffer is no farther
631 * than the record that we wanted. This shouldn't happen. */
632 if (rec->lrh_index > next_idx) {
633 CERROR("%s: missed desired record? %u > %u\n",
634 o->do_lu.lo_dev->ld_obd->obd_name,
635 rec->lrh_index, next_idx);
636 GOTO(out, rc = -ENOENT);
640 GOTO(out, rc = -EIO);
645 static int llog_osd_prev_block(const struct lu_env *env,
646 struct llog_handle *loghandle,
647 int prev_idx, void *buf, int len)
649 struct llog_thread_info *lgi = llog_info(env);
651 struct dt_device *dt;
655 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
658 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
661 LASSERT(loghandle->lgh_ctxt);
663 o = loghandle->lgh_obj;
665 LASSERT(dt_object_exists(o));
666 dt = lu2dt_dev(o->do_lu.lo_dev);
669 cur_offset = LLOG_CHUNK_SIZE;
670 llog_skip_over(&cur_offset, 0, prev_idx);
672 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
676 while (cur_offset < lgi->lgi_attr.la_size) {
677 struct llog_rec_hdr *rec, *last_rec;
678 struct llog_rec_tail *tail;
680 lgi->lgi_buf.lb_len = len;
681 lgi->lgi_buf.lb_buf = buf;
682 /* It is OK to have locking around dt_read() only, see
683 * comment in llog_osd_next_block for details
685 dt_read_lock(env, o, 0);
686 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
687 dt_read_unlock(env, o);
689 CERROR("%s: can't read llog block from log "DFID
690 " offset "LPU64": rc = %d\n",
691 o->do_lu.lo_dev->ld_obd->obd_name,
692 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
696 if (rc == 0) /* end of file, nothing to do */
699 if (rc < sizeof(*tail)) {
700 CERROR("%s: invalid llog block at log id "DOSTID"/%u "
702 o->do_lu.lo_dev->ld_obd->obd_name,
703 POSTID(&loghandle->lgh_id.lgl_oi),
704 loghandle->lgh_id.lgl_ogen, cur_offset);
705 GOTO(out, rc = -EINVAL);
709 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
710 lustre_swab_llog_rec(rec);
712 tail = (struct llog_rec_tail *)((char *)buf + rc -
713 sizeof(struct llog_rec_tail));
714 /* get the last record in block */
715 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
716 le32_to_cpu(tail->lrt_len));
718 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
719 lustre_swab_llog_rec(last_rec);
720 LASSERT(last_rec->lrh_index == tail->lrt_index);
722 /* this shouldn't happen */
723 if (tail->lrt_index == 0) {
724 CERROR("%s: invalid llog tail at log id "DOSTID"/%u "
726 o->do_lu.lo_dev->ld_obd->obd_name,
727 POSTID(&loghandle->lgh_id.lgl_oi),
728 loghandle->lgh_id.lgl_ogen, cur_offset);
729 GOTO(out, rc = -EINVAL);
731 if (tail->lrt_index < prev_idx)
734 /* sanity check that the start of the new buffer is no farther
735 * than the record that we wanted. This shouldn't happen. */
736 if (rec->lrh_index > prev_idx) {
737 CERROR("%s: missed desired record? %u > %u\n",
738 o->do_lu.lo_dev->ld_obd->obd_name,
739 rec->lrh_index, prev_idx);
740 GOTO(out, rc = -ENOENT);
744 GOTO(out, rc = -EIO);
749 struct dt_object *llog_osd_dir_get(const struct lu_env *env,
750 struct llog_ctxt *ctxt)
752 struct dt_device *dt;
753 struct dt_thread_info *dti = dt_info(env);
754 struct dt_object *dir;
757 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
758 if (ctxt->loc_dir == NULL) {
759 rc = dt_root_get(env, dt, &dti->dti_fid);
762 dir = dt_locate(env, dt, &dti->dti_fid);
764 lu_object_get(&ctxt->loc_dir->do_lu);
771 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
772 struct llog_logid *logid, char *name,
773 enum llog_open_param open_param)
775 struct llog_thread_info *lgi = llog_info(env);
776 struct llog_ctxt *ctxt = handle->lgh_ctxt;
778 struct dt_device *dt;
779 struct ls_device *ls;
780 struct local_oid_storage *los;
785 LASSERT(ctxt->loc_exp);
786 LASSERT(ctxt->loc_exp->exp_obd);
787 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
790 ls = ls_device_get(dt);
794 mutex_lock(&ls->ls_los_mutex);
795 los = dt_los_find(ls, name != NULL ? FID_SEQ_LLOG_NAME : FID_SEQ_LLOG);
796 mutex_unlock(&ls->ls_los_mutex);
798 ls_device_put(env, ls);
803 logid_to_fid(logid, &lgi->lgi_fid);
805 struct dt_object *llog_dir;
807 llog_dir = llog_osd_dir_get(env, ctxt);
808 if (IS_ERR(llog_dir))
809 GOTO(out, rc = PTR_ERR(llog_dir));
810 dt_read_lock(env, llog_dir, 0);
811 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
812 dt_read_unlock(env, llog_dir);
813 lu_object_put(env, &llog_dir->do_lu);
814 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
815 /* generate fid for new llog */
816 rc = local_object_fid_generate(env, los,
821 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
822 if (handle->lgh_name)
823 strcpy(handle->lgh_name, name);
825 GOTO(out, rc = -ENOMEM);
827 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
828 /* generate fid for new llog */
829 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
834 o = ls_locate(env, ls, &lgi->lgi_fid);
836 GOTO(out_name, rc = PTR_ERR(o));
838 /* No new llog is expected but doesn't exist */
839 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
840 GOTO(out_put, rc = -ENOENT);
842 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
844 handle->private_data = los;
845 LASSERT(handle->lgh_ctxt);
850 lu_object_put(env, &o->do_lu);
852 if (handle->lgh_name != NULL)
853 OBD_FREE(handle->lgh_name, strlen(name) + 1);
859 static int llog_osd_exist(struct llog_handle *handle)
861 LASSERT(handle->lgh_obj);
862 return (dt_object_exists(handle->lgh_obj) &&
863 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
866 static int llog_osd_declare_create(const struct lu_env *env,
867 struct llog_handle *res, struct thandle *th)
869 struct llog_thread_info *lgi = llog_info(env);
870 struct local_oid_storage *los;
874 LASSERT(res->lgh_obj);
877 /* object can be created by another thread */
879 if (dt_object_exists(o))
882 los = res->private_data;
885 rc = llog_osd_declare_new_object(env, los, o, th);
889 rc = dt_declare_record_write(env, o, LLOG_CHUNK_SIZE, 0, th);
894 struct dt_object *llog_dir;
896 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
897 if (IS_ERR(llog_dir))
898 return PTR_ERR(llog_dir);
899 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
900 rc = dt_declare_insert(env, llog_dir,
901 (struct dt_rec *)&lgi->lgi_fid,
902 (struct dt_key *)res->lgh_name, th);
903 lu_object_put(env, &llog_dir->do_lu);
905 CERROR("%s: can't declare named llog %s: rc = %d\n",
906 o->do_lu.lo_dev->ld_obd->obd_name,
912 /* This is a callback from the llog_* functions.
913 * Assumes caller has already pushed us into the kernel context. */
914 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
917 struct llog_thread_info *lgi = llog_info(env);
918 struct local_oid_storage *los;
926 /* llog can be already created */
927 if (dt_object_exists(o))
930 los = res->private_data;
933 dt_write_lock(env, o, 0);
934 if (!dt_object_exists(o))
935 rc = llog_osd_create_new_object(env, los, o, th);
939 dt_write_unlock(env, o);
944 struct dt_object *llog_dir;
946 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
947 if (IS_ERR(llog_dir))
948 return PTR_ERR(llog_dir);
950 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
951 dt_read_lock(env, llog_dir, 0);
952 rc = dt_insert(env, llog_dir,
953 (struct dt_rec *)&lgi->lgi_fid,
954 (struct dt_key *)res->lgh_name,
956 dt_read_unlock(env, llog_dir);
957 lu_object_put(env, &llog_dir->do_lu);
959 CERROR("%s: can't create named llog %s: rc = %d\n",
960 o->do_lu.lo_dev->ld_obd->obd_name,
966 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
968 struct local_oid_storage *los;
971 LASSERT(handle->lgh_obj);
973 lu_object_put(env, &handle->lgh_obj->do_lu);
975 los = handle->private_data;
979 if (handle->lgh_name)
980 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
985 static int llog_osd_destroy(const struct lu_env *env,
986 struct llog_handle *loghandle)
988 struct llog_ctxt *ctxt;
989 struct dt_object *o, *llog_dir = NULL;
995 ctxt = loghandle->lgh_ctxt;
998 o = loghandle->lgh_obj;
1001 d = lu2dt_dev(o->do_lu.lo_dev);
1003 LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1005 th = dt_trans_create(env, d);
1009 if (loghandle->lgh_name) {
1010 llog_dir = llog_osd_dir_get(env, ctxt);
1011 if (IS_ERR(llog_dir))
1012 GOTO(out_trans, rc = PTR_ERR(llog_dir));
1014 name = loghandle->lgh_name;
1015 rc = dt_declare_delete(env, llog_dir,
1016 (struct dt_key *)name, th);
1018 GOTO(out_trans, rc);
1021 dt_declare_ref_del(env, o, th);
1023 rc = dt_declare_destroy(env, o, th);
1025 GOTO(out_trans, rc);
1027 rc = dt_trans_start_local(env, d, th);
1029 GOTO(out_trans, rc);
1031 dt_write_lock(env, o, 0);
1032 if (dt_object_exists(o)) {
1034 dt_read_lock(env, llog_dir, 0);
1035 rc = dt_delete(env, llog_dir,
1036 (struct dt_key *) name,
1038 dt_read_unlock(env, llog_dir);
1040 CERROR("%s: can't remove llog %s: rc = %d\n",
1041 o->do_lu.lo_dev->ld_obd->obd_name,
1043 GOTO(out_unlock, rc);
1046 dt_ref_del(env, o, th);
1047 rc = dt_destroy(env, o, th);
1049 GOTO(out_unlock, rc);
1052 dt_write_unlock(env, o);
1054 dt_trans_stop(env, d, th);
1055 if (llog_dir != NULL)
1056 lu_object_put(env, &llog_dir->do_lu);
1060 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1061 struct obd_llog_group *olg, int ctxt_idx,
1062 struct obd_device *disk_obd)
1064 struct local_oid_storage *los;
1065 struct llog_thread_info *lgi = llog_info(env);
1066 struct llog_ctxt *ctxt;
1070 LASSERT(olg->olg_ctxts[ctxt_idx]);
1072 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1075 /* initialize data allowing to generate new fids,
1076 * literally we need a sequece */
1077 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1078 lgi->lgi_fid.f_oid = 1;
1079 lgi->lgi_fid.f_ver = 0;
1080 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1081 &lgi->lgi_fid, &los);
1085 lgi->lgi_fid.f_seq = FID_SEQ_LLOG_NAME;
1086 lgi->lgi_fid.f_oid = 1;
1087 lgi->lgi_fid.f_ver = 0;
1088 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1089 &lgi->lgi_fid, &los);
1090 llog_ctxt_put(ctxt);
1094 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1096 struct dt_device *dt;
1097 struct ls_device *ls;
1098 struct local_oid_storage *los, *nlos;
1100 LASSERT(ctxt->loc_exp->exp_obd);
1101 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1102 ls = ls_device_get(dt);
1106 mutex_lock(&ls->ls_los_mutex);
1107 los = dt_los_find(ls, FID_SEQ_LLOG);
1108 nlos = dt_los_find(ls, FID_SEQ_LLOG_NAME);
1109 mutex_unlock(&ls->ls_los_mutex);
1112 local_oid_storage_fini(env, los);
1116 local_oid_storage_fini(env, nlos);
1118 ls_device_put(env, ls);
1122 struct llog_operations llog_osd_ops = {
1123 .lop_next_block = llog_osd_next_block,
1124 .lop_prev_block = llog_osd_prev_block,
1125 .lop_read_header = llog_osd_read_header,
1126 .lop_destroy = llog_osd_destroy,
1127 .lop_setup = llog_osd_setup,
1128 .lop_cleanup = llog_osd_cleanup,
1129 .lop_open = llog_osd_open,
1130 .lop_exist = llog_osd_exist,
1131 .lop_declare_create = llog_osd_declare_create,
1132 .lop_create = llog_osd_create,
1133 .lop_declare_write_rec = llog_osd_declare_write_rec,
1134 .lop_write_rec = llog_osd_write_rec,
1135 .lop_close = llog_osd_close,
1137 EXPORT_SYMBOL(llog_osd_ops);
1139 /* reads the catalog list */
1140 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1141 int idx, int count, struct llog_catid *idarray)
1143 struct llog_thread_info *lgi = llog_info(env);
1144 struct dt_object *o = NULL;
1150 size = sizeof(*idarray) * count;
1151 lgi->lgi_off = idx * sizeof(*idarray);
1153 lu_local_obj_fid(&lgi->lgi_fid, LLOG_CATALOGS_OID);
1155 o = dt_locate(env, d, &lgi->lgi_fid);
1159 if (!dt_object_exists(o)) {
1160 th = dt_trans_create(env, d);
1162 GOTO(out, rc = PTR_ERR(th));
1164 lgi->lgi_attr.la_valid = LA_MODE;
1165 lgi->lgi_attr.la_mode = S_IFREG | S_IRUGO | S_IWUSR;
1166 lgi->lgi_dof.dof_type = dt_mode_to_dft(S_IFREG);
1168 rc = dt_declare_create(env, o, &lgi->lgi_attr, NULL,
1171 GOTO(out_trans, rc);
1173 rc = dt_trans_start_local(env, d, th);
1175 GOTO(out_trans, rc);
1177 dt_write_lock(env, o, 0);
1178 if (!dt_object_exists(o))
1179 rc = dt_create(env, o, &lgi->lgi_attr, NULL,
1181 dt_write_unlock(env, o);
1183 dt_trans_stop(env, d, th);
1188 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1192 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1193 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1194 o->do_lu.lo_dev->ld_obd->obd_name,
1195 lgi->lgi_attr.la_mode);
1196 GOTO(out, rc = -ENOENT);
1199 CDEBUG(D_CONFIG, "cat list: disk size=%d, read=%d\n",
1200 (int)lgi->lgi_attr.la_size, size);
1202 /* return just number of llogs */
1203 if (idarray == NULL) {
1204 rc = lgi->lgi_attr.la_size / sizeof(*idarray);
1208 /* read for new ost index or for empty file */
1209 memset(idarray, 0, size);
1210 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1212 if (lgi->lgi_attr.la_size < lgi->lgi_off + size)
1213 size = lgi->lgi_attr.la_size - lgi->lgi_off;
1215 lgi->lgi_buf.lb_buf = idarray;
1216 lgi->lgi_buf.lb_len = size;
1217 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
1219 CERROR("%s: error reading CATALOGS: rc = %d\n",
1220 o->do_lu.lo_dev->ld_obd->obd_name, rc);
1225 lu_object_put(env, &o->do_lu);
1228 EXPORT_SYMBOL(llog_osd_get_cat_list);
1230 /* writes the cat list */
1231 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1232 int idx, int count, struct llog_catid *idarray)
1234 struct llog_thread_info *lgi = llog_info(env);
1235 struct dt_object *o = NULL;
1244 size = sizeof(*idarray) * count;
1245 lgi->lgi_off = idx * sizeof(*idarray);
1247 lu_local_obj_fid(&lgi->lgi_fid, LLOG_CATALOGS_OID);
1249 o = dt_locate(env, d, &lgi->lgi_fid);
1253 if (!dt_object_exists(o))
1254 GOTO(out, rc = -ENOENT);
1256 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
1260 if (!S_ISREG(lgi->lgi_attr.la_mode)) {
1261 CERROR("%s: CATALOGS is not a regular file!: mode = %o\n",
1262 o->do_lu.lo_dev->ld_obd->obd_name,
1263 lgi->lgi_attr.la_mode);
1264 GOTO(out, rc = -ENOENT);
1267 th = dt_trans_create(env, d);
1269 GOTO(out, rc = PTR_ERR(th));
1271 rc = dt_declare_record_write(env, o, size, lgi->lgi_off, th);
1275 rc = dt_trans_start_local(env, d, th);
1277 GOTO(out_trans, rc);
1279 lgi->lgi_buf.lb_buf = idarray;
1280 lgi->lgi_buf.lb_len = size;
1281 rc = dt_record_write(env, o, &lgi->lgi_buf, &lgi->lgi_off, th);
1283 CDEBUG(D_INODE, "error writeing CATALOGS: rc = %d\n", rc);
1285 dt_trans_stop(env, d, th);
1287 lu_object_put(env, &o->do_lu);
1290 EXPORT_SYMBOL(llog_osd_put_cat_list);