summaryrefslogtreecommitdiffstats
path: root/src/mds/JournalPointer.cc
blob: 8a446a20f80588e591bbd4e6580fbd34ee1e932f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */


#include "common/debug.h"
#include "common/errno.h"
#include "common/Cond.h"
#include "osdc/Objecter.h"
#include "mds/mdstypes.h"
#include "msg/Messenger.h"

#include "mds/JournalPointer.h"


#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_journaler
#undef dout_prefix
#define dout_prefix *_dout << objecter->messenger->get_myname() << ".journalpointer "


std::string JournalPointer::get_object_id() const
{
  inodeno_t const pointer_ino = MDS_INO_LOG_POINTER_OFFSET + node_id;
  char buf[32];
  snprintf(buf, sizeof(buf), "%llx.%08llx", (long long unsigned)pointer_ino, (long long unsigned)0);

  return std::string(buf);
}


/**
 * Blocking read of JournalPointer for this MDS
 */
int JournalPointer::load(Objecter *objecter)
{
  ceph_assert(objecter != NULL);

  // Blocking read of data
  std::string const object_id = get_object_id();
  dout(4) << "Reading journal pointer '" << object_id << "'" << dendl;
  bufferlist data;
  C_SaferCond waiter;
  objecter->read_full(object_t(object_id), object_locator_t(pool_id),
      CEPH_NOSNAP, &data, 0, &waiter);
  int r = waiter.wait();

  // Construct JournalPointer result, null or decoded data
  if (r == 0) {
    auto q = data.cbegin();
    try {
      decode(q);
    } catch (const buffer::error &e) {
      return -CEPHFS_EINVAL;
    }
  } else {
    dout(1) << "Journal pointer '" << object_id << "' read failed: " << cpp_strerror(r) << dendl;
  }
  return r;
}


/**
 * Blocking write of JournalPointer for this MDS
 *
 * @return objecter write op status code
 */
int JournalPointer::save(Objecter *objecter) const
{
  ceph_assert(objecter != NULL);
  // It is not valid to persist a null pointer
  ceph_assert(!is_null());

  // Serialize JournalPointer object
  bufferlist data;
  encode(data);

  // Write to RADOS and wait for durability
  std::string const object_id = get_object_id();
  dout(4) << "Writing pointer object '" << object_id << "': 0x"
    << std::hex << front << ":0x" << back << std::dec << dendl;

  C_SaferCond waiter;
  objecter->write_full(object_t(object_id), object_locator_t(pool_id),
		       SnapContext(), data,
		       ceph::real_clock::now(), 0,
		       &waiter);
  int write_result = waiter.wait();
  if (write_result < 0) {
    derr << "Error writing pointer object '" << object_id << "': " << cpp_strerror(write_result) << dendl;
  }
  return write_result;
}


/**
 * Non-blocking variant of save() that assumes objecter lock already held by
 * caller
 */
void JournalPointer::save(Objecter *objecter, Context *completion) const
{
  ceph_assert(objecter != NULL);

  bufferlist data;
  encode(data);

  objecter->write_full(object_t(get_object_id()), object_locator_t(pool_id),
		       SnapContext(), data,
		       ceph::real_clock::now(), 0,
		       completion);
}