zone_table_segment_mapped.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // Copyright (C) 2013 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <datasrc/memory/zone_table_segment_mapped.h>
  15. #include <memory>
  16. using namespace isc::data;
  17. using namespace isc::dns;
  18. using namespace isc::util;
  19. namespace isc {
  20. namespace datasrc {
  21. namespace memory {
  22. namespace { // unnamed namespace
  23. // The name with which the zone table checksum is associated in the segment.
  24. const char* const ZONE_TABLE_CHECKSUM_NAME = "zone_table_checksum";
  25. // The name with which the zone table header is associated in the segment.
  26. const char* const ZONE_TABLE_HEADER_NAME = "zone_table_header";
  27. } // end of unnamed namespace
  28. ZoneTableSegmentMapped::ZoneTableSegmentMapped(const RRClass& rrclass) :
  29. ZoneTableSegment(rrclass),
  30. impl_type_("mapped"),
  31. rrclass_(rrclass),
  32. cached_header_(NULL)
  33. {
  34. }
  35. ZoneTableSegmentMapped::~ZoneTableSegmentMapped() {
  36. sync();
  37. }
  38. const std::string&
  39. ZoneTableSegmentMapped::getImplType() const {
  40. return (impl_type_);
  41. }
  42. bool
  43. ZoneTableSegmentMapped::processChecksum(MemorySegmentMapped& segment,
  44. bool create,
  45. std::string& error_msg)
  46. {
  47. const MemorySegment::NamedAddressResult result =
  48. segment.getNamedAddress(ZONE_TABLE_CHECKSUM_NAME);
  49. if (result.first) {
  50. if (create) {
  51. // There must be no previously saved checksum.
  52. error_msg = "There is already a saved checksum in the segment "
  53. "opened in create mode";
  54. return (false);
  55. } else {
  56. // The segment was already shrunk when it was last
  57. // closed. Check that its checksum is consistent.
  58. assert(result.second);
  59. size_t* checksum = static_cast<size_t*>(result.second);
  60. const size_t saved_checksum = *checksum;
  61. // First, clear the checksum so that getCheckSum() returns a
  62. // consistent value.
  63. *checksum = 0;
  64. const size_t new_checksum = segment.getCheckSum();
  65. if (saved_checksum != new_checksum) {
  66. error_msg = "Saved checksum doesn't match segment data";
  67. return (false);
  68. }
  69. }
  70. } else {
  71. // Allocate space for a checksum (which is saved during close).
  72. void* checksum = NULL;
  73. while (!checksum) {
  74. try {
  75. checksum = segment.allocate(sizeof(size_t));
  76. } catch (const MemorySegmentGrown&) {
  77. // Do nothing and try again.
  78. }
  79. }
  80. *static_cast<size_t*>(checksum) = 0;
  81. segment.setNamedAddress(ZONE_TABLE_CHECKSUM_NAME, checksum);
  82. }
  83. return (true);
  84. }
  85. bool
  86. ZoneTableSegmentMapped::processHeader(MemorySegmentMapped& segment,
  87. bool create,
  88. std::string& error_msg)
  89. {
  90. const MemorySegment::NamedAddressResult result =
  91. segment.getNamedAddress(ZONE_TABLE_HEADER_NAME);
  92. if (result.first) {
  93. if (create) {
  94. // There must be no previously saved checksum.
  95. error_msg = "There is already a saved ZoneTableHeader in the "
  96. "segment opened in create mode";
  97. return (false);
  98. } else {
  99. assert(result.second);
  100. }
  101. } else {
  102. void* ptr = NULL;
  103. while (!ptr) {
  104. try {
  105. ptr = segment.allocate(sizeof(ZoneTableHeader));
  106. } catch (const MemorySegmentGrown&) {
  107. // Do nothing and try again.
  108. }
  109. }
  110. try {
  111. ZoneTableHeader* new_header = new(ptr)
  112. ZoneTableHeader(ZoneTable::create(segment, rrclass_));
  113. segment.setNamedAddress(ZONE_TABLE_HEADER_NAME, new_header);
  114. } catch (const MemorySegmentGrown&) {
  115. // This is extremely unlikely and we just throw a fatal
  116. // exception here without attempting to recover.
  117. throw std::bad_alloc();
  118. }
  119. }
  120. return (true);
  121. }
  122. MemorySegmentMapped*
  123. ZoneTableSegmentMapped::openReadWrite(const std::string& filename,
  124. bool create)
  125. {
  126. const MemorySegmentMapped::OpenMode mode = create ?
  127. MemorySegmentMapped::CREATE_ONLY :
  128. MemorySegmentMapped::OPEN_OR_CREATE;
  129. // In case there is a problem, we throw. We want the segment to be
  130. // automatically destroyed then.
  131. std::auto_ptr<MemorySegmentMapped> segment
  132. (new MemorySegmentMapped(filename, mode));
  133. std::string error_msg;
  134. if ((!processChecksum(*segment, create, error_msg)) ||
  135. (!processHeader(*segment, create, error_msg))) {
  136. if (mem_sgmt_) {
  137. isc_throw(ResetFailed,
  138. "Error in resetting zone table segment to use "
  139. << filename << ": " << error_msg);
  140. } else {
  141. isc_throw(ResetFailedAndSegmentCleared,
  142. "Error in resetting zone table segment to use "
  143. << filename << ": " << error_msg);
  144. }
  145. }
  146. return (segment.release());
  147. }
  148. MemorySegmentMapped*
  149. ZoneTableSegmentMapped::openReadOnly(const std::string& filename) {
  150. // In case the checksum or table header is missing, we throw. We
  151. // want the segment to be automatically destroyed then.
  152. std::auto_ptr<MemorySegmentMapped> segment
  153. (new MemorySegmentMapped(filename));
  154. // There must be a previously saved checksum.
  155. MemorySegment::NamedAddressResult result =
  156. segment->getNamedAddress(ZONE_TABLE_CHECKSUM_NAME);
  157. if (!result.first) {
  158. const std::string error_msg =
  159. "There is no previously saved checksum in a "
  160. "mapped segment opened in read-only mode";
  161. if (mem_sgmt_) {
  162. isc_throw(ResetFailed,
  163. "Error in resetting zone table segment to use "
  164. << filename << ": " << error_msg);
  165. } else {
  166. isc_throw(ResetFailedAndSegmentCleared,
  167. "Error in resetting zone table segment to use "
  168. << filename << ": " << error_msg);
  169. }
  170. }
  171. // We can't verify the checksum here as we can't set the checksum to
  172. // 0 for checksum calculation in a read-only segment. So we continue
  173. // without verifying the checksum.
  174. // There must be a previously saved ZoneTableHeader.
  175. result = segment->getNamedAddress(ZONE_TABLE_HEADER_NAME);
  176. if (result.first) {
  177. assert(result.second);
  178. } else {
  179. const std::string error_msg =
  180. "There is no previously saved ZoneTableHeader in a "
  181. "mapped segment opened in read-only mode.";
  182. if (mem_sgmt_) {
  183. isc_throw(ResetFailed,
  184. "Error in resetting zone table segment to use "
  185. << filename << ": " << error_msg);
  186. } else {
  187. isc_throw(ResetFailedAndSegmentCleared,
  188. "Error in resetting zone table segment to use "
  189. << filename << ": " << error_msg);
  190. }
  191. }
  192. return (segment.release());
  193. }
  194. void
  195. ZoneTableSegmentMapped::reset(MemorySegmentOpenMode mode,
  196. isc::data::ConstElementPtr params)
  197. {
  198. if (!params || params->getType() != Element::map) {
  199. isc_throw(isc::InvalidParameter,
  200. "Configuration does not contain a map");
  201. }
  202. if (!params->contains("mapped-file")) {
  203. isc_throw(isc::InvalidParameter,
  204. "Configuration does not contain a \"mapped-file\" key");
  205. }
  206. ConstElementPtr mapped_file = params->get("mapped-file");
  207. if ((!mapped_file) || (mapped_file->getType() != Element::string)) {
  208. isc_throw(isc::InvalidParameter,
  209. "Value of \"mapped-file\" is not a string");
  210. }
  211. const std::string filename = mapped_file->stringValue();
  212. if (mem_sgmt_ && (filename == current_filename_)) {
  213. // This reset() is an attempt to re-open the currently open
  214. // mapped file. We cannot do this in many mode combinations
  215. // unless we close the existing mapped file. So just close it.
  216. clear();
  217. } else {
  218. sync();
  219. }
  220. // In case current_filename_ below fails, we want the segment to be
  221. // automatically destroyed.
  222. std::auto_ptr<MemorySegmentMapped> segment;
  223. switch (mode) {
  224. case CREATE:
  225. segment.reset(openReadWrite(filename, true));
  226. break;
  227. case READ_WRITE:
  228. segment.reset(openReadWrite(filename, false));
  229. break;
  230. case READ_ONLY:
  231. segment.reset(openReadOnly(filename));
  232. break;
  233. default:
  234. isc_throw(isc::InvalidOperation,
  235. "Invalid MemorySegmentOpenMode passed to reset()");
  236. }
  237. current_filename_ = filename;
  238. current_mode_ = mode;
  239. mem_sgmt_.reset(segment.release());
  240. // Given what we setup above, resetHeader() must not throw at this
  241. // point. If it does, all bets are off.
  242. resetHeader();
  243. }
  244. void
  245. ZoneTableSegmentMapped::sync() {
  246. // Synchronize checksum, etc.
  247. if (mem_sgmt_ && isWritable()) {
  248. // If there is a previously opened segment, and it was opened in
  249. // read-write mode, update its checksum.
  250. mem_sgmt_->shrinkToFit();
  251. const MemorySegment::NamedAddressResult result =
  252. mem_sgmt_->getNamedAddress(ZONE_TABLE_CHECKSUM_NAME);
  253. assert(result.first);
  254. assert(result.second);
  255. size_t* checksum = static_cast<size_t*>(result.second);
  256. // First, clear the checksum so that getCheckSum() returns a
  257. // consistent value.
  258. *checksum = 0;
  259. const size_t new_checksum = mem_sgmt_->getCheckSum();
  260. // Now, update it into place.
  261. *checksum = new_checksum;
  262. }
  263. }
  264. void
  265. ZoneTableSegmentMapped::clear() {
  266. if (mem_sgmt_) {
  267. sync();
  268. mem_sgmt_.reset();
  269. }
  270. }
  271. void
  272. ZoneTableSegmentMapped::resetHeader() {
  273. // We cannot perform resetHeader() lazily during getHeader() as
  274. // getHeader() has to work on const objects too. So we do it here
  275. // now.
  276. if (!isUsable()) {
  277. isc_throw(isc::InvalidOperation,
  278. "resetHeader() called without calling reset() first");
  279. }
  280. const MemorySegment::NamedAddressResult result =
  281. mem_sgmt_->getNamedAddress(ZONE_TABLE_HEADER_NAME);
  282. if (!result.first) {
  283. isc_throw(isc::Unexpected,
  284. "Unable to look up the address of the table header in "
  285. "getHeader()");
  286. }
  287. cached_header_ = static_cast<ZoneTableHeader*>(result.second);
  288. }
  289. template<typename T>
  290. T*
  291. ZoneTableSegmentMapped::getHeaderHelper() const {
  292. if (!isUsable()) {
  293. isc_throw(isc::InvalidOperation,
  294. "getHeader() called without calling reset() first");
  295. }
  296. assert(cached_header_);
  297. return (cached_header_);
  298. }
  299. ZoneTableHeader&
  300. ZoneTableSegmentMapped::getHeader() {
  301. return (*getHeaderHelper<ZoneTableHeader>());
  302. }
  303. const ZoneTableHeader&
  304. ZoneTableSegmentMapped::getHeader() const {
  305. return (*getHeaderHelper<const ZoneTableHeader>());
  306. }
  307. MemorySegment&
  308. ZoneTableSegmentMapped::getMemorySegment() {
  309. if (!isUsable()) {
  310. isc_throw(isc::InvalidOperation,
  311. "getMemorySegment() called without calling reset() first");
  312. }
  313. return (*mem_sgmt_);
  314. }
  315. bool
  316. ZoneTableSegmentMapped::isUsable() const {
  317. // If mem_sgmt_ is not empty, then it is usable.
  318. return (mem_sgmt_);
  319. }
  320. bool
  321. ZoneTableSegmentMapped::isWritable() const {
  322. if (!isUsable()) {
  323. // If reset() was never performed for this segment, or if the
  324. // most recent reset() had failed, then the segment is not
  325. // writable.
  326. return (false);
  327. }
  328. return ((current_mode_ == CREATE) || (current_mode_ == READ_WRITE));
  329. }
  330. } // namespace memory
  331. } // namespace datasrc
  332. } // namespace isc