Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / mds / JournalPointer.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software 
11  * Foundation.  See file COPYING.
12  * 
13  */
14
15
16 #include "common/debug.h"
17 #include "common/errno.h"
18 #include "common/Cond.h"
19 #include "osdc/Objecter.h"
20 #include "mds/mdstypes.h"
21
22 #include "mds/JournalPointer.h"
23
24
25 #define dout_context g_ceph_context
26 #define dout_subsys ceph_subsys_journaler
27 #undef dout_prefix
28 #define dout_prefix *_dout << objecter->messenger->get_myname() << ".journalpointer "
29
30
31 std::string JournalPointer::get_object_id() const
32 {
33   inodeno_t const pointer_ino = MDS_INO_LOG_POINTER_OFFSET + node_id;
34   char buf[32];
35   snprintf(buf, sizeof(buf), "%llx.%08llx", (long long unsigned)pointer_ino, (long long unsigned)0);
36
37   return std::string(buf);
38 }
39
40
41 /**
42  * Blocking read of JournalPointer for this MDS
43  */
44 int JournalPointer::load(Objecter *objecter)
45 {
46   assert(objecter != NULL);
47
48   // Blocking read of data
49   std::string const object_id = get_object_id();
50   dout(4) << "Reading journal pointer '" << object_id << "'" << dendl;
51   bufferlist data;
52   C_SaferCond waiter;
53   objecter->read_full(object_t(object_id), object_locator_t(pool_id),
54       CEPH_NOSNAP, &data, 0, &waiter);
55   int r = waiter.wait();
56
57   // Construct JournalPointer result, null or decoded data
58   if (r == 0) {
59     bufferlist::iterator q = data.begin();
60     try {
61       decode(q);
62     } catch (const buffer::error &e) {
63       return -EINVAL;
64     }
65   } else {
66     dout(1) << "Journal pointer '" << object_id << "' read failed: " << cpp_strerror(r) << dendl;
67   }
68   return r;
69 }
70
71
72 /**
73  * Blocking write of JournalPointer for this MDS
74  *
75  * @return objecter write op status code
76  */
77 int JournalPointer::save(Objecter *objecter) const
78 {
79   assert(objecter != NULL);
80   // It is not valid to persist a null pointer
81   assert(!is_null());
82
83   // Serialize JournalPointer object
84   bufferlist data;
85   encode(data);
86
87   // Write to RADOS and wait for durability
88   std::string const object_id = get_object_id();
89   dout(4) << "Writing pointer object '" << object_id << "': 0x"
90     << std::hex << front << ":0x" << back << std::dec << dendl;
91
92   C_SaferCond waiter;
93   objecter->write_full(object_t(object_id), object_locator_t(pool_id),
94                        SnapContext(), data,
95                        ceph::real_clock::now(), 0,
96                        &waiter);
97   int write_result = waiter.wait();
98   if (write_result < 0) {
99     derr << "Error writing pointer object '" << object_id << "': " << cpp_strerror(write_result) << dendl;
100   }
101   return write_result;
102 }
103
104
105 /**
106  * Non-blocking variant of save() that assumes objecter lock already held by
107  * caller
108  */
109 void JournalPointer::save(Objecter *objecter, Context *completion) const
110 {
111   assert(objecter != NULL);
112
113   bufferlist data;
114   encode(data);
115
116   objecter->write_full(object_t(get_object_id()), object_locator_t(pool_id),
117                        SnapContext(), data,
118                        ceph::real_clock::now(), 0,
119                        completion);
120 }
121