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);
251 loghandle->lgh_last_idx++;
252 index = loghandle->lgh_last_idx;
253 rec->lrh_index = cpu_to_le32(index);
255 lrt = (void *)rec + le32_to_cpu(rec->lrh_len) - sizeof(*lrt);
256 lrt->lrt_len = rec->lrh_len;
257 lrt->lrt_index = rec->lrh_index;
259 if (ext2_set_bit(index, llh->llh_bitmap)) {
260 CERROR("argh, index %u already set in log bitmap?\n", index);
261 LBUG(); /* should never happen */
263 llh->llh_count = cpu_to_le32(le32_to_cpu(llh->llh_count) + 1);
264 llh->llh_tail.lrt_index = cpu_to_le32(index);
267 rc = llog_lvfs_write_blob(ctxt, file, &llh->llh_hdr, NULL, 0);
271 CDEBUG(D_HA, "adding record "LPX64": idx: %u, %u bytes off: %lld\n",
272 loghandle->lgh_id.lgl_oid, index, le32_to_cpu(rec->lrh_len),
275 rc = llog_lvfs_write_blob(ctxt, file, rec, buf, file->f_pos);
279 if (rc == 0 && reccookie) {
280 if (llog_cookie_get_flags(reccookie) & LLOG_COOKIE_REPLAY) {
281 LASSERT(EQ_LOGID(reccookie->lgc_lgl, loghandle->lgh_id));
282 LASSERT(reccookie->lgc_index == index);
284 reccookie->lgc_lgl = loghandle->lgh_id;
285 reccookie->lgc_index = index;
286 llog_cookie_add_flags(reccookie, LLOG_COOKIE_REPLAY);
289 if (le32_to_cpu(rec->lrh_type) == MDS_UNLINK_REC)
290 reccookie->lgc_subsys = LLOG_UNLINK_ORIG_CTXT;
291 else if (le32_to_cpu(rec->lrh_type) == OST_SZ_REC)
292 reccookie->lgc_subsys = LLOG_SIZE_ORIG_CTXT;
293 else if (le32_to_cpu(rec->lrh_type) == OST_RAID1_REC)
294 reccookie->lgc_subsys = LLOG_RD1_ORIG_CTXT;
296 reccookie->lgc_subsys = -1;
299 if (rc == 0 && (le32_to_cpu(rec->lrh_type) == LLOG_GEN_REC ||
300 le32_to_cpu(rec->lrh_type) == SMFS_UPDATE_REC))
306 /* We can skip reading at least as many log blocks as the number of
307 * minimum sized log records we are skipping. If it turns out
308 * that we are not far enough along the log (because the
309 * actual records are larger than minimum size) we just skip
310 * some more records. */
312 static void llog_skip_over(__u64 *off, int curr, int goal)
316 *off = (*off + (goal-curr-1) * LLOG_MIN_REC_SIZE) &
317 ~(LLOG_CHUNK_SIZE - 1);
321 * - curr_offset to the furthest point read in the log file
322 * - curr_idx to the log index preceeding curr_offset
323 * returns -EIO/-EINVAL on error
325 static int llog_lvfs_next_block(struct llog_handle *loghandle, int *curr_idx,
326 int next_idx, __u64 *curr_offset, void *buf,
329 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
332 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
335 CDEBUG(D_OTHER, "looking for log index %u (cur idx %u off "LPU64")\n",
336 next_idx, *curr_idx, *curr_offset);
338 while (*curr_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
339 struct llog_rec_hdr *rec;
340 struct llog_rec_tail *tail;
344 llog_skip_over(curr_offset, *curr_idx, next_idx);
347 rc = llog_fsfilt_read_record(ctxt, loghandle->lgh_file,
351 CERROR("Cant read llog block at log id "LPU64
352 "/%u offset "LPU64"\n",
353 loghandle->lgh_id.lgl_oid,
354 loghandle->lgh_id.lgl_ogen,
359 nbytes = ppos - *curr_offset;
362 if (nbytes == 0) /* end of file, nothing to do */
365 if (nbytes < sizeof(*tail)) {
366 CERROR("Invalid llog block at log id "LPU64"/%u offset "
367 LPU64"\n", loghandle->lgh_id.lgl_oid,
368 loghandle->lgh_id.lgl_ogen, *curr_offset);
372 tail = buf + nbytes - sizeof(struct llog_rec_tail);
373 *curr_idx = le32_to_cpu(tail->lrt_index);
375 /* this shouldn't happen */
376 if (tail->lrt_index == 0) {
377 CERROR("Invalid llog tail at log id "LPU64"/%u offset "
378 LPU64"\n", loghandle->lgh_id.lgl_oid,
379 loghandle->lgh_id.lgl_ogen, *curr_offset);
382 if (le32_to_cpu(tail->lrt_index) < next_idx)
385 /* sanity check that the start of the new buffer is no farther
386 * than the record that we wanted. This shouldn't happen. */
388 if (le32_to_cpu(rec->lrh_index) > next_idx) {
389 CERROR("missed desired record? %u > %u\n",
390 le32_to_cpu(rec->lrh_index), next_idx);
398 static int llog_lvfs_prev_block(struct llog_handle *loghandle,
399 int prev_idx, void *buf, int len)
401 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
406 if (len == 0 || len & (LLOG_CHUNK_SIZE - 1))
409 CDEBUG(D_OTHER, "looking for log index %u n", prev_idx);
411 curr_offset = LLOG_CHUNK_SIZE;
412 llog_skip_over(&curr_offset, 0, prev_idx);
414 while (curr_offset < loghandle->lgh_file->f_dentry->d_inode->i_size) {
415 struct llog_rec_hdr *rec;
416 struct llog_rec_tail *tail;
420 rc = llog_fsfilt_read_record(ctxt, loghandle->lgh_file,
424 CERROR("Cant read llog block at log id "LPU64
425 "/%u offset "LPU64"\n",
426 loghandle->lgh_id.lgl_oid,
427 loghandle->lgh_id.lgl_ogen,
432 /* put number of bytes read into rc to make code simpler */
433 rc = ppos - curr_offset;
436 if (rc == 0) /* end of file, nothing to do */
439 if (rc < sizeof(*tail)) {
440 CERROR("Invalid llog block at log id "LPU64"/%u offset "
441 LPU64"\n", loghandle->lgh_id.lgl_oid,
442 loghandle->lgh_id.lgl_ogen, curr_offset);
446 tail = buf + rc - sizeof(struct llog_rec_tail);
448 /* this shouldn't happen */
449 if (tail->lrt_index == 0) {
450 CERROR("Invalid llog tail at log id "LPU64"/%u offset "
451 LPU64"\n", loghandle->lgh_id.lgl_oid,
452 loghandle->lgh_id.lgl_ogen, curr_offset);
455 if (le32_to_cpu(tail->lrt_index) < prev_idx)
458 /* sanity check that the start of the new buffer is no farther
459 * than the record that we wanted. This shouldn't happen. */
461 if (le32_to_cpu(rec->lrh_index) > prev_idx) {
462 CERROR("missed desired record? %u > %u\n",
463 le32_to_cpu(rec->lrh_index), prev_idx);
471 static struct file *llog_filp_open(char *name, int flags, int mode)
477 OBD_ALLOC(logname, PATH_MAX);
479 return ERR_PTR(-ENOMEM);
481 len = snprintf(logname, PATH_MAX, "LOGS/%s", name);
482 if (len >= PATH_MAX - 1) {
483 filp = ERR_PTR(-ENAMETOOLONG);
485 filp = l_filp_open(logname, flags, mode);
487 CERROR("logfile %s(%s): %ld\n",
488 flags & O_CREAT ? "create" : "open", logname,
493 OBD_FREE(logname, PATH_MAX);
497 /* creates object for the case when we have no obd (smfs). */
499 llog_object_create_alone(struct llog_ctxt *ctxt, struct llog_logid *lgh_id)
505 LASSERT(lgh_id != NULL);
506 if (lgh_id->lgl_oid) {
507 struct dentry *dchild;
508 char fidname[LL_FID_NAMELEN];
511 down(&ctxt->loc_objects_dir->d_inode->i_sem);
512 fidlen = ll_fid2str(fidname, lgh_id->lgl_oid, lgh_id->lgl_ogen);
513 dchild = lookup_one_len(fidname, ctxt->loc_objects_dir, fidlen);
514 if (IS_ERR(dchild)) {
515 up(&ctxt->loc_objects_dir->d_inode->i_sem);
516 RETURN((struct file *)dchild);
518 if (dchild->d_inode == NULL) {
519 struct dentry_params dp;
522 dchild->d_fsdata = (void *) &dp;
524 dp.p_inum = lgh_id->lgl_oid;
525 rc = ll_vfs_create(ctxt->loc_objects_dir->d_inode,
526 dchild, S_IFREG, NULL);
527 if (dchild->d_fsdata == (void *)(unsigned long)lgh_id->lgl_oid)
528 dchild->d_fsdata = NULL;
530 CDEBUG(D_INODE, "err during create: %d\n", rc);
532 up(&ctxt->loc_objects_dir->d_inode->i_sem);
535 inode = dchild->d_inode;
536 LASSERT(inode->i_ino == lgh_id->lgl_oid);
537 inode->i_generation = lgh_id->lgl_ogen;
538 CDEBUG(D_HA, "recreated ino %lu with gen %u\n",
539 inode->i_ino, inode->i_generation);
540 mark_inode_dirty(inode);
543 mntget(ctxt->loc_lvfs_ctxt->pwdmnt);
544 filp = dentry_open(dchild, ctxt->loc_lvfs_ctxt->pwdmnt,
545 O_RDWR | O_LARGEFILE);
548 up(&ctxt->loc_objects_dir->d_inode->i_sem);
551 if (!S_ISREG(filp->f_dentry->d_inode->i_mode)) {
552 CERROR("%s is not a regular file!: mode = %o\n", fidname,
553 filp->f_dentry->d_inode->i_mode);
555 up(&ctxt->loc_objects_dir->d_inode->i_sem);
556 RETURN(ERR_PTR(-ENOENT));
559 up(&ctxt->loc_objects_dir->d_inode->i_sem);
563 unsigned int tmpname = ll_insecure_random_int();
564 char fidname[LL_FID_NAMELEN];
565 struct dentry *new_child, *parent;
569 sprintf(fidname, "OBJECTS/%u", tmpname);
570 filp = filp_open(fidname, O_CREAT | O_EXCL, 0644);
574 CERROR("impossible object name collision %u\n",
578 CERROR("error creating tmp object %u: rc %d\n", tmpname, rc);
582 namelen = ll_fid2str(fidname, filp->f_dentry->d_inode->i_ino,
583 filp->f_dentry->d_inode->i_generation);
584 parent = filp->f_dentry->d_parent;
585 down(&parent->d_inode->i_sem);
586 new_child = lookup_one_len(fidname, parent, namelen);
587 if (IS_ERR(new_child)) {
588 CERROR("getting neg dentry for obj rename: %d\n", rc);
589 GOTO(out_close, rc = PTR_ERR(new_child));
591 if (new_child->d_inode != NULL) {
592 CERROR("impossible non-negative obj dentry %lu:%u!\n",
593 filp->f_dentry->d_inode->i_ino,
594 filp->f_dentry->d_inode->i_generation);
598 handle = llog_fsfilt_start(ctxt, parent->d_inode, FSFILT_OP_RENAME, NULL);
600 GOTO(out_dput, rc = PTR_ERR(handle));
603 rc = vfs_rename(parent->d_inode, filp->f_dentry,
604 parent->d_inode, new_child);
607 CERROR("error renaming new object %lu:%u: rc %d\n",
608 filp->f_dentry->d_inode->i_ino,
609 filp->f_dentry->d_inode->i_generation, rc);
611 err = llog_fsfilt_commit(ctxt, parent->d_inode, handle, 0);
618 up(&parent->d_inode->i_sem);
623 /* FIXME: is this group 1 is correct? */
625 lgh_id->lgl_oid = filp->f_dentry->d_inode->i_ino;
626 lgh_id->lgl_ogen = filp->f_dentry->d_inode->i_generation;
632 /* creates object for generic case (obd exists) */
634 llog_object_create_generic(struct llog_ctxt *ctxt, struct llog_logid *lgh_id)
637 struct dentry *dchild;
638 struct obd_device *obd;
639 struct obdo *oa = NULL;
640 int open_flags = O_RDWR | O_LARGEFILE;
644 obd = ctxt->loc_exp->exp_obd;
645 LASSERT(obd != NULL);
647 if (lgh_id->lgl_oid) {
648 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, lgh_id->lgl_oid,
649 lgh_id->lgl_ogen, lgh_id->lgl_ogr);
650 if (IS_ERR(dchild) == -ENOENT) {
651 OBD_ALLOC(oa, sizeof(*oa));
653 RETURN(ERR_PTR(-ENOMEM));
655 oa->o_id = lgh_id->lgl_oid;
656 oa->o_generation = lgh_id->lgl_ogen;
657 oa->o_gr = lgh_id->lgl_ogr;
658 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
659 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
661 CDEBUG(D_INODE, "err during create: %d\n", rc);
662 GOTO(out_free_oa, rc);
664 CDEBUG(D_HA, "re-create log object "LPX64":0x%x:"LPX64"\n",
665 lgh_id->lgl_oid, lgh_id->lgl_ogen, lgh_id->lgl_ogr);
667 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, lgh_id->lgl_oid,
668 lgh_id->lgl_ogen, lgh_id->lgl_ogr);
669 } else if (IS_ERR(dchild)) {
670 CERROR("error looking up logfile "LPX64":0x%x: rc %d\n",
671 lgh_id->lgl_oid, lgh_id->lgl_ogen, rc);
672 RETURN((struct file *)dchild);
675 filp = l_dentry_open(&obd->obd_lvfs_ctxt, dchild, open_flags);
679 CERROR("error opening logfile "LPX64"0x%x: rc %d\n",
680 lgh_id->lgl_oid, lgh_id->lgl_ogen, rc);
682 GOTO(out_free_oa, rc);
684 /* this is important to work here over obd_create() as it manages
685 groups and we need it. Yet another reason is that mds_obd_create()
686 is fully the same as old version of this function and this helps
687 us to avoid code duplicating and layering violating. */
688 OBD_ALLOC(oa, sizeof(*oa));
690 RETURN(ERR_PTR(-ENOMEM));
692 oa->o_gr = FILTER_GROUP_LLOG;
693 oa->o_valid = OBD_MD_FLGENER | OBD_MD_FLGROUP;
694 rc = obd_create(ctxt->loc_exp, oa, NULL, NULL);
696 GOTO(out_free_oa, rc);
698 dchild = obd_lvfs_fid2dentry(ctxt->loc_exp, oa->o_id,
699 oa->o_generation, oa->o_gr);
701 GOTO(out_free_oa, rc = PTR_ERR(dchild));
703 filp = l_dentry_open(&obd->obd_lvfs_ctxt, dchild,
707 GOTO(out_free_oa, rc = PTR_ERR(filp));
710 /* group 1 is not longer valid, we use the group which is set
711 by obd_create()->mds_obd_create(). */
712 lgh_id->lgl_ogr = oa->o_gr;
713 lgh_id->lgl_oid = oa->o_id;
714 lgh_id->lgl_ogen = oa->o_generation;
721 OBD_FREE(oa, sizeof(*oa));
726 llog_object_create(struct llog_ctxt *ctxt, struct llog_logid *lgh_id)
729 return llog_object_create_alone(ctxt, lgh_id);
731 return llog_object_create_generic(ctxt, lgh_id);
734 static int llog_add_link_object(struct llog_ctxt *ctxt, struct llog_logid logid,
735 struct dentry *dentry)
737 struct dentry *new_child;
738 char fidname[LL_FID_NAMELEN];
740 int namelen, rc = 0, err;
743 namelen = ll_fid2str(fidname, logid.lgl_oid, logid.lgl_ogen);
744 down(&ctxt->loc_objects_dir->d_inode->i_sem);
745 new_child = lookup_one_len(fidname, ctxt->loc_objects_dir, namelen);
746 if (IS_ERR(new_child)) {
747 CERROR("getting neg dentry for obj rename: %d\n", rc);
748 GOTO(out, rc = PTR_ERR(new_child));
750 if (new_child->d_inode == dentry->d_inode)
752 if (new_child->d_inode != NULL) {
753 CERROR("impossible non-negative obj dentry "LPX64":%u!\n",
754 logid.lgl_oid, logid.lgl_ogen);
757 handle = llog_fsfilt_start(ctxt, ctxt->loc_objects_dir->d_inode,
758 FSFILT_OP_LINK, NULL);
760 GOTO(out_dput, rc = PTR_ERR(handle));
763 rc = vfs_link(dentry, ctxt->loc_objects_dir->d_inode, new_child);
766 CERROR("error link new object "LPX64":%u: rc %d\n",
767 logid.lgl_oid, logid.lgl_ogen, rc);
768 err = llog_fsfilt_commit(ctxt, ctxt->loc_objects_dir->d_inode, handle, 0);
772 up(&ctxt->loc_objects_dir->d_inode->i_sem);
776 static int llog_lvfs_open(struct llog_ctxt *ctxt, struct llog_handle **res,
777 struct llog_logid *logid, char *name, int flags)
779 struct llog_handle *handle;
780 struct lvfs_run_ctxt saved;
782 int open_flags = O_RDWR | O_LARGEFILE;
785 if (flags & OBD_LLOG_FL_CREATE)
786 open_flags |= O_CREAT;
788 handle = llog_alloc_handle();
794 if (ctxt->loc_lvfs_ctxt)
795 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
798 handle->lgh_file = llog_object_create(ctxt, logid);
799 if (IS_ERR(handle->lgh_file)) {
800 CERROR("cannot create/open llog object "LPX64":%x "
801 "error = %ld", logid->lgl_oid, logid->lgl_ogen,
802 PTR_ERR(handle->lgh_file));
803 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
805 handle->lgh_id = *logid;
808 handle->lgh_file = llog_filp_open(name, open_flags, 0644);
809 if (IS_ERR(handle->lgh_file)) {
810 CERROR("cannot open %s file, error = %ld\n",
811 name, PTR_ERR(handle->lgh_file));
812 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
814 LASSERT(handle->lgh_file->f_dentry->d_parent == ctxt->loc_logs_dir);
816 handle->lgh_id.lgl_ogr = 1;
817 handle->lgh_id.lgl_oid = handle->lgh_file->f_dentry->d_inode->i_ino;
818 handle->lgh_id.lgl_ogen = handle->lgh_file->f_dentry->d_inode->i_generation;
819 rc = llog_add_link_object(ctxt, handle->lgh_id, handle->lgh_file->f_dentry);
824 handle->lgh_file = llog_object_create(ctxt, &handle->lgh_id);
825 if (IS_ERR(handle->lgh_file)) {
826 CERROR("cannot create llog object, error = %ld\n",
827 PTR_ERR(handle->lgh_file));
828 GOTO(cleanup, rc = PTR_ERR(handle->lgh_file));
832 handle->lgh_ctxt = ctxt;
834 if (ctxt->loc_lvfs_ctxt)
835 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
838 llog_free_handle(handle);
842 static int llog_lvfs_close(struct llog_handle *handle)
847 rc = filp_close(handle->lgh_file, 0);
849 CERROR("error closing log: rc %d\n", rc);
853 static int llog_lvfs_destroy(struct llog_handle *loghandle)
855 struct llog_ctxt *ctxt = loghandle->lgh_ctxt;
856 struct lvfs_run_ctxt saved;
857 struct dentry *fdentry;
858 struct inode *parent_inode;
859 char fidname[LL_FID_NAMELEN];
861 int rc = -EINVAL, err, namelen;
864 if (ctxt->loc_lvfs_ctxt)
865 push_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
867 fdentry = loghandle->lgh_file->f_dentry;
868 parent_inode = fdentry->d_parent->d_inode;
870 if (!strcmp(fdentry->d_parent->d_name.name, "LOGS")) {
871 LASSERT(parent_inode == ctxt->loc_logs_dir->d_inode);
873 namelen = ll_fid2str(fidname, fdentry->d_inode->i_ino,
874 fdentry->d_inode->i_generation);
876 rc = llog_lvfs_close(loghandle);
882 handle = llog_fsfilt_start(ctxt, parent_inode,
883 FSFILT_OP_UNLINK, NULL);
884 if (IS_ERR(handle)) {
886 GOTO(out, rc = PTR_ERR(handle));
889 down(&parent_inode->i_sem);
890 rc = vfs_unlink(parent_inode, fdentry);
891 up(&parent_inode->i_sem);
895 down(&ctxt->loc_objects_dir->d_inode->i_sem);
896 fdentry = lookup_one_len(fidname, ctxt->loc_objects_dir,
898 if (fdentry == NULL || fdentry->d_inode == NULL) {
899 CERROR("destroy non_existent object %s\n", fidname);
900 GOTO(out_err, rc = IS_ERR(fdentry) ?
901 PTR_ERR(fdentry) : -ENOENT);
903 rc = vfs_unlink(ctxt->loc_objects_dir->d_inode, fdentry);
906 up(&ctxt->loc_objects_dir->d_inode->i_sem);
908 err = llog_fsfilt_commit(ctxt, parent_inode, handle, 0);
914 if (ctxt->loc_alone) {
915 if (!strcmp(fdentry->d_parent->d_name.name, "OBJECTS")) {
916 LASSERT(parent_inode == ctxt->loc_objects_dir->d_inode);
919 rc = llog_lvfs_close(loghandle);
921 down(&parent_inode->i_sem);
922 rc = vfs_unlink(parent_inode, fdentry);
923 up(&parent_inode->i_sem);
928 struct obdo *oa = NULL;
930 OBD_ALLOC(oa, sizeof(*oa));
932 GOTO(out, rc = -ENOMEM);
934 oa->o_id = loghandle->lgh_id.lgl_oid;
935 oa->o_gr = loghandle->lgh_id.lgl_ogr;
936 oa->o_generation = loghandle->lgh_id.lgl_ogen;
937 oa->o_valid = OBD_MD_FLID | OBD_MD_FLGROUP | OBD_MD_FLGENER;
939 rc = llog_lvfs_close(loghandle);
941 GOTO(out_free_oa, rc);
943 rc = obd_destroy(loghandle->lgh_ctxt->loc_exp, oa, NULL, NULL);
945 OBD_FREE(oa, sizeof(*oa));
948 if (ctxt->loc_lvfs_ctxt)
949 pop_ctxt(&saved, ctxt->loc_lvfs_ctxt, NULL);
953 /* reads the catalog list */
954 int llog_get_cat_list(struct lvfs_run_ctxt *ctxt,
955 struct fsfilt_operations *fsops, char *name,
956 int count, struct llog_catid *idarray)
958 struct lvfs_run_ctxt saved;
960 int size = sizeof(*idarray) * count;
967 push_ctxt(&saved, ctxt, NULL);
968 file = l_filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
969 if (!file || IS_ERR(file)) {
971 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
976 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
977 CERROR("%s is not a regular file!: mode = %o\n", name,
978 file->f_dentry->d_inode->i_mode);
979 GOTO(out, rc = -ENOENT);
982 rc = fsops->fs_read_record(file, idarray, size, &off);
984 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
990 if (file && !IS_ERR(file))
991 rc = filp_close(file, 0);
993 pop_ctxt(&saved, ctxt, NULL);
996 EXPORT_SYMBOL(llog_get_cat_list);
998 /* writes the cat list */
999 int llog_put_cat_list(struct lvfs_run_ctxt *ctxt,
1000 struct fsfilt_operations *fsops, char *name,
1001 int count, struct llog_catid *idarray)
1003 struct lvfs_run_ctxt saved;
1004 struct l_file *file;
1005 int size = sizeof(*idarray) * count;
1012 push_ctxt(&saved, ctxt, NULL);
1013 file = filp_open(name, O_RDWR | O_CREAT | O_LARGEFILE, 0700);
1014 if (!file || IS_ERR(file)) {
1016 CERROR("OBD filter: cannot open/create %s: rc = %d\n",
1021 if (!S_ISREG(file->f_dentry->d_inode->i_mode)) {
1022 CERROR("%s is not a regular file!: mode = %o\n", name,
1023 file->f_dentry->d_inode->i_mode);
1024 GOTO(out, rc = -ENOENT);
1027 rc = fsops->fs_write_record(file, idarray, size, &off, 1);
1029 CDEBUG(D_INODE,"OBD filter: error reading %s: rc %d\n",
1035 if (file && !IS_ERR(file))
1036 rc = filp_close(file, 0);
1038 pop_ctxt(&saved, ctxt, NULL);
1041 EXPORT_SYMBOL(llog_put_cat_list);
1043 struct llog_operations llog_lvfs_ops = {
1044 lop_open: llog_lvfs_open,
1045 lop_destroy: llog_lvfs_destroy,
1046 lop_close: llog_lvfs_close,
1047 lop_read_header: llog_lvfs_read_header,
1048 lop_write_rec: llog_lvfs_write_rec,
1049 lop_next_block: llog_lvfs_next_block,
1050 lop_prev_block: llog_lvfs_prev_block,
1052 EXPORT_SYMBOL(llog_lvfs_ops);
1054 #else /* !__KERNEL__ */
1056 static int llog_lvfs_read_header(struct llog_handle *handle)
1062 static int llog_lvfs_write_rec(struct llog_handle *loghandle,
1063 struct llog_rec_hdr *rec,
1064 struct llog_cookie *reccookie, int cookiecount,
1071 static int llog_lvfs_open(struct llog_ctxt *ctxt, struct llog_handle **res,
1072 struct llog_logid *logid, char *name, int flags)
1078 static int llog_lvfs_close(struct llog_handle *handle)
1084 static int llog_lvfs_destroy(struct llog_handle *handle)
1090 int llog_get_cat_list(struct lvfs_run_ctxt *ctxt,
1091 struct fsfilt_operations *fsops, char *name,
1092 int count, struct llog_catid *idarray)
1098 int llog_put_cat_list(struct lvfs_run_ctxt *ctxt,
1099 struct fsfilt_operations *fsops, char *name,
1100 int count, struct llog_catid *idarray)
1106 int llog_lvfs_prev_block(struct llog_handle *loghandle,
1107 int prev_idx, void *buf, int len)
1113 int llog_lvfs_next_block(struct llog_handle *loghandle, int *curr_idx,
1114 int next_idx, __u64 *offset, void *buf, int len)
1120 struct llog_operations llog_lvfs_ops = {
1121 lop_open: llog_lvfs_open,
1122 lop_destroy: llog_lvfs_destroy,
1123 lop_close: llog_lvfs_close,
1124 lop_read_header: llog_lvfs_read_header,
1125 lop_write_rec: llog_lvfs_write_rec,
1126 lop_next_block: llog_lvfs_next_block,
1127 lop_prev_block: llog_lvfs_prev_block,