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);
152 lgi->lgi_buf.lb_len = buflen;
153 lgi->lgi_buf.lb_buf = rec;
154 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
156 CERROR("%s: error writing log record: rc = %d\n",
157 o->do_lu.lo_dev->ld_obd->obd_name, rc);
162 /* protect the following 3 writes from concurrent read */
163 dt_write_lock(env, o, 0);
164 rec->lrh_len = sizeof(*rec) + buflen + sizeof(lgi->lgi_tail);
165 lgi->lgi_buf.lb_len = sizeof(*rec);
166 lgi->lgi_buf.lb_buf = rec;
167 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
169 CERROR("%s: error writing log hdr: rc = %d\n",
170 o->do_lu.lo_dev->ld_obd->obd_name, rc);
174 lgi->lgi_buf.lb_len = buflen;
175 lgi->lgi_buf.lb_buf = buf;
176 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
178 CERROR("%s: error writing log buffer: rc = %d\n",
179 o->do_lu.lo_dev->ld_obd->obd_name, rc);
183 lgi->lgi_tail.lrt_len = rec->lrh_len;
184 lgi->lgi_tail.lrt_index = rec->lrh_index;
185 lgi->lgi_buf.lb_len = sizeof(lgi->lgi_tail);
186 lgi->lgi_buf.lb_buf = &lgi->lgi_tail;
187 rc = dt_record_write(env, o, &lgi->lgi_buf, off, th);
189 CERROR("%s: error writing log tail: rc = %d\n",
190 o->do_lu.lo_dev->ld_obd->obd_name, rc);
192 dt_write_unlock(env, o);
196 static int llog_osd_read_header(const struct lu_env *env,
197 struct llog_handle *handle)
199 struct llog_rec_hdr *llh_hdr;
201 struct llog_thread_info *lgi;
206 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
211 lgi = llog_info(env);
213 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
217 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
219 if (lgi->lgi_attr.la_size == 0) {
220 CDEBUG(D_HA, "not reading header from 0-byte log\n");
225 lgi->lgi_buf.lb_buf = handle->lgh_hdr;
226 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE;
228 rc = dt_record_read(env, o, &lgi->lgi_buf, &lgi->lgi_off);
230 CERROR("%s: error reading log header from "DFID": rc = %d\n",
231 o->do_lu.lo_dev->ld_obd->obd_name,
232 PFID(lu_object_fid(&o->do_lu)), rc);
236 llh_hdr = &handle->lgh_hdr->llh_hdr;
237 if (LLOG_REC_HDR_NEEDS_SWABBING(llh_hdr))
238 lustre_swab_llog_hdr(handle->lgh_hdr);
240 if (llh_hdr->lrh_type != LLOG_HDR_MAGIC) {
241 CERROR("%s: bad log %s "DFID" header magic: %#x "
242 "(expected %#x)\n", o->do_lu.lo_dev->ld_obd->obd_name,
243 handle->lgh_name ? handle->lgh_name : "",
244 PFID(lu_object_fid(&o->do_lu)),
245 llh_hdr->lrh_type, LLOG_HDR_MAGIC);
247 } else if (llh_hdr->lrh_len != LLOG_CHUNK_SIZE) {
248 CERROR("%s: incorrectly sized log %s "DFID" header: "
249 "%#x (expected %#x)\n"
250 "you may need to re-run lconf --write_conf.\n",
251 o->do_lu.lo_dev->ld_obd->obd_name,
252 handle->lgh_name ? handle->lgh_name : "",
253 PFID(lu_object_fid(&o->do_lu)),
254 llh_hdr->lrh_len, LLOG_CHUNK_SIZE);
258 handle->lgh_last_idx = handle->lgh_hdr->llh_tail.lrt_index;
263 static int llog_osd_declare_write_rec(const struct lu_env *env,
264 struct llog_handle *loghandle,
265 struct llog_rec_hdr *rec,
266 int idx, struct thandle *th)
268 struct llog_thread_info *lgi = llog_info(env);
278 o = loghandle->lgh_obj;
281 /* each time we update header */
282 rc = dt_declare_record_write(env, o, sizeof(struct llog_log_hdr), 0,
284 if (rc || idx == 0) /* if error or just header */
287 if (dt_object_exists(o)) {
288 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
289 lgi->lgi_off = lgi->lgi_attr.la_size;
290 LASSERT(ergo(rc == 0, lgi->lgi_attr.la_valid & LA_SIZE));
297 /* XXX: implement declared window or multi-chunks approach */
298 rc = dt_declare_record_write(env, o, 32 * 1024, lgi->lgi_off, th);
303 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
304 /* appends if idx == -1, otherwise overwrites record idx. */
305 static int llog_osd_write_rec(const struct lu_env *env,
306 struct llog_handle *loghandle,
307 struct llog_rec_hdr *rec,
308 struct llog_cookie *reccookie, int cookiecount,
309 void *buf, int idx, struct thandle *th)
311 struct llog_thread_info *lgi = llog_info(env);
312 struct llog_log_hdr *llh;
313 int reclen = rec->lrh_len;
315 struct llog_rec_tail *lrt;
322 llh = loghandle->lgh_hdr;
324 o = loghandle->lgh_obj;
328 CDEBUG(D_OTHER, "new record %x to "DFID"\n",
329 rec->lrh_type, PFID(lu_object_fid(&o->do_lu)));
331 /* record length should not bigger than LLOG_CHUNK_SIZE */
333 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr) -
334 sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
336 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
340 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
345 /* write_blob adds header and tail to lrh_len. */
346 reclen = sizeof(*rec) + rec->lrh_len +
347 sizeof(struct llog_rec_tail);
350 /* no header: only allowed to insert record 1 */
351 if (idx != 1 && lgi->lgi_attr.la_size == 0)
354 if (idx && llh->llh_size && llh->llh_size != rec->lrh_len)
357 if (!ext2_test_bit(idx, llh->llh_bitmap))
358 CERROR("%s: modify unset record %u\n",
359 o->do_lu.lo_dev->ld_obd->obd_name, idx);
360 if (idx != rec->lrh_index)
361 CERROR("%s: index mismatch %d %u\n",
362 o->do_lu.lo_dev->ld_obd->obd_name, idx,
366 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL,
368 /* we are done if we only write the header or on error */
373 /* We assume that caller has set lgh_cur_* */
374 lgi->lgi_off = loghandle->lgh_cur_offset;
376 "modify record "LPX64": idx:%d/%u/%d, len:%u "
378 loghandle->lgh_id.lgl_oid, idx, rec->lrh_index,
379 loghandle->lgh_cur_idx, rec->lrh_len,
380 (long long)(lgi->lgi_off - sizeof(*llh)));
381 if (rec->lrh_index != loghandle->lgh_cur_idx) {
382 CERROR("%s: modify idx mismatch %u/%d\n",
383 o->do_lu.lo_dev->ld_obd->obd_name, idx,
384 loghandle->lgh_cur_idx);
388 /* Assumes constant lrh_len */
389 lgi->lgi_off = sizeof(*llh) + (idx - 1) * reclen;
392 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
393 if (rc == 0 && reccookie) {
394 reccookie->lgc_lgl = loghandle->lgh_id;
395 reccookie->lgc_index = idx;
401 /* Make sure that records don't cross a chunk boundary, so we can
402 * process them page-at-a-time if needed. If it will cross a chunk
403 * boundary, write in a fake (but referenced) entry to pad the chunk.
405 * We know that llog_current_log() will return a loghandle that is
406 * big enough to hold reclen, so all we care about is padding here.
408 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
409 lgi->lgi_off = lgi->lgi_attr.la_size;
410 left = LLOG_CHUNK_SIZE - (lgi->lgi_off & (LLOG_CHUNK_SIZE - 1));
411 /* NOTE: padding is a record, but no bit is set */
412 if (left != 0 && left != reclen &&
413 left < (reclen + LLOG_MIN_REC_SIZE)) {
414 index = loghandle->lgh_last_idx + 1;
415 rc = llog_osd_pad(env, o, &lgi->lgi_off, left, index, th);
418 loghandle->lgh_last_idx++; /*for pad rec*/
420 /* if it's the last idx in log file, then return -ENOSPC */
421 if (loghandle->lgh_last_idx >= LLOG_BITMAP_SIZE(llh) - 1)
424 loghandle->lgh_last_idx++;
425 index = loghandle->lgh_last_idx;
426 LASSERT(index < LLOG_BITMAP_SIZE(llh));
427 rec->lrh_index = index;
429 lrt = (struct llog_rec_tail *)((char *)rec + rec->lrh_len -
431 lrt->lrt_len = rec->lrh_len;
432 lrt->lrt_index = rec->lrh_index;
434 /* The caller should make sure only 1 process access the lgh_last_idx,
435 * Otherwise it might hit the assert.*/
436 LASSERT(index < LLOG_BITMAP_SIZE(llh));
437 spin_lock(&loghandle->lgh_hdr_lock);
438 if (ext2_set_bit(index, llh->llh_bitmap)) {
439 CERROR("%s: index %u already set in log bitmap\n",
440 o->do_lu.lo_dev->ld_obd->obd_name, index);
441 spin_unlock(&loghandle->lgh_hdr_lock);
442 LBUG(); /* should never happen */
445 spin_unlock(&loghandle->lgh_hdr_lock);
446 llh->llh_tail.lrt_index = index;
449 rc = llog_osd_write_blob(env, o, &llh->llh_hdr, NULL, &lgi->lgi_off,
454 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
457 LASSERT(lgi->lgi_attr.la_valid & LA_SIZE);
458 lgi->lgi_off = lgi->lgi_attr.la_size;
460 rc = llog_osd_write_blob(env, o, rec, buf, &lgi->lgi_off, th);
464 CDEBUG(D_RPCTRACE, "added record "LPX64": idx: %u, %u\n",
465 loghandle->lgh_id.lgl_oid, index, rec->lrh_len);
466 if (rc == 0 && reccookie) {
467 reccookie->lgc_lgl = loghandle->lgh_id;
468 reccookie->lgc_index = index;
469 if ((rec->lrh_type == MDS_UNLINK_REC) ||
470 (rec->lrh_type == MDS_SETATTR64_REC))
471 reccookie->lgc_subsys = LLOG_MDS_OST_ORIG_CTXT;
472 else if (rec->lrh_type == OST_SZ_REC)
473 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
475 reccookie->lgc_subsys = -1;
481 /* We can skip reading at least as many log blocks as the number of
482 * minimum sized log records we are skipping. If it turns out
483 * that we are not far enough along the log (because the
484 * actual records are larger than minimum size) we just skip
487 static void llog_skip_over(__u64 *off, int curr, int goal)
491 *off = (*off + (goal - curr - 1) * LLOG_MIN_REC_SIZE) &
492 ~(LLOG_CHUNK_SIZE - 1);
496 * - cur_offset to the furthest point read in the log file
497 * - cur_idx to the log index preceeding cur_offset
498 * returns -EIO/-EINVAL on error
500 static int llog_osd_next_block(const struct lu_env *env,
501 struct llog_handle *loghandle, int *cur_idx,
502 int next_idx, __u64 *cur_offset, void *buf,
505 struct llog_thread_info *lgi = llog_info(env);
507 struct dt_device *dt;
515 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
518 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
519 next_idx, *cur_idx, *cur_offset);
522 LASSERT(loghandle->lgh_ctxt);
524 o = loghandle->lgh_obj;
526 LASSERT(dt_object_exists(o));
527 dt = lu2dt_dev(o->do_lu.lo_dev);
530 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
534 while (*cur_offset < lgi->lgi_attr.la_size) {
535 struct llog_rec_hdr *rec, *last_rec;
536 struct llog_rec_tail *tail;
538 llog_skip_over(cur_offset, *cur_idx, next_idx);
540 /* read up to next LLOG_CHUNK_SIZE block */
541 lgi->lgi_buf.lb_len = LLOG_CHUNK_SIZE -
542 (*cur_offset & (LLOG_CHUNK_SIZE - 1));
543 lgi->lgi_buf.lb_buf = buf;
545 /* Note: read lock is not needed around la_size get above at
546 * the time of dt_attr_get(). There are only two cases that
547 * matter. Either la_size == cur_offset, in which case the
548 * entire read is skipped, or la_size > cur_offset and the loop
549 * is entered and this thread is blocked at dt_read_lock()
550 * until the write is completed. When the write completes, then
551 * the dt_read() will be done with the full length, and will
554 dt_read_lock(env, o, 0);
555 rc = dt_read(env, o, &lgi->lgi_buf, cur_offset);
556 dt_read_unlock(env, o);
558 CERROR("%s: can't read llog block from log "DFID
559 " offset "LPU64": rc = %d\n",
560 o->do_lu.lo_dev->ld_obd->obd_name,
561 PFID(lu_object_fid(&o->do_lu)), *cur_offset,
567 /* signal the end of the valid buffer to
569 memset(buf + rc, 0, len - rc);
572 if (rc == 0) /* end of file, nothing to do */
575 if (rc < sizeof(*tail)) {
576 CERROR("%s: invalid llog block at log id "LPU64"/%u "
578 o->do_lu.lo_dev->ld_obd->obd_name,
579 loghandle->lgh_id.lgl_oid,
580 loghandle->lgh_id.lgl_ogen, *cur_offset);
581 GOTO(out, rc = -EINVAL);
585 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
586 lustre_swab_llog_rec(rec);
588 tail = (struct llog_rec_tail *)((char *)buf + rc -
589 sizeof(struct llog_rec_tail));
590 /* get the last record in block */
591 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
592 le32_to_cpu(tail->lrt_len));
594 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
595 lustre_swab_llog_rec(last_rec);
596 LASSERT(last_rec->lrh_index == tail->lrt_index);
598 *cur_idx = tail->lrt_index;
600 /* this shouldn't happen */
601 if (tail->lrt_index == 0) {
602 CERROR("%s: invalid llog tail at log id "LPU64"/%u "
604 o->do_lu.lo_dev->ld_obd->obd_name,
605 loghandle->lgh_id.lgl_oid,
606 loghandle->lgh_id.lgl_ogen, *cur_offset);
607 GOTO(out, rc = -EINVAL);
609 if (tail->lrt_index < next_idx)
612 /* sanity check that the start of the new buffer is no farther
613 * than the record that we wanted. This shouldn't happen. */
614 if (rec->lrh_index > next_idx) {
615 CERROR("%s: missed desired record? %u > %u\n",
616 o->do_lu.lo_dev->ld_obd->obd_name,
617 rec->lrh_index, next_idx);
618 GOTO(out, rc = -ENOENT);
622 GOTO(out, rc = -EIO);
627 static int llog_osd_prev_block(const struct lu_env *env,
628 struct llog_handle *loghandle,
629 int prev_idx, void *buf, int len)
631 struct llog_thread_info *lgi = llog_info(env);
633 struct dt_device *dt;
639 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
642 CDEBUG(D_OTHER, "looking for log index %u\n", prev_idx);
645 LASSERT(loghandle->lgh_ctxt);
647 o = loghandle->lgh_obj;
649 LASSERT(dt_object_exists(o));
650 dt = lu2dt_dev(o->do_lu.lo_dev);
653 cur_offset = LLOG_CHUNK_SIZE;
654 llog_skip_over(&cur_offset, 0, prev_idx);
656 rc = dt_attr_get(env, o, &lgi->lgi_attr, BYPASS_CAPA);
660 while (cur_offset < lgi->lgi_attr.la_size) {
661 struct llog_rec_hdr *rec, *last_rec;
662 struct llog_rec_tail *tail;
664 lgi->lgi_buf.lb_len = len;
665 lgi->lgi_buf.lb_buf = buf;
666 /* It is OK to have locking around dt_read() only, see
667 * comment in llog_osd_next_block for details
669 dt_read_lock(env, o, 0);
670 rc = dt_read(env, o, &lgi->lgi_buf, &cur_offset);
671 dt_read_unlock(env, o);
673 CERROR("%s: can't read llog block from log "DFID
674 " offset "LPU64": rc = %d\n",
675 o->do_lu.lo_dev->ld_obd->obd_name,
676 PFID(lu_object_fid(&o->do_lu)), cur_offset, rc);
680 if (rc == 0) /* end of file, nothing to do */
683 if (rc < sizeof(*tail)) {
684 CERROR("%s: invalid llog block at log id "LPU64"/%u "
686 o->do_lu.lo_dev->ld_obd->obd_name,
687 loghandle->lgh_id.lgl_oid,
688 loghandle->lgh_id.lgl_ogen, cur_offset);
689 GOTO(out, rc = -EINVAL);
693 if (LLOG_REC_HDR_NEEDS_SWABBING(rec))
694 lustre_swab_llog_rec(rec);
696 tail = (struct llog_rec_tail *)((char *)buf + rc -
697 sizeof(struct llog_rec_tail));
698 /* get the last record in block */
699 last_rec = (struct llog_rec_hdr *)((char *)buf + rc -
700 le32_to_cpu(tail->lrt_len));
702 if (LLOG_REC_HDR_NEEDS_SWABBING(last_rec))
703 lustre_swab_llog_rec(last_rec);
704 LASSERT(last_rec->lrh_index == tail->lrt_index);
706 /* this shouldn't happen */
707 if (tail->lrt_index == 0) {
708 CERROR("%s: invalid llog tail at log id "LPU64"/%u "
710 o->do_lu.lo_dev->ld_obd->obd_name,
711 loghandle->lgh_id.lgl_oid,
712 loghandle->lgh_id.lgl_ogen, cur_offset);
713 GOTO(out, rc = -EINVAL);
715 if (tail->lrt_index < prev_idx)
718 /* sanity check that the start of the new buffer is no farther
719 * than the record that we wanted. This shouldn't happen. */
720 if (rec->lrh_index > prev_idx) {
721 CERROR("%s: missed desired record? %u > %u\n",
722 o->do_lu.lo_dev->ld_obd->obd_name,
723 rec->lrh_index, prev_idx);
724 GOTO(out, rc = -ENOENT);
728 GOTO(out, rc = -EIO);
733 struct dt_object *llog_osd_dir_get(const struct lu_env *env,
734 struct llog_ctxt *ctxt)
736 struct dt_device *dt;
737 struct dt_thread_info *dti = dt_info(env);
738 struct dt_object *dir;
741 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
742 if (ctxt->loc_dir == NULL) {
743 rc = dt_root_get(env, dt, &dti->dti_fid);
746 dir = dt_locate(env, dt, &dti->dti_fid);
748 lu_object_get(&ctxt->loc_dir->do_lu);
755 static int llog_osd_open(const struct lu_env *env, struct llog_handle *handle,
756 struct llog_logid *logid, char *name,
757 enum llog_open_param open_param)
759 struct llog_thread_info *lgi = llog_info(env);
760 struct llog_ctxt *ctxt = handle->lgh_ctxt;
762 struct dt_device *dt;
763 struct ls_device *ls;
764 struct local_oid_storage *los;
771 LASSERT(ctxt->loc_exp);
772 LASSERT(ctxt->loc_exp->exp_obd);
773 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
776 ls = ls_device_get(dt);
780 mutex_lock(&ls->ls_los_mutex);
781 los = dt_los_find(ls, FID_SEQ_LLOG);
782 mutex_unlock(&ls->ls_los_mutex);
784 ls_device_put(env, ls);
789 logid_to_fid(logid, &lgi->lgi_fid);
791 struct dt_object *llog_dir;
793 llog_dir = llog_osd_dir_get(env, ctxt);
794 if (IS_ERR(llog_dir))
795 GOTO(out, rc = PTR_ERR(llog_dir));
796 dt_read_lock(env, llog_dir, 0);
797 rc = dt_lookup_dir(env, llog_dir, name, &lgi->lgi_fid);
798 dt_read_unlock(env, llog_dir);
799 lu_object_put(env, &llog_dir->do_lu);
800 if (rc == -ENOENT && open_param == LLOG_OPEN_NEW) {
801 /* generate fid for new llog */
802 rc = local_object_fid_generate(env, los,
807 OBD_ALLOC(handle->lgh_name, strlen(name) + 1);
808 if (handle->lgh_name)
809 strcpy(handle->lgh_name, name);
811 GOTO(out, rc = -ENOMEM);
813 LASSERTF(open_param & LLOG_OPEN_NEW, "%#x\n", open_param);
814 /* generate fid for new llog */
815 rc = local_object_fid_generate(env, los, &lgi->lgi_fid);
820 o = ls_locate(env, ls, &lgi->lgi_fid);
822 GOTO(out_name, rc = PTR_ERR(o));
824 /* No new llog is expected but doesn't exist */
825 if (open_param != LLOG_OPEN_NEW && !dt_object_exists(o))
826 GOTO(out_put, rc = -ENOENT);
828 fid_to_logid(&lgi->lgi_fid, &handle->lgh_id);
830 handle->private_data = los;
831 LASSERT(handle->lgh_ctxt);
836 lu_object_put(env, &o->do_lu);
838 if (handle->lgh_name != NULL)
839 OBD_FREE(handle->lgh_name, strlen(name) + 1);
845 static int llog_osd_exist(struct llog_handle *handle)
847 LASSERT(handle->lgh_obj);
848 return (dt_object_exists(handle->lgh_obj) &&
849 !lu_object_is_dying(handle->lgh_obj->do_lu.lo_header));
852 static int llog_osd_declare_create(const struct lu_env *env,
853 struct llog_handle *res, struct thandle *th)
855 struct llog_thread_info *lgi = llog_info(env);
856 struct local_oid_storage *los;
862 LASSERT(res->lgh_obj);
865 /* object can be created by another thread */
867 if (dt_object_exists(o))
870 los = res->private_data;
873 rc = llog_osd_declare_new_object(env, los, o, th);
877 rc = dt_declare_record_write(env, o, LLOG_CHUNK_SIZE, 0, th);
882 struct dt_object *llog_dir;
884 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
885 if (IS_ERR(llog_dir))
886 RETURN(PTR_ERR(llog_dir));
887 dt_declare_ref_add(env, o, th);
888 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
889 rc = dt_declare_insert(env, llog_dir,
890 (struct dt_rec *)&lgi->lgi_fid,
891 (struct dt_key *)res->lgh_name, th);
892 lu_object_put(env, &llog_dir->do_lu);
894 CERROR("%s: can't declare named llog %s: rc = %d\n",
895 o->do_lu.lo_dev->ld_obd->obd_name,
901 /* This is a callback from the llog_* functions.
902 * Assumes caller has already pushed us into the kernel context. */
903 static int llog_osd_create(const struct lu_env *env, struct llog_handle *res,
906 struct llog_thread_info *lgi = llog_info(env);
907 struct local_oid_storage *los;
917 /* llog can be already created */
918 if (dt_object_exists(o))
921 los = res->private_data;
924 dt_write_lock(env, o, 0);
925 if (!dt_object_exists(o))
926 rc = llog_osd_create_new_object(env, los, o, th);
930 dt_ref_add(env, o, th);
931 dt_write_unlock(env, o);
936 struct dt_object *llog_dir;
938 llog_dir = llog_osd_dir_get(env, res->lgh_ctxt);
939 if (IS_ERR(llog_dir))
940 RETURN(PTR_ERR(llog_dir));
942 logid_to_fid(&res->lgh_id, &lgi->lgi_fid);
943 dt_read_lock(env, llog_dir, 0);
944 rc = dt_insert(env, llog_dir,
945 (struct dt_rec *)&lgi->lgi_fid,
946 (struct dt_key *)res->lgh_name,
948 dt_read_unlock(env, llog_dir);
949 lu_object_put(env, &llog_dir->do_lu);
951 CERROR("%s: can't create named llog %s: rc = %d\n",
952 o->do_lu.lo_dev->ld_obd->obd_name,
958 static int llog_osd_close(const struct lu_env *env, struct llog_handle *handle)
960 struct local_oid_storage *los;
965 LASSERT(handle->lgh_obj);
967 lu_object_put(env, &handle->lgh_obj->do_lu);
969 los = handle->private_data;
973 if (handle->lgh_name)
974 OBD_FREE(handle->lgh_name, strlen(handle->lgh_name) + 1);
979 static int llog_osd_destroy(const struct lu_env *env,
980 struct llog_handle *loghandle)
982 struct llog_thread_info *lgi = llog_info(env);
983 struct llog_ctxt *ctxt;
984 struct dt_object *o, *llog_dir = NULL;
992 ctxt = loghandle->lgh_ctxt;
995 o = loghandle->lgh_obj;
998 d = lu2dt_dev(o->do_lu.lo_dev);
1000 LASSERT(d == ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt);
1002 th = dt_trans_create(env, d);
1004 RETURN(PTR_ERR(th));
1006 if (loghandle->lgh_name) {
1007 llog_dir = llog_osd_dir_get(env, ctxt);
1008 if (IS_ERR(llog_dir))
1009 GOTO(out_trans, rc = PTR_ERR(llog_dir));
1011 dt_declare_ref_del(env, o, th);
1012 name = loghandle->lgh_name;
1013 rc = dt_declare_delete(env, llog_dir,
1014 (struct dt_key *)name, th);
1016 GOTO(out_trans, rc);
1019 dt_declare_ref_del(env, o, th);
1021 rc = dt_declare_destroy(env, o, th);
1023 GOTO(out_trans, rc);
1025 rc = dt_trans_start_local(env, d, th);
1027 GOTO(out_trans, rc);
1029 dt_write_lock(env, o, 0);
1030 if (dt_object_exists(o)) {
1032 dt_ref_del(env, o, th);
1033 dt_read_lock(env, llog_dir, 0);
1034 rc = dt_delete(env, llog_dir,
1035 (struct dt_key *) name,
1037 dt_read_unlock(env, llog_dir);
1039 CERROR("%s: can't remove llog %s: rc = %d\n",
1040 o->do_lu.lo_dev->ld_obd->obd_name,
1042 GOTO(out_unlock, rc);
1046 * XXX: compatibility bits
1047 * on old filesystems llogs are referenced by the name
1048 * on the new ones they are referenced by OI and by
1051 rc = dt_attr_get(env, o, &lgi->lgi_attr, NULL);
1053 GOTO(out_unlock, rc);
1054 LASSERT(lgi->lgi_attr.la_nlink < 2);
1055 if (lgi->lgi_attr.la_nlink == 1)
1056 dt_ref_del(env, o, th);
1057 rc = dt_destroy(env, o, th);
1059 GOTO(out_unlock, rc);
1062 dt_write_unlock(env, o);
1064 dt_trans_stop(env, d, th);
1065 if (llog_dir != NULL)
1066 lu_object_put(env, &llog_dir->do_lu);
1070 static int llog_osd_setup(const struct lu_env *env, struct obd_device *obd,
1071 struct obd_llog_group *olg, int ctxt_idx,
1072 struct obd_device *disk_obd)
1074 struct local_oid_storage *los;
1075 struct llog_thread_info *lgi = llog_info(env);
1076 struct llog_ctxt *ctxt;
1082 LASSERT(olg->olg_ctxts[ctxt_idx]);
1084 ctxt = llog_ctxt_get(olg->olg_ctxts[ctxt_idx]);
1087 /* initialize data allowing to generate new fids,
1088 * literally we need a sequece */
1089 lgi->lgi_fid.f_seq = FID_SEQ_LLOG;
1090 lgi->lgi_fid.f_oid = 1;
1091 lgi->lgi_fid.f_ver = 0;
1092 rc = local_oid_storage_init(env, disk_obd->obd_lvfs_ctxt.dt,
1093 &lgi->lgi_fid, &los);
1094 llog_ctxt_put(ctxt);
1098 static int llog_osd_cleanup(const struct lu_env *env, struct llog_ctxt *ctxt)
1100 struct dt_device *dt;
1101 struct ls_device *ls;
1102 struct local_oid_storage *los;
1104 LASSERT(ctxt->loc_exp->exp_obd);
1105 dt = ctxt->loc_exp->exp_obd->obd_lvfs_ctxt.dt;
1106 ls = ls_device_get(dt);
1108 RETURN(PTR_ERR(ls));
1110 mutex_lock(&ls->ls_los_mutex);
1111 los = dt_los_find(ls, FID_SEQ_LLOG);
1112 mutex_unlock(&ls->ls_los_mutex);
1115 local_oid_storage_fini(env, los);
1117 ls_device_put(env, ls);
1121 struct llog_operations llog_osd_ops = {
1122 .lop_next_block = llog_osd_next_block,
1123 .lop_prev_block = llog_osd_prev_block,
1124 .lop_read_header = llog_osd_read_header,
1125 .lop_destroy = llog_osd_destroy,
1126 .lop_setup = llog_osd_setup,
1127 .lop_cleanup = llog_osd_cleanup,
1128 .lop_open = llog_osd_open,
1129 .lop_exist = llog_osd_exist,
1130 .lop_declare_create = llog_osd_declare_create,
1131 .lop_create = llog_osd_create,
1132 .lop_declare_write_rec = llog_osd_declare_write_rec,
1133 .lop_write_rec = llog_osd_write_rec,
1134 .lop_close = llog_osd_close,
1136 EXPORT_SYMBOL(llog_osd_ops);
1138 /* reads the catalog list */
1139 int llog_osd_get_cat_list(const struct lu_env *env, struct dt_device *d,
1140 int idx, int count, struct llog_catid *idarray)
1142 struct llog_thread_info *lgi = llog_info(env);
1143 struct dt_object *o = NULL;
1151 size = sizeof(*idarray) * count;
1152 lgi->lgi_off = idx * sizeof(*idarray);
1154 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);
1226 lu_object_put(env, &o->do_lu);
1229 EXPORT_SYMBOL(llog_osd_get_cat_list);
1231 /* writes the cat list */
1232 int llog_osd_put_cat_list(const struct lu_env *env, struct dt_device *d,
1233 int idx, int count, struct llog_catid *idarray)
1235 struct llog_thread_info *lgi = llog_info(env);
1236 struct dt_object *o = NULL;
1245 size = sizeof(*idarray) * count;
1246 lgi->lgi_off = idx * sizeof(*idarray);
1248 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);