mmap.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: mmap.cc,v 1.22 2001/05/27 05:19:30 jgg Exp $
  4. /* ######################################################################
  5. MMap Class - Provides 'real' mmap or a faked mmap using read().
  6. MMap cover class.
  7. Some broken versions of glibc2 (libc6) have a broken definition
  8. of mmap that accepts a char * -- all other systems (and libc5) use
  9. void *. We can't safely do anything here that would be portable, so
  10. libc6 generates warnings -- which should be errors, g++ isn't properly
  11. strict.
  12. ##################################################################### */
  13. /*}}}*/
  14. // Include Files /*{{{*/
  15. #define _DEFAULT_SOURCE
  16. #include <config.h>
  17. #include <apt-pkg/mmap.h>
  18. #include <apt-pkg/error.h>
  19. #include <apt-pkg/fileutl.h>
  20. #include <apt-pkg/macros.h>
  21. #include <string>
  22. #include <sys/mman.h>
  23. #include <unistd.h>
  24. #include <stdlib.h>
  25. #include <errno.h>
  26. #include <cstring>
  27. #include <apti18n.h>
  28. /*}}}*/
  29. // MMap::MMap - Constructor /*{{{*/
  30. // ---------------------------------------------------------------------
  31. /* */
  32. MMap::MMap(FileFd &F,unsigned long Flags) : Flags(Flags), iSize(0),
  33. Base(nullptr), SyncToFd(nullptr)
  34. {
  35. Map(F);
  36. }
  37. /*}}}*/
  38. // MMap::MMap - Constructor /*{{{*/
  39. // ---------------------------------------------------------------------
  40. /* */
  41. MMap::MMap(unsigned long Flags) : Flags(Flags), iSize(0),
  42. Base(nullptr), SyncToFd(nullptr)
  43. {
  44. }
  45. /*}}}*/
  46. // MMap::~MMap - Destructor /*{{{*/
  47. // ---------------------------------------------------------------------
  48. /* */
  49. MMap::~MMap()
  50. {
  51. Close();
  52. }
  53. /*}}}*/
  54. // MMap::Map - Perform the mapping /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* */
  57. bool MMap::Map(FileFd &Fd)
  58. {
  59. iSize = Fd.Size();
  60. // Set the permissions.
  61. int Prot = PROT_READ;
  62. int Map = MAP_SHARED;
  63. if ((Flags & ReadOnly) != ReadOnly)
  64. Prot |= PROT_WRITE;
  65. if ((Flags & Public) != Public)
  66. Map = MAP_PRIVATE;
  67. if (iSize == 0)
  68. return _error->Error(_("Can't mmap an empty file"));
  69. // We can't mmap compressed fd's directly, so we need to read it completely
  70. if (Fd.IsCompressed() == true)
  71. {
  72. if ((Flags & ReadOnly) != ReadOnly)
  73. return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str());
  74. Base = malloc(iSize);
  75. if (unlikely(Base == nullptr))
  76. return _error->Errno("MMap-compressed-malloc", _("Couldn't make mmap of %llu bytes"), iSize);
  77. SyncToFd = new FileFd();
  78. if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false)
  79. return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str());
  80. return true;
  81. }
  82. // Map it.
  83. Base = (Flags & Fallback) ? MAP_FAILED : mmap(0,iSize,Prot,Map,Fd.Fd(),0);
  84. if (Base == MAP_FAILED)
  85. {
  86. if (errno == ENODEV || errno == EINVAL || (Flags & Fallback))
  87. {
  88. // The filesystem doesn't support this particular kind of mmap.
  89. // So we allocate a buffer and read the whole file into it.
  90. if ((Flags & ReadOnly) == ReadOnly)
  91. {
  92. // for readonly, we don't need sync, so make it simple
  93. Base = malloc(iSize);
  94. if (unlikely(Base == nullptr))
  95. return _error->Errno("MMap-malloc", _("Couldn't make mmap of %llu bytes"), iSize);
  96. SyncToFd = new FileFd();
  97. return Fd.Seek(0L) && Fd.Read(Base, iSize);
  98. }
  99. // FIXME: Writing to compressed fd's ?
  100. int const dupped_fd = dup(Fd.Fd());
  101. if (dupped_fd == -1)
  102. return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd());
  103. Base = malloc(iSize);
  104. if (unlikely(Base == nullptr))
  105. return _error->Errno("MMap-calloc", _("Couldn't make mmap of %llu bytes"), iSize);
  106. SyncToFd = new FileFd (dupped_fd);
  107. if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize))
  108. return false;
  109. }
  110. else
  111. return _error->Errno("MMap-mmap", _("Couldn't make mmap of %llu bytes"), iSize);
  112. }
  113. return true;
  114. }
  115. /*}}}*/
  116. // MMap::Close - Close the map /*{{{*/
  117. // ---------------------------------------------------------------------
  118. /* */
  119. bool MMap::Close(bool DoSync)
  120. {
  121. if ((Flags & UnMapped) == UnMapped || validData() == false || iSize == 0)
  122. return true;
  123. if (DoSync == true)
  124. Sync();
  125. if (SyncToFd != NULL)
  126. {
  127. free(Base);
  128. delete SyncToFd;
  129. SyncToFd = NULL;
  130. }
  131. else
  132. {
  133. if (munmap((char *)Base, iSize) != 0)
  134. _error->WarningE("mmap", _("Unable to close mmap"));
  135. }
  136. iSize = 0;
  137. Base = 0;
  138. return true;
  139. }
  140. /*}}}*/
  141. // MMap::Sync - Syncronize the map with the disk /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* This is done in syncronous mode - the docs indicate that this will
  144. not return till all IO is complete */
  145. bool MMap::Sync()
  146. {
  147. if ((Flags & UnMapped) == UnMapped)
  148. return true;
  149. if ((Flags & ReadOnly) != ReadOnly)
  150. {
  151. if (SyncToFd != NULL)
  152. {
  153. if (!SyncToFd->Seek(0) || !SyncToFd->Write(Base, iSize))
  154. return false;
  155. }
  156. else
  157. {
  158. #ifdef _POSIX_SYNCHRONIZED_IO
  159. if (msync((char *)Base, iSize, MS_SYNC) < 0)
  160. return _error->Errno("msync", _("Unable to synchronize mmap"));
  161. #endif
  162. }
  163. }
  164. return true;
  165. }
  166. /*}}}*/
  167. // MMap::Sync - Syncronize a section of the file to disk /*{{{*/
  168. // ---------------------------------------------------------------------
  169. /* */
  170. bool MMap::Sync(unsigned long Start,unsigned long Stop)
  171. {
  172. if ((Flags & UnMapped) == UnMapped)
  173. return true;
  174. if ((Flags & ReadOnly) != ReadOnly)
  175. {
  176. if (SyncToFd != 0)
  177. {
  178. if (!SyncToFd->Seek(Start) ||
  179. !SyncToFd->Write (((char *)Base)+Start, Stop-Start))
  180. return false;
  181. }
  182. else
  183. {
  184. #ifdef _POSIX_SYNCHRONIZED_IO
  185. unsigned long long const PSize = sysconf(_SC_PAGESIZE);
  186. Start = (Start/PSize)*PSize;
  187. if (msync((char *)Base+Start, Stop - Start, MS_SYNC) < 0)
  188. return _error->Errno("msync", _("Unable to synchronize mmap"));
  189. #endif
  190. }
  191. }
  192. return true;
  193. }
  194. /*}}}*/
  195. // DynamicMMap::DynamicMMap - Constructor /*{{{*/
  196. // ---------------------------------------------------------------------
  197. /* */
  198. DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &Workspace,
  199. unsigned long const &Grow, unsigned long const &Limit) :
  200. MMap(Flags), Fd(&F), WorkSpace(Workspace),
  201. GrowFactor(Grow), Limit(Limit)
  202. {
  203. // disable Moveable if we don't grow
  204. if (Grow == 0)
  205. this->Flags &= ~Moveable;
  206. #ifndef __linux__
  207. // kfreebsd doesn't have mremap, so we use the fallback
  208. if ((this->Flags & Moveable) == Moveable)
  209. this->Flags |= Fallback;
  210. #endif
  211. unsigned long long EndOfFile = Fd->Size();
  212. if (EndOfFile > WorkSpace)
  213. WorkSpace = EndOfFile;
  214. else if(WorkSpace > 0)
  215. {
  216. Fd->Seek(WorkSpace - 1);
  217. char C = 0;
  218. Fd->Write(&C,sizeof(C));
  219. }
  220. Map(F);
  221. iSize = EndOfFile;
  222. }
  223. /*}}}*/
  224. // DynamicMMap::DynamicMMap - Constructor for a non-file backed map /*{{{*/
  225. // ---------------------------------------------------------------------
  226. /* We try here to use mmap to reserve some space - this is much more
  227. cooler than the fallback solution to simply allocate a char array
  228. and could come in handy later than we are able to grow such an mmap */
  229. DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace,
  230. unsigned long const &Grow, unsigned long const &Limit) :
  231. MMap(Flags | UnMapped), Fd(0), WorkSpace(WorkSpace),
  232. GrowFactor(Grow), Limit(Limit)
  233. {
  234. // disable Moveable if we don't grow
  235. if (Grow == 0)
  236. this->Flags &= ~Moveable;
  237. #ifndef __linux__
  238. // kfreebsd doesn't have mremap, so we use the fallback
  239. if ((this->Flags & Moveable) == Moveable)
  240. this->Flags |= Fallback;
  241. #endif
  242. #ifdef _POSIX_MAPPED_FILES
  243. if ((this->Flags & Fallback) != Fallback) {
  244. // Set the permissions.
  245. int Prot = PROT_READ;
  246. #ifdef MAP_ANONYMOUS
  247. int Map = MAP_PRIVATE | MAP_ANONYMOUS;
  248. #else
  249. int Map = MAP_PRIVATE | MAP_ANON;
  250. #endif
  251. if ((this->Flags & ReadOnly) != ReadOnly)
  252. Prot |= PROT_WRITE;
  253. if ((this->Flags & Public) == Public)
  254. #ifdef MAP_ANONYMOUS
  255. Map = MAP_SHARED | MAP_ANONYMOUS;
  256. #else
  257. Map = MAP_SHARED | MAP_ANON;
  258. #endif
  259. // use anonymous mmap() to get the memory
  260. Base = (unsigned char*) mmap(0, WorkSpace, Prot, Map, -1, 0);
  261. if(Base == MAP_FAILED)
  262. _error->Errno("DynamicMMap",_("Couldn't make mmap of %lu bytes"),WorkSpace);
  263. iSize = 0;
  264. return;
  265. }
  266. #endif
  267. // fallback to a static allocated space
  268. Base = calloc(WorkSpace, 1);
  269. iSize = 0;
  270. }
  271. /*}}}*/
  272. // DynamicMMap::~DynamicMMap - Destructor /*{{{*/
  273. // ---------------------------------------------------------------------
  274. /* We truncate the file to the size of the memory data set */
  275. DynamicMMap::~DynamicMMap()
  276. {
  277. if (Fd == 0)
  278. {
  279. if (validData() == false)
  280. return;
  281. #ifdef _POSIX_MAPPED_FILES
  282. if ((Flags & Fallback) != Fallback) {
  283. munmap(Base, WorkSpace);
  284. } else
  285. #endif
  286. free(Base);
  287. return;
  288. }
  289. unsigned long long EndOfFile = iSize;
  290. iSize = WorkSpace;
  291. Close(false);
  292. if(ftruncate(Fd->Fd(),EndOfFile) < 0)
  293. _error->Errno("ftruncate", _("Failed to truncate file"));
  294. }
  295. /*}}}*/
  296. // DynamicMMap::RawAllocate - Allocate a raw chunk of unaligned space /*{{{*/
  297. // ---------------------------------------------------------------------
  298. /* This allocates a block of memory aligned to the given size */
  299. unsigned long DynamicMMap::RawAllocate(unsigned long long Size,unsigned long Aln)
  300. {
  301. unsigned long long Result = iSize;
  302. if (Aln != 0)
  303. Result += Aln - (iSize%Aln);
  304. iSize = Result + Size;
  305. // try to grow the buffer
  306. while(Result + Size > WorkSpace)
  307. {
  308. if(!Grow())
  309. {
  310. _error->Fatal(_("Dynamic MMap ran out of room. Please increase the size "
  311. "of APT::Cache-Start. Current value: %lu. (man 5 apt.conf)"), WorkSpace);
  312. return 0;
  313. }
  314. }
  315. return Result;
  316. }
  317. /*}}}*/
  318. // DynamicMMap::Allocate - Pooled aligned allocation /*{{{*/
  319. // ---------------------------------------------------------------------
  320. /* This allocates an Item of size ItemSize so that it is aligned to its
  321. size in the file. */
  322. unsigned long DynamicMMap::Allocate(unsigned long ItemSize)
  323. {
  324. if (unlikely(ItemSize == 0))
  325. {
  326. _error->Fatal("Can't allocate an item of size zero");
  327. return 0;
  328. }
  329. // Look for a matching pool entry
  330. Pool *I;
  331. Pool *Empty = 0;
  332. for (I = Pools; I != Pools + PoolCount; ++I)
  333. {
  334. if (I->ItemSize == 0)
  335. Empty = I;
  336. if (I->ItemSize == ItemSize)
  337. break;
  338. }
  339. // No pool is allocated, use an unallocated one
  340. if (I == Pools + PoolCount)
  341. {
  342. // Woops, we ran out, the calling code should allocate more.
  343. if (Empty == 0)
  344. {
  345. _error->Error("Ran out of allocation pools");
  346. return 0;
  347. }
  348. I = Empty;
  349. I->ItemSize = ItemSize;
  350. I->Count = 0;
  351. }
  352. unsigned long Result = 0;
  353. // Out of space, allocate some more
  354. if (I->Count == 0)
  355. {
  356. const unsigned long size = 20*1024;
  357. I->Count = size/ItemSize;
  358. Pool* oldPools = Pools;
  359. _error->PushToStack();
  360. Result = RawAllocate(size,ItemSize);
  361. bool const newError = _error->PendingError();
  362. _error->MergeWithStack();
  363. if (Pools != oldPools)
  364. I += Pools - oldPools;
  365. // Does the allocation failed ?
  366. if (Result == 0 && newError)
  367. return 0;
  368. I->Start = Result;
  369. }
  370. else
  371. Result = I->Start;
  372. I->Count--;
  373. I->Start += ItemSize;
  374. return Result/ItemSize;
  375. }
  376. /*}}}*/
  377. // DynamicMMap::WriteString - Write a string to the file /*{{{*/
  378. // ---------------------------------------------------------------------
  379. /* Strings are aligned to 16 bytes */
  380. unsigned long DynamicMMap::WriteString(const char *String,
  381. unsigned long Len)
  382. {
  383. if (Len == (unsigned long)-1)
  384. Len = strlen(String);
  385. _error->PushToStack();
  386. unsigned long Result = RawAllocate(Len+1+sizeof(uint16_t),sizeof(uint16_t));
  387. bool const newError = _error->PendingError();
  388. _error->MergeWithStack();
  389. if (Base == NULL || (Result == 0 && newError))
  390. return 0;
  391. if (Len >= std::numeric_limits<uint16_t>::max())
  392. abort();
  393. uint16_t LenToWrite = Len;
  394. memcpy((char *)Base + Result, &LenToWrite, sizeof(LenToWrite));
  395. Result += + sizeof(LenToWrite);
  396. memcpy((char *)Base + Result,String,Len);
  397. ((char *)Base)[Result + Len] = 0;
  398. return Result;
  399. }
  400. /*}}}*/
  401. // DynamicMMap::Grow - Grow the mmap /*{{{*/
  402. // ---------------------------------------------------------------------
  403. /* This method is a wrapper around different methods to (try to) grow
  404. a mmap (or our char[]-fallback). Encounterable environments:
  405. 1. Moveable + !Fallback + linux -> mremap with MREMAP_MAYMOVE
  406. 2. Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
  407. 3. Moveable + Fallback -> realloc
  408. 4. !Moveable + !Fallback + linux -> mremap alone - which will fail in 99,9%
  409. 5. !Moveable + !Fallback + !linux -> not possible (forbidden by constructor)
  410. 6. !Moveable + Fallback -> not possible
  411. [ While Moveable and Fallback stands for the equally named flags and
  412. "linux" indicates a linux kernel instead of a freebsd kernel. ]
  413. So what you can see here is, that a MMAP which want to be growable need
  414. to be moveable to have a real chance but that this method will at least try
  415. the nearly impossible 4 to grow it before it finally give up: Never say never. */
  416. bool DynamicMMap::Grow() {
  417. if (Limit != 0 && WorkSpace >= Limit)
  418. return _error->Error(_("Unable to increase the size of the MMap as the "
  419. "limit of %lu bytes is already reached."), Limit);
  420. if (GrowFactor <= 0)
  421. return _error->Error(_("Unable to increase size of the MMap as automatic growing is disabled by user."));
  422. unsigned long long const newSize = WorkSpace + GrowFactor;
  423. if(Fd != 0) {
  424. Fd->Seek(newSize - 1);
  425. char C = 0;
  426. Fd->Write(&C,sizeof(C));
  427. }
  428. unsigned long const poolOffset = Pools - ((Pool*) Base);
  429. if ((Flags & Fallback) != Fallback) {
  430. #if defined(_POSIX_MAPPED_FILES) && defined(__linux__)
  431. #ifdef MREMAP_MAYMOVE
  432. if ((Flags & Moveable) == Moveable)
  433. Base = mremap(Base, WorkSpace, newSize, MREMAP_MAYMOVE);
  434. else
  435. #endif
  436. Base = mremap(Base, WorkSpace, newSize, 0);
  437. if(Base == MAP_FAILED)
  438. return false;
  439. #else
  440. return false;
  441. #endif
  442. } else {
  443. if ((Flags & Moveable) != Moveable)
  444. return false;
  445. auto Temp = realloc(Base, newSize);
  446. if (Temp == NULL)
  447. return false;
  448. else {
  449. Base = Temp;
  450. /* Set new memory to 0 */
  451. memset((char*)Base + WorkSpace, 0, newSize - WorkSpace);
  452. }
  453. }
  454. Pools =(Pool*) Base + poolOffset;
  455. WorkSpace = newSize;
  456. return true;
  457. }
  458. /*}}}*/