1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2001-2003 Cluster File Systems, Inc.
5 * Author: Andreas Dilger <adilger@clusterfs.com>
7 * This file is part of Lustre, http://www.lustre.org.
9 * Lustre is free software; you can redistribute it and/or
10 * modify it under the terms of version 2 of the GNU General Public
11 * License as published by the Free Software Foundation.
13 * Lustre is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Lustre; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 * OST<->MDS recovery logging infrastructure.
24 * Invariants in implementation:
25 * - we do not share logs among different OST<->MDS connections, so that
26 * if an OST or MDS fails it need only look at log(s) relevant to itself
29 #define DEBUG_SUBSYSTEM S_LOG
38 #include <liblustre.h>
41 #include <linux/lvfs.h>
42 #include <linux/lustre_fsfilt.h>
43 #include <linux/lustre_log.h>
47 static int llog_lvfs_pad(struct llog_ctxt *ctxt, struct l_file *file,
50 struct llog_rec_hdr rec;
51 struct llog_rec_tail tail;
55 LASSERT(len >= LLOG_MIN_REC_SIZE && (len & 0x7) == 0);
57 tail.lrt_len = rec.lrh_len = cpu_to_le32(len);
58 tail.lrt_index = rec.lrh_index = cpu_to_le32(index);
61 rc = llog_fsfilt_write_record(ctxt, file, &rec, sizeof(rec),
64 CERROR("error writing padding record: rc %d\n", rc);
68 file->f_pos += len - sizeof(rec) - sizeof(tail);
69 rc = llog_fsfilt_write_record(ctxt, file, &tail, sizeof(tail),
72 CERROR("error writing padding record: rc %d\n", rc);
80 static int llog_lvfs_write_blob(struct llog_ctxt *ctxt, struct l_file *file,
81 struct llog_rec_hdr *rec, void *buf, loff_t off)
84 struct llog_rec_tail end;
85 loff_t saved_off = file->f_pos;
86 int buflen = le32_to_cpu(rec->lrh_len);
92 rc = llog_fsfilt_write_record(ctxt, file, rec, buflen,
95 CERROR("error writing log record: rc %d\n", rc);
102 rec->lrh_len = cpu_to_le32(sizeof(*rec) + buflen + sizeof(end));
103 rc = llog_fsfilt_write_record(ctxt, file, rec, sizeof(*rec),
106 CERROR("error writing log hdr: rc %d\n", rc);
110 rc = llog_fsfilt_write_record(ctxt, file, buf, buflen,
113 CERROR("error writing log buffer: rc %d\n", rc);
117 end.lrt_len = rec->lrh_len;
118 end.lrt_index = rec->lrh_index;
119 rc = llog_fsfilt_write_record(ctxt, file, &end, sizeof(end),
122 CERROR("error writing log tail: rc %d\n", rc);
128 if (saved_off > file->f_pos)
129 file->f_pos = saved_off;
134 static int llog_lvfs_read_blob(struct llog_ctxt *ctxt, struct l_file *file,
135 void *buf, int size, loff_t off)
141 rc = llog_fsfilt_read_record(ctxt, file, buf, size, &offset);
143 CERROR("error reading log record: rc %d\n", rc);
149 static int llog_lvfs_read_header(struct llog_handle *handle)
151 struct llog_ctxt *ctxt = handle->lgh_ctxt;
155 LASSERT(sizeof(*handle->lgh_hdr) == LLOG_CHUNK_SIZE);
156 LASSERT(ctxt != NULL);
158 if (handle->lgh_file->f_dentry->d_inode->i_size == 0) {
159 CDEBUG(D_HA, "not reading header from 0-byte log\n");
163 rc = llog_lvfs_read_blob(ctxt, handle->lgh_file, handle->lgh_hdr,
166 CERROR("error reading log header\n");
168 handle->lgh_last_idx = le32_to_cpu(handle->lgh_hdr->llh_tail.lrt_index);
169 handle->lgh_file->f_pos = handle->lgh_file->f_dentry->d_inode->i_size;
174 /* returns negative in on error; 0 if success && reccookie == 0; 1 otherwise */
175 /* appends if idx == -1, otherwise overwrites record idx. */
176 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
177 struct llog_rec_hdr *rec,
178 struct llog_cookie *reccookie,
182 struct llog_log_hdr *llh;
183 int reclen = le32_to_cpu(rec->lrh_len), index, rc;
184 struct llog_rec_tail *lrt;
185 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
191 llh = loghandle->lgh_hdr;
192 file = loghandle->lgh_file;
194 /* record length should not bigger than LLOG_CHUNK_SIZE */
196 rc = (reclen > LLOG_CHUNK_SIZE - sizeof(struct llog_rec_hdr)
197 - sizeof(struct llog_rec_tail)) ? -E2BIG : 0;
199 rc = (reclen > LLOG_CHUNK_SIZE) ? -E2BIG : 0;
206 /* no header: only allowed to insert record 1 */
207 if (idx > 1 && !file->f_dentry->d_inode->i_size) {
208 CERROR("idx != -1 in empty log\n");
212 if (idx && llh->llh_size && llh->llh_size != reclen)
215 rc = llog_lvfs_write_blob(ctxt, file, &llh->llh_hdr, NULL, 0);
216 /* we are done if we only write the header or on error */
220 saved_offset = sizeof(*llh) + (idx-1)*le32_to_cpu(rec->lrh_len);
221 rc = llog_lvfs_write_blob(ctxt, file, rec, buf, saved_offset);
222 if (rc == 0 && reccookie) {
223 reccookie->lgc_lgl = loghandle->lgh_id;
224 reccookie->lgc_index = idx;
230 /* Make sure that records don't cross a chunk boundary, so we can
231 * process them page-at-a-time if needed. If it will cross a chunk
232 * boundary, write in a fake (but referenced) entry to pad the chunk.
234 * We know that llog_current_log() will return a loghandle that is
235 * big enough to hold reclen, so all we care about is padding here.
237 left = LLOG_CHUNK_SIZE - (file->f_pos & (LLOG_CHUNK_SIZE - 1));
239 reclen = sizeof(*rec) + le32_to_cpu(rec->lrh_len) +
240 sizeof(struct llog_rec_tail);
242 /* NOTE: padding is a record, but no bit is set */
243 if (left != 0 && left != reclen &&
244 left < (reclen + LLOG_MIN_REC_SIZE)) {
245 loghandle->lgh_last_idx++;
246 rc = llog_lvfs_pad(ctxt, file, left, loghandle->lgh_last_idx);
249 /* if it's the last idx in log file, then return -ENOSPC */
250 if (loghandle->lgh_last_idx == LLOG_BITMAP_SIZE(llh) - 1)
254 loghandle->lgh_last_idx++;
255 index = loghandle->lgh_last_idx;
256 LASSERT(index < LLOG_BITMAP_SIZE(llh));
257 rec->lrh_index = cpu_to_le32(index);
259 lrt = (void *)rec + le32_to_cpu(rec->lrh_len) - sizeof(*lrt);
260 lrt->lrt_len = rec->lrh_len;
261 lrt->lrt_index = rec->lrh_index;
263 if (ext2_set_bit(index, llh->llh_bitmap)) {
264 CERROR("argh, index %u already set in log bitmap?\n", index);
265 LBUG(); /* should never happen */
267 llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) + 1);
268 llh->llh_tail.lrt_index = cpu_to_le32(index);
271 rc = llog_lvfs_write_blob(ctxt, file, &llh->llh_hdr, NULL, 0);
275 CDEBUG(D_HA, "adding record "LPX64": idx: %u, %u bytes off: %lld\n",
276 loghandle->lgh_id.lgl_oid, index, le32_to_cpu(rec->lrh_len),
279 rc = llog_lvfs_write_blob(ctxt, file, rec, buf, file->f_pos);
283 if (rc == 0 && reccookie) {
284 if (llog_cookie_get_flags(reccookie) & LLOG_COOKIE_REPLAY) {
285 LASSERTF(EQ_LOGID(reccookie->lgc_lgl,loghandle->lgh_id),
286 "lgc_lgl.oid/gr "LPU64"/"LPU64" lgh_id.oid/gr"
288 reccookie->lgc_lgl.lgl_oid,
289 reccookie->lgc_lgl.lgl_ogr,
290 loghandle->lgh_id.lgl_oid,
291 loghandle->lgh_id.lgl_oid);
292 LASSERTF(reccookie->lgc_index == index,
293 "lgc_index %u != index %u\n",
294 reccookie->lgc_index, index);
296 reccookie->lgc_lgl = loghandle->lgh_id;
297 reccookie->lgc_index = index;
298 llog_cookie_add_flags(reccookie, LLOG_COOKIE_REPLAY);
301 if (le32_to_cpu(rec->lrh_type) == MDS_UNLINK_REC)
302 reccookie->lgc_subsys = LLOG_UNLINK_ORIG_CTXT;
303 else if (le32_to_cpu(rec->lrh_type) == OST_SZ_REC)
304 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
305 else if (le32_to_cpu(rec->lrh_type) == OST_RAID1_REC)
306 reccookie->lgc_subsys = LLOG_RD1_ORIG_CTXT;
308 reccookie->lgc_subsys = -1;
311 if (rc == 0 && (le32_to_cpu(rec->lrh_type) == LLOG_GEN_REC ||
312 le32_to_cpu(rec->lrh_type) == SMFS_UPDATE_REC))
318 /* We can skip reading at least as many log blocks as the number of
319 * minimum sized log records we are skipping. If it turns out
320 * that we are not far enough along the log (because the
321 * actual records are larger than minimum size) we just skip
322 * some more records. */
324 static void llog_skip_over(__u64 *off, int curr, int goal)
328 *off = (*off + (goal-curr-1) * LLOG_MIN_REC_SIZE) &
329 ~(LLOG_CHUNK_SIZE - 1);
333 * - curr_offset to the furthest point read in the log file
334 * - curr_idx to the log index preceeding curr_offset
335 * returns -EIO/-EINVAL on error
337 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *curr_idx,
338 int next_idx, __u64 *curr_offset, void *buf,
341 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
344 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
347 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
348 next_idx, *curr_idx, *curr_offset);
350 while (*curr_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
351 struct llog_rec_hdr *rec;
352 struct llog_rec_tail *tail;
356 llog_skip_over(curr_offset, *curr_idx, next_idx);
359 rc = llog_fsfilt_read_record(ctxt, loghandle->lgh_file,
363 CERROR("Cant read llog block at log id "LPU64
364 "/%u offset "LPU64"\n",
365 loghandle->lgh_id.lgl_oid,
366 loghandle->lgh_id.lgl_ogen,
371 nbytes = ppos - *curr_offset;
374 if (nbytes == 0) /* end of file, nothing to do */
377 if (nbytes < sizeof(*tail)) {
378 CERROR("Invalid llog block at log id "LPU64"/%u offset "
379 LPU64"\n", loghandle->lgh_id.lgl_oid,
380 loghandle->lgh_id.lgl_ogen, *curr_offset);
384 tail = buf + nbytes - sizeof(struct llog_rec_tail);
385 *curr_idx = le32_to_cpu(tail->lrt_index);
387 /* this shouldn't happen */
388 if (tail->lrt_index == 0) {
389 CERROR("Invalid llog tail at log id "LPU64"/%u offset "
390 LPU64"\n", loghandle->lgh_id.lgl_oid,
391 loghandle->lgh_id.lgl_ogen, *curr_offset);
394 if (le32_to_cpu(tail->lrt_index) < next_idx)
397 /* sanity check that the start of the new buffer is no farther
398 * than the record that we wanted. This shouldn't happen. */
400 if (le32_to_cpu(rec->lrh_index) > next_idx) {
401 CERROR("missed desired record? %u > %u\n",
402 le32_to_cpu(rec->lrh_index), next_idx);
410 static int llog_lvfs_prev_block(struct llog_handle *loghandle,
411 int prev_idx, void *buf, int len)
413 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
418 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
421 CDEBUG(D_OTHER, "looking for log index %u n", prev_idx);
423 curr_offset = LLOG_CHUNK_SIZE;
424 llog_skip_over(&curr_offset, 0, prev_idx);
426 while (curr_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
427 struct llog_rec_hdr *rec;
428 struct llog_rec_tail *tail;
432 rc = llog_fsfilt_read_record(ctxt, loghandle->lgh_file,
436 CERROR("Cant read llog block at log id "LPU64
437 "/%u offset "LPU64"\n",
438 loghandle->lgh_id.lgl_oid,
439 loghandle->lgh_id.lgl_ogen,
444 /* put number of bytes read into rc to make code simpler */
445 rc = ppos - curr_offset;
448 if (rc == 0) /* end of file, nothing to do */
451 if (rc < sizeof(*tail)) {
452 CERROR("Invalid llog block at log id "LPU64"/%u offset "
453 LPU64"\n", loghandle->lgh_id.lgl_oid,
454 loghandle->lgh_id.lgl_ogen, curr_offset);
458 tail = buf + rc - sizeof(struct llog_rec_tail);
460 /* this shouldn't happen */
461 if (tail->lrt_index == 0) {
462 CERROR("Invalid llog tail at log id "LPU64"/%u offset "
463 LPU64"\n", loghandle->lgh_id.lgl_oid,
464 loghandle->lgh_id.lgl_ogen, curr_offset);
467 if (le32_to_cpu(tail->lrt_index) < prev_idx)
470 /* sanity check that the start of the new buffer is no farther
471 * than the record that we wanted. This shouldn't happen. */
473 if (le32_to_cpu(rec->lrh_index) > prev_idx) {
474 CERROR("missed desired record? %u > %u\n",
475 le32_to_cpu(rec->lrh_index), prev_idx);
483 static struct file *llog_filp_open(char *name, int flags, int mode)
489 OBD_ALLOC(logname, PATH_MAX);
491 return ERR_PTR(-ENOMEM);
493 len = snprintf(logname, PATH_MAX, "LOGS/%s", name);
494 if (len >= PATH_MAX - 1) {
495 filp = ERR_PTR(-ENAMETOOLONG);
497 filp = l_filp_open(logname, flags, mode);
499 CERROR("logfile %s(%s): %ld\n",
500 flags & O_CREAT ? "create" : "open", logname,
505 OBD_FREE(logname, PATH_MAX);
509 /* creates object for the case when we have no obd (smfs). */
511 llog_object_create_alone(struct llog_ctxt *ctxt, struct llog_logid *lgh_id)
517 LASSERT(lgh_id != NULL);
518 if (lgh_id->lgl_oid) {
519 struct dentry *dchild;
520 char fidname[LL_FID_NAMELEN];
523 down(&ctxt->loc_objects_dir->d_inode->i_sem);
524 fidlen = ll_fid2str(fidname, lgh_id->lgl_oid, lgh_id->lgl_ogen);
525 dchild = lookup_one_len(fidname, ctxt->loc_objects_dir, fidlen);
526 if (IS_ERR(dchild)) {
527 up(&ctxt->loc_objects_dir->d_inode->i_sem);
528 RETURN((struct file *)dchild);
530 if (dchild->d_inode == NULL) {
531 struct dentry_params dp;
534 dchild->d_fsdata = (void *) &dp;
536 dp.p_inum = lgh_id->lgl_oid;
537 rc = ll_vfs_create(ctxt->loc_objects_dir->d_inode,
538 dchild, S_IFREG, NULL);
539 if (dchild->d_fsdata == (void *)(unsigned long)lgh_id->lgl_oid)
540 dchild->d_fsdata = NULL;
542 CDEBUG(D_INODE, "err during create: %d\n", rc);
544 up(&ctxt->loc_objects_dir->d_inode->i_sem);
547 inode = dchild->d_inode;
548 LASSERT(inode->i_ino == lgh_id->lgl_oid);
549 inode->i_generation = lgh_id->lgl_ogen;
550 CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
551 inode->i_ino, inode->i_generation);
552 mark_inode_dirty(inode);
555 mntget(ctxt->loc_lvfs_ctxt->pwdmnt);
556 filp = dentry_open(dchild, ctxt->loc_lvfs_ctxt->pwdmnt,
557 O_RDWR | O_LARGEFILE);
560 up(&ctxt->loc_objects_dir->d_inode->i_sem);
563 if (!S_ISREG(filp->f_dentry->d_inode->i_mode)) {
564 CERROR("%s is not a regular file!: mode = %o\n", fidname,
565 filp->f_dentry->d_inode->i_mode);
567 up(&ctxt->loc_objects_dir->d_inode->i_sem);
568 RETURN(ERR_PTR(-ENOENT));
571 up(&ctxt->loc_objects_dir->d_inode->i_sem);
575 unsigned int tmpname = ll_insecure_random_int();
576 char fidname[LL_FID_NAMELEN];
577 struct dentry *new_child, *parent;
581 sprintf(fidname, "OBJECTS/%u", tmpname);
582 filp = filp_open(fidname, O_CREAT | O_EXCL, 0644);
586 CERROR("impossible object name collision %u\n",
590 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
594 namelen = ll_fid2str(fidname, filp->f_dentry->d_inode->i_ino,
595 filp->f_dentry->d_inode->i_generation);
596 parent = filp->f_dentry->d_parent;
597 down(&parent->d_inode->i_sem);
598 new_child = lookup_one_len(fidname, parent, namelen);
599 if (IS_ERR(new_child)) {
600 CERROR("getting neg dentry for obj rename: %d\n", rc);
601 GOTO(out_close, rc = PTR_ERR(new_child));
603 if (new_child->d_inode != NULL) {
604 CERROR("impossible non-negative obj dentry %lu:%u!\n",
605 filp->f_dentry->d_inode->i_ino,
606 filp->f_dentry->d_inode->i_generation);
610 handle = llog_fsfilt_start(ctxt, parent->d_inode, FSFILT_OP_RENAME, NULL);
612 GOTO(out_dput, rc = PTR_ERR(handle));
615 rc = vfs_rename(parent->d_inode, filp->f_dentry,
616 parent->d_inode, new_child);
619 CERROR("error renaming new object %lu:%u: rc %d\n",
620 filp->f_dentry->d_inode->i_ino,
621 filp->f_dentry->d_inode->i_generation, rc);
623 err = llog_fsfilt_commit(ctxt, parent->d_inode, handle, 0);
630 up(&parent->d_inode->i_sem);
635 /* FIXME: is this group 1 is correct? */
637 lgh_id->lgl_oid = filp->f_dentry->d_inode->i_ino;
638 lgh_id->lgl_ogen = filp->f_dentry->d_inode->i_generation;
644 /* creates object for generic case (obd exists) */
646 llog_object_create_generic(struct llog_ctxt *ctxt, struct llog_logid *lgh_id)
648 struct file *filp = NULL;
649 struct dentry *dchild;
650 struct obd_device *obd;
651 struct obdo *oa = NULL;
652 int open_flags = O_RDWR | O_LARGEFILE;
656 obd = ctxt->loc_exp->exp_obd;
657 LASSERT(obd != NULL);
659 if (lgh_id->lgl_oid) {
660 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, lgh_id->lgl_oid,
661 lgh_id->lgl_ogen, lgh_id->lgl_ogr);
662 if (IS_ERR(dchild) == -ENOENT) {
663 OBD_ALLOC(oa, sizeof(*oa));
665 RETURN(ERR_PTR(-ENOMEM));
667 oa->o_id = lgh_id->lgl_oid;
668 oa->o_generation = lgh_id->lgl_ogen;
669 oa->o_gr = lgh_id->lgl_ogr;
670 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
671 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
673 CDEBUG(D_INODE, "err during create: %d\n", rc);
674 GOTO(out_free_oa, rc);
676 CDEBUG(D_HA, "re-create log object "LPX64":0x%x:"LPX64"\n",
677 lgh_id->lgl_oid, lgh_id->lgl_ogen, lgh_id->lgl_ogr);
679 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, lgh_id->lgl_oid,
680 lgh_id->lgl_ogen, lgh_id->lgl_ogr);
681 } else if (IS_ERR(dchild)) {
682 CERROR("error looking up logfile "LPX64":0x%x: rc %d\n",
683 lgh_id->lgl_oid, lgh_id->lgl_ogen, rc);
684 RETURN((struct file *)dchild);
687 filp = l_dentry_open(&obd->obd_lvfs_ctxt, dchild, open_flags);
691 CERROR("error opening logfile "LPX64"0x%x: rc %d\n",
692 lgh_id->lgl_oid, lgh_id->lgl_ogen, rc);
694 GOTO(out_free_oa, rc);
696 /* this is important to work here over obd_create() as it manages
697 groups and we need it. Yet another reason is that mds_obd_create()
698 is fully the same as old version of this function and this helps
699 us to avoid code duplicating and layering violating. */
700 OBD_ALLOC(oa, sizeof(*oa));
702 RETURN(ERR_PTR(-ENOMEM));
704 oa->o_gr = FILTER_GROUP_LLOG;
705 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
706 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
708 GOTO(out_free_oa, rc);
710 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, oa->o_id,
711 oa->o_generation, oa->o_gr);
713 GOTO(out_free_oa, rc = PTR_ERR(dchild));
715 filp = l_dentry_open(&obd->obd_lvfs_ctxt, dchild,
719 GOTO(out_free_oa, rc = PTR_ERR(filp));
722 /* group 1 is not longer valid, we use the group which is set
723 by obd_create()->mds_obd_create(). */
724 lgh_id->lgl_ogr = oa->o_gr;
725 lgh_id->lgl_oid = oa->o_id;
726 lgh_id->lgl_ogen = oa->o_generation;
733 OBD_FREE(oa, sizeof(*oa));
738 llog_object_create(struct llog_ctxt *ctxt, struct llog_logid *lgh_id)
741 return llog_object_create_alone(ctxt, lgh_id);
743 return llog_object_create_generic(ctxt, lgh_id);
746 static int llog_add_link_object(struct llog_ctxt *ctxt, struct llog_logid logid,
747 struct dentry *dentry)
749 struct dentry *new_child;
750 char fidname[LL_FID_NAMELEN];
752 int namelen, rc = 0, err;
755 namelen = ll_fid2str(fidname, logid.lgl_oid, logid.lgl_ogen);
756 down(&ctxt->loc_objects_dir->d_inode->i_sem);
757 new_child = lookup_one_len(fidname, ctxt->loc_objects_dir, namelen);
758 if (IS_ERR(new_child)) {
759 CERROR("getting neg dentry for obj rename: %d\n", rc);
760 GOTO(out, rc = PTR_ERR(new_child));
762 if (new_child->d_inode == dentry->d_inode)
764 if (new_child->d_inode != NULL) {
765 CERROR("impossible non-negative obj dentry "LPX64":%u!\n",
766 logid.lgl_oid, logid.lgl_ogen);
769 handle = llog_fsfilt_start(ctxt, ctxt->loc_objects_dir->d_inode,
770 FSFILT_OP_LINK, NULL);
772 GOTO(out_dput, rc = PTR_ERR(handle));
775 rc = vfs_link(dentry, ctxt->loc_objects_dir->d_inode, new_child);
778 CERROR("error link new object "LPX64":%08x: rc %d\n",
779 logid.lgl_oid, logid.lgl_ogen, rc);
780 /* it doesn't make much sense to get -EEXIST here */
781 LASSERTF(rc != -EEXIST, "bug 3490: dentry: %p "
782 "dir->d_ionode %p new_child: %p \n",
783 dentry, ctxt->loc_objects_dir->d_inode, new_child);
785 err = llog_fsfilt_commit(ctxt, ctxt->loc_objects_dir->d_inode, handle, 0);
789 up(&ctxt->loc_objects_dir->d_inode->i_sem);
793 static int llog_lvfs_open(struct llog_ctxt *ctxt, struct llog_handle **res,
794 struct llog_logid *logid, char *name, int flags)
796 struct llog_handle *handle;
797 struct lvfs_run_ctxt saved;
799 int open_flags = O_RDWR | O_LARGEFILE;
802 if (flags & OBD_LLOG_FL_CREATE)
803 open_flags |= O_CREAT;
805 handle = llog_alloc_handle();
811 if (ctxt->loc_lvfs_ctxt)
812 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
815 handle->lgh_file = llog_object_create(ctxt, logid);
816 if (IS_ERR(handle->lgh_file)) {
817 CERROR("cannot create/open llog object "LPX64":%x "
818 "error = %ld", logid->lgl_oid, logid->lgl_ogen,
819 PTR_ERR(handle->lgh_file));
820 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
822 handle->lgh_id = *logid;
825 handle->lgh_file = llog_filp_open(name, open_flags, 0644);
826 if (IS_ERR(handle->lgh_file)) {
827 CERROR("cannot open %s file, error = %ld\n",
828 name, PTR_ERR(handle->lgh_file));
829 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
831 LASSERT(handle->lgh_file->f_dentry->d_parent == ctxt->loc_logs_dir);
833 handle->lgh_id.lgl_ogr = 1;
834 handle->lgh_id.lgl_oid = handle->lgh_file->f_dentry->d_inode->i_ino;
835 handle->lgh_id.lgl_ogen = handle->lgh_file->f_dentry->d_inode->i_generation;
836 rc = llog_add_link_object(ctxt, handle->lgh_id, handle->lgh_file->f_dentry);
841 handle->lgh_file = llog_object_create(ctxt, &handle->lgh_id);
842 if (IS_ERR(handle->lgh_file)) {
843 CERROR("cannot create llog object, error = %ld\n",
844 PTR_ERR(handle->lgh_file));
845 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
849 handle->lgh_ctxt = ctxt;
851 if (ctxt->loc_lvfs_ctxt)
852 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
855 llog_free_handle(handle);
859 static int llog_lvfs_close(struct llog_handle *handle)
864 rc = filp_close(handle->lgh_file, 0);
866 CERROR("error closing log: rc %d\n", rc);
870 static int llog_lvfs_destroy(struct llog_handle *loghandle)
872 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
873 struct lvfs_run_ctxt saved;
874 struct dentry *fdentry;
875 struct inode *parent_inode;
876 char fidname[LL_FID_NAMELEN];
878 int rc = -EINVAL, err, namelen;
881 if (ctxt->loc_lvfs_ctxt)
882 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
884 fdentry = loghandle->lgh_file->f_dentry;
885 parent_inode = fdentry->d_parent->d_inode;
887 if (!strcmp(fdentry->d_parent->d_name.name, "LOGS")) {
888 LASSERT(parent_inode == ctxt->loc_logs_dir->d_inode);
890 namelen = ll_fid2str(fidname, fdentry->d_inode->i_ino,
891 fdentry->d_inode->i_generation);
893 rc = llog_lvfs_close(loghandle);
899 handle = llog_fsfilt_start(ctxt, parent_inode,
900 FSFILT_OP_UNLINK, NULL);
901 if (IS_ERR(handle)) {
903 GOTO(out, rc = PTR_ERR(handle));
906 down(&parent_inode->i_sem);
907 rc = vfs_unlink(parent_inode, fdentry);
908 up(&parent_inode->i_sem);
912 down(&ctxt->loc_objects_dir->d_inode->i_sem);
913 fdentry = lookup_one_len(fidname, ctxt->loc_objects_dir,
915 if (fdentry == NULL || fdentry->d_inode == NULL) {
916 CERROR("destroy non_existent object %s\n", fidname);
917 GOTO(out_err, rc = IS_ERR(fdentry) ?
918 PTR_ERR(fdentry) : -ENOENT);
920 rc = vfs_unlink(ctxt->loc_objects_dir->d_inode, fdentry);
923 up(&ctxt->loc_objects_dir->d_inode->i_sem);
925 err = llog_fsfilt_commit(ctxt, parent_inode, handle, 0);
931 if (ctxt->loc_alone) {
932 if (!strcmp(fdentry->d_parent->d_name.name, "OBJECTS")) {
933 LASSERT(parent_inode == ctxt->loc_objects_dir->d_inode);
936 rc = llog_lvfs_close(loghandle);
938 down(&parent_inode->i_sem);
939 rc = vfs_unlink(parent_inode, fdentry);
940 up(&parent_inode->i_sem);
945 struct obdo *oa = NULL;
947 OBD_ALLOC(oa, sizeof(*oa));
949 GOTO(out, rc = -ENOMEM);
951 oa->o_id = loghandle->lgh_id.lgl_oid;
952 oa->o_gr = loghandle->lgh_id.lgl_ogr;
953 oa->o_generation = loghandle->lgh_id.lgl_ogen;
954 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLGENER;
956 rc = llog_lvfs_close(loghandle);
958 GOTO(out_free_oa, rc);
960 rc = obd_destroy(loghandle->lgh_ctxt->loc_exp, oa, NULL, NULL);
962 OBD_FREE(oa, sizeof(*oa));
965 if (ctxt->loc_lvfs_ctxt)
966 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
970 /* reads the catalog list */
971 int llog_get_cat_list(struct lvfs_run_ctxt *ctxt,
972 struct fsfilt_operations *fsops, const char *name,
973 int count, struct llog_catid *idarray)
975 struct lvfs_run_ctxt saved;
977 int size = sizeof(*idarray) * count;
984 push_ctxt(&saved, ctxt, NULL);
985 file = l_filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
986 if (!file || IS_ERR(file)) {
988 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
993 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
994 CERROR("%s is not a regular file!: mode = %o\n", name,
995 file->f_dentry->d_inode->i_mode);
996 GOTO(out, rc = -ENOENT);
999 rc = fsops->fs_read_record(file, idarray, size, &off);
1001 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
1007 if (file && !IS_ERR(file))
1008 rc = filp_close(file, 0);
1010 pop_ctxt(&saved, ctxt, NULL);
1013 EXPORT_SYMBOL(llog_get_cat_list);
1015 /* writes the cat list */
1016 int llog_put_cat_list(struct lvfs_run_ctxt *ctxt,
1017 struct fsfilt_operations *fsops, const char *name,
1018 int count, struct llog_catid *idarray)
1020 struct lvfs_run_ctxt saved;
1021 struct l_file *file;
1022 int size = sizeof(*idarray) * count;
1029 push_ctxt(&saved, ctxt, NULL);
1030 file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
1031 if (!file || IS_ERR(file)) {
1033 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
1038 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1039 CERROR("%s is not a regular file!: mode = %o\n", name,
1040 file->f_dentry->d_inode->i_mode);
1041 GOTO(out, rc = -ENOENT);
1044 rc = fsops->fs_write_record(file, idarray, size, &off, 1);
1046 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
1052 if (file && !IS_ERR(file))
1053 rc = filp_close(file, 0);
1055 pop_ctxt(&saved, ctxt, NULL);
1058 EXPORT_SYMBOL(llog_put_cat_list);
1060 struct llog_operations llog_lvfs_ops = {
1061 lop_open: llog_lvfs_open,
1062 lop_destroy: llog_lvfs_destroy,
1063 lop_close: llog_lvfs_close,
1064 lop_read_header: llog_lvfs_read_header,
1065 lop_write_rec: llog_lvfs_write_rec,
1066 lop_next_block: llog_lvfs_next_block,
1067 lop_prev_block: llog_lvfs_prev_block,
1069 EXPORT_SYMBOL(llog_lvfs_ops);
1071 #else /* !__KERNEL__ */
1073 static int llog_lvfs_read_header(struct llog_handle *handle)
1079 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
1080 struct llog_rec_hdr *rec,
1081 struct llog_cookie *reccookie, int cookiecount,
1088 static int llog_lvfs_open(struct llog_ctxt *ctxt, struct llog_handle **res,
1089 struct llog_logid *logid, char *name, int flags)
1095 static int llog_lvfs_close(struct llog_handle *handle)
1101 static int llog_lvfs_destroy(struct llog_handle *handle)
1107 int llog_get_cat_list(struct lvfs_run_ctxt *ctxt,
1108 struct fsfilt_operations *fsops, const char *name,
1109 int count, struct llog_catid *idarray)
1115 int llog_put_cat_list(struct lvfs_run_ctxt *ctxt,
1116 struct fsfilt_operations *fsops, const char *name,
1117 int count, struct llog_catid *idarray)
1123 int llog_lvfs_prev_block(struct llog_handle *loghandle,
1124 int prev_idx, void *buf, int len)
1130 int llog_lvfs_next_block(struct llog_handle *loghandle, int *curr_idx,
1131 int next_idx, __u64 *offset, void *buf, int len)
1137 struct llog_operations llog_lvfs_ops = {
1138 lop_open: llog_lvfs_open,
1139 lop_destroy: llog_lvfs_destroy,
1140 lop_close: llog_lvfs_close,
1141 lop_read_header: llog_lvfs_read_header,
1142 lop_write_rec: llog_lvfs_write_rec,
1143 lop_next_block: llog_lvfs_next_block,
1144 lop_prev_block: llog_lvfs_prev_block,