mf_iocache.cc 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684
  1. /* Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
  2. This program is free software; you can redistribute it and/or modify
  3. it under the terms of the GNU General Public License, version 2.0,
  4. as published by the Free Software Foundation.
  5. This program is also distributed with certain software (including
  6. but not limited to OpenSSL) that is licensed under separate terms,
  7. as designated in a particular file or component or in included license
  8. documentation. The authors of MySQL hereby grant you an additional
  9. permission to link the program and your derivative works with the
  10. separately licensed software that they have included with MySQL.
  11. Without limiting anything contained in the foregoing, this file,
  12. which is part of C Driver for MySQL (Connector/C), is also subject to the
  13. Universal FOSS Exception, version 1.0, a copy of which can be found at
  14. http://oss.oracle.com/licenses/universal-foss-exception.
  15. This program is distributed in the hope that it will be useful,
  16. but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. GNU General Public License, version 2.0, for more details.
  19. You should have received a copy of the GNU General Public License
  20. along with this program; if not, write to the Free Software
  21. Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
  22. /**
  23. @file mysys/mf_iocache.cc
  24. Cashing of files with only does (sequential) read or writes of fixed-
  25. length records. A read isn't allowed to go over file-length. A read is ok
  26. if it ends at file-length and next read can try to read after file-length
  27. (and get a EOF-error).
  28. Possibly use of asyncronic io.
  29. macros for read and writes for faster io.
  30. Used instead of FILE when reading or writing whole files.
  31. This code makes mf_rec_cache obsolete (currently only used by ISAM)
  32. One can change info->pos_in_file to a higher value to skip bytes in file if
  33. also info->read_pos is set to info->read_end.
  34. If called through open_cached_file(), then the temporary file will
  35. only be created if a write exeeds the file buffer or if one calls
  36. my_b_flush_io_cache().
  37. If one uses SEQ_READ_APPEND, then two buffers are allocated, one for
  38. reading and another for writing. Reads are first done from disk and
  39. then done from the write buffer. This is an efficient way to read
  40. from a log file when one is writing to it at the same time.
  41. For this to work, the file has to be opened in append mode!
  42. Note that when one uses SEQ_READ_APPEND, one MUST write using
  43. my_b_append ! This is needed because we need to lock the mutex
  44. every time we access the write buffer.
  45. TODO:
  46. When one SEQ_READ_APPEND and we are reading and writing at the same time,
  47. each time the write buffer gets full and it's written to disk, we will
  48. always do a disk read to read a part of the buffer from disk to the
  49. read buffer.
  50. This should be fixed so that when we do a my_b_flush_io_cache() and
  51. we have been reading the write buffer, we should transfer the rest of the
  52. write buffer to the read buffer before we start to reuse it.
  53. */
  54. #include <errno.h>
  55. #include <fcntl.h>
  56. #include <math.h>
  57. #include <stdarg.h>
  58. #include <stdio.h>
  59. #include <stdlib.h>
  60. #include <string.h>
  61. #include <sys/stat.h>
  62. #include <sys/types.h>
  63. #include <algorithm>
  64. #include "m_string.h"
  65. #include "my_byteorder.h"
  66. #include "my_compiler.h"
  67. #include "my_dbug.h"
  68. #include "my_inttypes.h"
  69. #include "my_io.h"
  70. #include "my_macros.h"
  71. #include "my_sys.h"
  72. #include "my_thread_local.h"
  73. #include "mysql/psi/mysql_cond.h"
  74. #include "mysql/psi/mysql_file.h"
  75. #include "mysql/psi/mysql_mutex.h"
  76. #include "mysql/psi/psi_base.h"
  77. #include "mysql/service_mysql_alloc.h"
  78. #include "mysys/mysys_priv.h"
  79. #include "thr_mutex.h"
  80. PSI_file_key key_file_io_cache;
  81. #ifndef DBUG_OFF
  82. bool binlog_cache_temporary_file_is_encrypted = false;
  83. #endif
  84. #define lock_append_buffer(info) mysql_mutex_lock(&(info)->append_buffer_lock)
  85. #define unlock_append_buffer(info) \
  86. mysql_mutex_unlock(&(info)->append_buffer_lock)
  87. #define IO_ROUND_UP(X) (((X) + IO_SIZE - 1) & ~(IO_SIZE - 1))
  88. #define IO_ROUND_DN(X) ((X) & ~(IO_SIZE - 1))
  89. /*
  90. Setup internal pointers inside IO_CACHE
  91. SYNOPSIS
  92. setup_io_cache()
  93. info IO_CACHE handler
  94. NOTES
  95. This is called on automaticly on init or reinit of IO_CACHE
  96. It must be called externally if one moves or copies an IO_CACHE
  97. object.
  98. */
  99. void setup_io_cache(IO_CACHE *info) {
  100. /* Ensure that my_b_tell() and my_b_bytes_in_cache works */
  101. if (info->type == WRITE_CACHE) {
  102. info->current_pos = &info->write_pos;
  103. info->current_end = &info->write_end;
  104. } else {
  105. info->current_pos = &info->read_pos;
  106. info->current_end = &info->read_end;
  107. }
  108. }
  109. static void init_functions(IO_CACHE *info) {
  110. enum cache_type type = info->type;
  111. switch (type) {
  112. case READ_NET:
  113. /*
  114. Must be initialized by the caller. The problem is that
  115. _my_b_net_read has to be defined in sql directory because of
  116. the dependency on THD, and therefore cannot be visible to
  117. programs that link against mysys but know nothing about THD, such
  118. as myisamchk
  119. */
  120. break;
  121. case SEQ_READ_APPEND:
  122. info->read_function = _my_b_seq_read;
  123. info->write_function = 0; /* Force a core if used */
  124. break;
  125. default:
  126. info->read_function = info->share ? _my_b_read_r : _my_b_read;
  127. info->write_function = _my_b_write;
  128. }
  129. setup_io_cache(info);
  130. }
  131. /*
  132. Initialize an IO_CACHE object
  133. SYNOPSIS
  134. init_io_cache_ext()
  135. info cache handler to initialize
  136. file File that should be associated to to the handler
  137. If == -1 then real_open_cached_file()
  138. will be called when it's time to open file.
  139. cachesize Size of buffer to allocate for read/write
  140. If == 0 then use my_default_record_cache_size
  141. type Type of cache
  142. seek_offset Where cache should start reading/writing
  143. use_async_io Set to 1 of we should use async_io (if avaiable)
  144. cache_myflags Bitmap of different flags
  145. MY_WME | MY_FAE | MY_NABP | MY_FNABP |
  146. MY_DONT_CHECK_FILESIZE
  147. file_key Instrumented file key for temporary cache file
  148. RETURN
  149. 0 ok
  150. # error
  151. */
  152. int init_io_cache_ext(IO_CACHE *info, File file, size_t cachesize,
  153. enum cache_type type, my_off_t seek_offset,
  154. bool use_async_io, myf cache_myflags,
  155. PSI_file_key file_key) {
  156. size_t min_cache;
  157. my_off_t pos;
  158. my_off_t end_of_file = ~(my_off_t)0;
  159. DBUG_ENTER("init_io_cache_ext");
  160. DBUG_PRINT("enter", ("cache: %p type: %d pos: %ld", info, (int)type,
  161. (ulong)seek_offset));
  162. DBUG_EXECUTE_IF("simulate_init_io_cache_failure", DBUG_RETURN(1););
  163. info->file = file;
  164. info->file_key = file_key;
  165. info->type = TYPE_NOT_SET; /* Don't set it until mutex are created */
  166. info->pos_in_file = seek_offset;
  167. info->pre_close = info->pre_read = info->post_read = 0;
  168. info->arg = 0;
  169. info->alloced_buffer = 0;
  170. info->buffer = 0;
  171. info->seek_not_done = false;
  172. if (file >= 0) {
  173. pos = mysql_file_tell(file, MYF(0));
  174. if ((pos == (my_off_t)-1) && (my_errno() == ESPIPE)) {
  175. /*
  176. This kind of object doesn't support seek() or tell(). Don't set a
  177. flag that will make us again try to seek() later and fail.
  178. */
  179. info->seek_not_done = false;
  180. /*
  181. Additionally, if we're supposed to start somewhere other than the
  182. the beginning of whatever this file is, then somebody made a bad
  183. assumption.
  184. */
  185. DBUG_ASSERT(seek_offset == 0);
  186. } else
  187. info->seek_not_done = (seek_offset != pos);
  188. }
  189. info->disk_writes = 0;
  190. info->share = 0;
  191. if (!cachesize && !(cachesize = my_default_record_cache_size))
  192. DBUG_RETURN(1); /* No cache requested */
  193. min_cache = use_async_io ? IO_SIZE * 4 : IO_SIZE * 2;
  194. if (type == READ_CACHE ||
  195. type == SEQ_READ_APPEND) { /* Assume file isn't growing */
  196. if (!(cache_myflags & MY_DONT_CHECK_FILESIZE)) {
  197. /* Calculate end of file to avoid allocating oversized buffers */
  198. end_of_file = mysql_encryption_file_seek(info, 0L, MY_SEEK_END, MYF(0));
  199. /* Need to reset seek_not_done now that we just did a seek. */
  200. info->seek_not_done = !(end_of_file == seek_offset);
  201. if (end_of_file < seek_offset) end_of_file = seek_offset;
  202. /* Trim cache size if the file is very small */
  203. if ((my_off_t)cachesize > end_of_file - seek_offset + IO_SIZE * 2 - 1) {
  204. cachesize = (size_t)(end_of_file - seek_offset) + IO_SIZE * 2 - 1;
  205. use_async_io = 0; /* No need to use async */
  206. }
  207. }
  208. }
  209. cache_myflags &= ~MY_DONT_CHECK_FILESIZE;
  210. if (type != READ_NET && type != WRITE_NET) {
  211. /* Retry allocating memory in smaller blocks until we get one */
  212. cachesize = ((cachesize + min_cache - 1) & ~(min_cache - 1));
  213. for (;;) {
  214. size_t buffer_block;
  215. /*
  216. Unset MY_WAIT_IF_FULL bit if it is set, to prevent conflict with
  217. MY_ZEROFILL.
  218. */
  219. myf flags = (myf)(cache_myflags & ~(MY_WME | MY_WAIT_IF_FULL));
  220. if (cachesize < min_cache) cachesize = min_cache;
  221. buffer_block = cachesize;
  222. if (type == SEQ_READ_APPEND) buffer_block *= 2;
  223. if (cachesize == min_cache) flags |= (myf)MY_WME;
  224. if ((info->buffer = (uchar *)my_malloc(key_memory_IO_CACHE, buffer_block,
  225. flags)) != 0) {
  226. info->write_buffer = info->buffer;
  227. if (type == SEQ_READ_APPEND)
  228. info->write_buffer = info->buffer + cachesize;
  229. info->alloced_buffer = 1;
  230. break; /* Enough memory found */
  231. }
  232. if (cachesize == min_cache) DBUG_RETURN(2); /* Can't alloc cache */
  233. /* Try with less memory */
  234. cachesize = (cachesize * 3 / 4 & ~(min_cache - 1));
  235. }
  236. }
  237. DBUG_PRINT("info", ("init_io_cache: cachesize = %lu", (ulong)cachesize));
  238. info->read_length = info->buffer_length = cachesize;
  239. info->myflags = cache_myflags & ~(MY_NABP | MY_FNABP);
  240. info->request_pos = info->read_pos = info->write_pos = info->buffer;
  241. if (type == SEQ_READ_APPEND) {
  242. info->append_read_pos = info->write_pos = info->write_buffer;
  243. info->write_end = info->write_buffer + info->buffer_length;
  244. mysql_mutex_init(key_IO_CACHE_append_buffer_lock, &info->append_buffer_lock,
  245. MY_MUTEX_INIT_FAST);
  246. }
  247. #if defined(SAFE_MUTEX)
  248. else {
  249. /* Clear mutex so that safe_mutex will notice that it's not initialized */
  250. new (&info->append_buffer_lock) mysql_mutex_t();
  251. }
  252. #endif
  253. if (type == WRITE_CACHE)
  254. info->write_end =
  255. info->buffer + info->buffer_length - (seek_offset & (IO_SIZE - 1));
  256. else
  257. info->read_end = info->buffer; /* Nothing in cache */
  258. /* End_of_file may be changed by user later */
  259. info->end_of_file = end_of_file;
  260. info->error = 0;
  261. info->type = type;
  262. init_functions(info);
  263. DBUG_RETURN(0);
  264. } /* init_io_cache_ext */
  265. /*
  266. Initialize an IO_CACHE object
  267. SYNOPSIS
  268. init_io_cache() - Wrapper for init_io_cache_ext()
  269. NOTE
  270. This function should be used if the IO_CACHE tempfile is not instrumented.
  271. */
  272. int init_io_cache(IO_CACHE *info, File file, size_t cachesize,
  273. enum cache_type type, my_off_t seek_offset, bool use_async_io,
  274. myf cache_myflags) {
  275. return init_io_cache_ext(info, file, cachesize, type, seek_offset,
  276. use_async_io, cache_myflags, key_file_io_cache);
  277. }
  278. /*
  279. Use this to reset cache to re-start reading or to change the type
  280. between READ_CACHE <-> WRITE_CACHE
  281. If we are doing a reinit of a cache where we have the start of the file
  282. in the cache, we are reusing this memory without flushing it to disk.
  283. */
  284. bool reinit_io_cache(IO_CACHE *info, enum cache_type type, my_off_t seek_offset,
  285. bool use_async_io MY_ATTRIBUTE((unused)),
  286. bool clear_cache) {
  287. DBUG_ENTER("reinit_io_cache");
  288. DBUG_PRINT("enter", ("cache: %p type: %d seek_offset: %lu clear_cache: %d",
  289. info, type, (ulong)seek_offset, (int)clear_cache));
  290. /* One can't do reinit with the following types */
  291. DBUG_ASSERT(type != READ_NET && info->type != READ_NET && type != WRITE_NET &&
  292. info->type != WRITE_NET && type != SEQ_READ_APPEND &&
  293. info->type != SEQ_READ_APPEND);
  294. /* If the whole file is in memory, avoid flushing to disk */
  295. if (!clear_cache && seek_offset >= info->pos_in_file &&
  296. seek_offset <= my_b_tell(info)) {
  297. /* Reuse current buffer without flushing it to disk */
  298. uchar *pos;
  299. if (info->type == WRITE_CACHE && type == READ_CACHE) {
  300. info->read_end = info->write_pos;
  301. info->end_of_file = my_b_tell(info);
  302. /*
  303. Trigger a new seek only if we have a valid
  304. file handle.
  305. */
  306. info->seek_not_done = (info->file != -1);
  307. } else if (type == WRITE_CACHE) {
  308. if (info->type == READ_CACHE) {
  309. info->write_end = info->write_buffer + info->buffer_length;
  310. info->seek_not_done = true;
  311. }
  312. info->end_of_file = ~(my_off_t)0;
  313. }
  314. pos = info->request_pos + (seek_offset - info->pos_in_file);
  315. if (type == WRITE_CACHE)
  316. info->write_pos = pos;
  317. else
  318. info->read_pos = pos;
  319. } else {
  320. /*
  321. If we change from WRITE_CACHE to READ_CACHE, assume that everything
  322. after the current positions should be ignored
  323. */
  324. if (info->type == WRITE_CACHE && type == READ_CACHE)
  325. info->end_of_file = my_b_tell(info);
  326. /* flush cache if we want to reuse it */
  327. if (!clear_cache && my_b_flush_io_cache(info, 1)) DBUG_RETURN(1);
  328. info->pos_in_file = seek_offset;
  329. /* Better to do always do a seek */
  330. info->seek_not_done = true;
  331. info->request_pos = info->read_pos = info->write_pos = info->buffer;
  332. if (type == READ_CACHE) {
  333. info->read_end = info->buffer; /* Nothing in cache */
  334. } else {
  335. info->write_end =
  336. (info->buffer + info->buffer_length - (seek_offset & (IO_SIZE - 1)));
  337. info->end_of_file = ~(my_off_t)0;
  338. }
  339. }
  340. info->type = type;
  341. info->error = 0;
  342. init_functions(info);
  343. DBUG_RETURN(0);
  344. } /* reinit_io_cache */
  345. /*
  346. Read buffered.
  347. SYNOPSIS
  348. _my_b_read()
  349. info IO_CACHE pointer
  350. Buffer Buffer to retrieve count bytes from file
  351. Count Number of bytes to read into Buffer
  352. NOTE
  353. This function is only called from the my_b_read() macro when there
  354. isn't enough characters in the buffer to satisfy the request.
  355. WARNING
  356. When changing this function, be careful with handling file offsets
  357. (end-of_file, pos_in_file). Do not cast them to possibly smaller
  358. types than my_off_t unless you can be sure that their value fits.
  359. Same applies to differences of file offsets.
  360. When changing this function, check _my_b_read_r(). It might need the
  361. same change.
  362. RETURN
  363. 0 we succeeded in reading all data
  364. 1 Error: couldn't read requested characters. In this case:
  365. If info->error == -1, we got a read error.
  366. Otherwise info->error contains the number of bytes in Buffer.
  367. */
  368. int _my_b_read(IO_CACHE *info, uchar *Buffer, size_t Count) {
  369. size_t length, diff_length, left_length, max_length;
  370. my_off_t pos_in_file;
  371. DBUG_ENTER("_my_b_read");
  372. /* If the buffer is not empty yet, copy what is available. */
  373. if ((left_length = (size_t)(info->read_end - info->read_pos))) {
  374. DBUG_ASSERT(Count >= left_length); /* User is not using my_b_read() */
  375. memcpy(Buffer, info->read_pos, left_length);
  376. Buffer += left_length;
  377. Count -= left_length;
  378. }
  379. /* pos_in_file always point on where info->buffer was read */
  380. pos_in_file = info->pos_in_file + (size_t)(info->read_end - info->buffer);
  381. /*
  382. Whenever a function which operates on IO_CACHE flushes/writes
  383. some part of the IO_CACHE to disk it will set the property
  384. "seek_not_done" to indicate this to other functions operating
  385. on the IO_CACHE.
  386. */
  387. if (info->seek_not_done) {
  388. if ((mysql_encryption_file_seek(info, pos_in_file, MY_SEEK_SET, MYF(0)) !=
  389. MY_FILEPOS_ERROR)) {
  390. /* No error, reset seek_not_done flag. */
  391. info->seek_not_done = false;
  392. } else {
  393. /*
  394. If the seek failed and the error number is ESPIPE, it is because
  395. info->file is a pipe or socket or FIFO. We never should have tried
  396. to seek on that. See Bugs#25807 and #22828 for more info.
  397. */
  398. DBUG_ASSERT(my_errno() != ESPIPE);
  399. info->error = -1;
  400. DBUG_RETURN(1);
  401. }
  402. }
  403. /*
  404. Calculate, how much we are within a IO_SIZE block. Ideally this
  405. should be zero.
  406. */
  407. diff_length = (size_t)(pos_in_file & (IO_SIZE - 1));
  408. /*
  409. If more than a block plus the rest of the current block is wanted,
  410. we do read directly, without filling the buffer.
  411. */
  412. if (Count >=
  413. (size_t)(IO_SIZE +
  414. (IO_SIZE - diff_length))) { /* Fill first intern buffer */
  415. size_t read_length;
  416. if (info->end_of_file <= pos_in_file) {
  417. /* End of file. Return, what we did copy from the buffer. */
  418. info->error = (int)left_length;
  419. DBUG_RETURN(1);
  420. }
  421. /*
  422. Crop the wanted count to a multiple of IO_SIZE and subtract,
  423. what we did already read from a block. That way, the read will
  424. end aligned with a block.
  425. */
  426. length = (Count & (size_t) ~(IO_SIZE - 1)) - diff_length;
  427. if ((read_length = mysql_encryption_file_read(info, Buffer, length,
  428. info->myflags)) != length) {
  429. /*
  430. If we didn't get, what we wanted, we either return -1 for a read
  431. error, or (it's end of file), how much we got in total.
  432. */
  433. info->error =
  434. (read_length == (size_t)-1 ? -1 : (int)(read_length + left_length));
  435. DBUG_RETURN(1);
  436. }
  437. Count -= length;
  438. Buffer += length;
  439. pos_in_file += length;
  440. left_length += length;
  441. diff_length = 0;
  442. }
  443. /*
  444. At this point, we want less than one and a partial block.
  445. We will read a full cache, minus the number of bytes, we are
  446. within a block already. So we will reach new alignment.
  447. */
  448. max_length = info->read_length - diff_length;
  449. /* We will not read past end of file. */
  450. if (info->type != READ_FIFO && max_length > (info->end_of_file - pos_in_file))
  451. max_length = (size_t)(info->end_of_file - pos_in_file);
  452. /*
  453. If there is nothing left to read,
  454. we either are done, or we failed to fulfill the request.
  455. Otherwise, we read max_length into the cache.
  456. */
  457. if (!max_length) {
  458. if (Count) {
  459. /* We couldn't fulfil the request. Return, how much we got. */
  460. info->error = (int)left_length;
  461. DBUG_RETURN(1);
  462. }
  463. length = 0; /* Didn't read any chars */
  464. } else if ((length = mysql_encryption_file_read(
  465. info, info->buffer, max_length, info->myflags)) < Count ||
  466. length == (size_t)-1) {
  467. /*
  468. We got an read error, or less than requested (end of file).
  469. If not a read error, copy, what we got.
  470. */
  471. if (length != (size_t)-1) memcpy(Buffer, info->buffer, length);
  472. info->pos_in_file = pos_in_file;
  473. /* For a read error, return -1, otherwise, what we got in total. */
  474. info->error = length == (size_t)-1 ? -1 : (int)(length + left_length);
  475. info->read_pos = info->read_end = info->buffer;
  476. DBUG_RETURN(1);
  477. }
  478. /*
  479. Count is the remaining number of bytes requested.
  480. length is the amount of data in the cache.
  481. Read Count bytes from the cache.
  482. */
  483. info->read_pos = info->buffer + Count;
  484. info->read_end = info->buffer + length;
  485. info->pos_in_file = pos_in_file;
  486. memcpy(Buffer, info->buffer, Count);
  487. DBUG_RETURN(0);
  488. }
  489. /*
  490. Prepare IO_CACHE for shared use.
  491. SYNOPSIS
  492. init_io_cache_share()
  493. read_cache A read cache. This will be copied for
  494. every thread after setup.
  495. cshare The share.
  496. write_cache If non-NULL a write cache that is to be
  497. synchronized with the read caches.
  498. num_threads Number of threads sharing the cache
  499. including the write thread if any.
  500. DESCRIPTION
  501. The shared cache is used so: One IO_CACHE is initialized with
  502. init_io_cache(). This includes the allocation of a buffer. Then a
  503. share is allocated and init_io_cache_share() is called with the io
  504. cache and the share. Then the io cache is copied for each thread. So
  505. every thread has its own copy of IO_CACHE. But the allocated buffer
  506. is shared because cache->buffer is the same for all caches.
  507. One thread reads data from the file into the buffer. All threads
  508. read from the buffer, but every thread maintains its own set of
  509. pointers into the buffer. When all threads have used up the buffer
  510. contents, one of the threads reads the next block of data into the
  511. buffer. To accomplish this, each thread enters the cache lock before
  512. accessing the buffer. They wait in lock_io_cache() until all threads
  513. joined the lock. The last thread entering the lock is in charge of
  514. reading from file to buffer. It wakes all threads when done.
  515. Synchronizing a write cache to the read caches works so: Whenever
  516. the write buffer needs a flush, the write thread enters the lock and
  517. waits for all other threads to enter the lock too. They do this when
  518. they have used up the read buffer. When all threads are in the lock,
  519. the write thread copies the write buffer to the read buffer and
  520. wakes all threads.
  521. share->running_threads is the number of threads not being in the
  522. cache lock. When entering lock_io_cache() the number is decreased.
  523. When the thread that fills the buffer enters unlock_io_cache() the
  524. number is reset to the number of threads. The condition
  525. running_threads == 0 means that all threads are in the lock. Bumping
  526. up the number to the full count is non-intuitive. But increasing the
  527. number by one for each thread that leaves the lock could lead to a
  528. solo run of one thread. The last thread to join a lock reads from
  529. file to buffer, wakes the other threads, processes the data in the
  530. cache and enters the lock again. If no other thread left the lock
  531. meanwhile, it would think it's the last one again and read the next
  532. block...
  533. The share has copies of 'error', 'buffer', 'read_end', and
  534. 'pos_in_file' from the thread that filled the buffer. We may not be
  535. able to access this information directly from its cache because the
  536. thread may be removed from the share before the variables could be
  537. copied by all other threads. Or, if a write buffer is synchronized,
  538. it would change its 'pos_in_file' after waking the other threads,
  539. possibly before they could copy its value.
  540. However, the 'buffer' variable in the share is for a synchronized
  541. write cache. It needs to know where to put the data. Otherwise it
  542. would need access to the read cache of one of the threads that is
  543. not yet removed from the share.
  544. RETURN
  545. void
  546. */
  547. void init_io_cache_share(IO_CACHE *read_cache, IO_CACHE_SHARE *cshare,
  548. IO_CACHE *write_cache, uint num_threads) {
  549. DBUG_ENTER("init_io_cache_share");
  550. DBUG_PRINT("io_cache_share", ("read_cache: %p share: %p "
  551. "write_cache: %p threads: %u",
  552. read_cache, cshare, write_cache, num_threads));
  553. DBUG_ASSERT(num_threads > 1);
  554. DBUG_ASSERT(read_cache->type == READ_CACHE);
  555. DBUG_ASSERT(!write_cache || (write_cache->type == WRITE_CACHE));
  556. mysql_mutex_init(key_IO_CACHE_SHARE_mutex, &cshare->mutex,
  557. MY_MUTEX_INIT_FAST);
  558. mysql_cond_init(key_IO_CACHE_SHARE_cond, &cshare->cond);
  559. mysql_cond_init(key_IO_CACHE_SHARE_cond_writer, &cshare->cond_writer);
  560. cshare->running_threads = num_threads;
  561. cshare->total_threads = num_threads;
  562. cshare->error = 0; /* Initialize. */
  563. cshare->buffer = read_cache->buffer;
  564. cshare->read_end = NULL; /* See function comment of lock_io_cache(). */
  565. cshare->pos_in_file = 0; /* See function comment of lock_io_cache(). */
  566. cshare->source_cache = write_cache; /* Can be NULL. */
  567. read_cache->share = cshare;
  568. read_cache->read_function = _my_b_read_r;
  569. read_cache->current_pos = NULL;
  570. read_cache->current_end = NULL;
  571. if (write_cache) write_cache->share = cshare;
  572. DBUG_VOID_RETURN;
  573. }
  574. /*
  575. Remove a thread from shared access to IO_CACHE.
  576. SYNOPSIS
  577. remove_io_thread()
  578. cache The IO_CACHE to be removed from the share.
  579. NOTE
  580. Every thread must do that on exit for not to deadlock other threads.
  581. The last thread destroys the pthread resources.
  582. A writer flushes its cache first.
  583. RETURN
  584. void
  585. */
  586. void remove_io_thread(IO_CACHE *cache) {
  587. IO_CACHE_SHARE *cshare = cache->share;
  588. uint total;
  589. DBUG_ENTER("remove_io_thread");
  590. /* If the writer goes, it needs to flush the write cache. */
  591. if (cache == cshare->source_cache) flush_io_cache(cache);
  592. mysql_mutex_lock(&cshare->mutex);
  593. DBUG_PRINT(
  594. "io_cache_share",
  595. ("%s: %p", (cache == cshare->source_cache) ? "writer" : "reader", cache));
  596. /* Remove from share. */
  597. total = --cshare->total_threads;
  598. DBUG_PRINT("io_cache_share", ("remaining threads: %u", total));
  599. /* Detach from share. */
  600. cache->share = NULL;
  601. /* If the writer goes, let the readers know. */
  602. if (cache == cshare->source_cache) {
  603. DBUG_PRINT("io_cache_share", ("writer leaves"));
  604. cshare->source_cache = NULL;
  605. }
  606. /* If all threads are waiting for me to join the lock, wake them. */
  607. if (!--cshare->running_threads) {
  608. DBUG_PRINT("io_cache_share", ("the last running thread leaves, wake all"));
  609. mysql_cond_signal(&cshare->cond_writer);
  610. mysql_cond_broadcast(&cshare->cond);
  611. }
  612. mysql_mutex_unlock(&cshare->mutex);
  613. if (!total) {
  614. DBUG_PRINT("io_cache_share", ("last thread removed, destroy share"));
  615. mysql_cond_destroy(&cshare->cond_writer);
  616. mysql_cond_destroy(&cshare->cond);
  617. mysql_mutex_destroy(&cshare->mutex);
  618. }
  619. DBUG_VOID_RETURN;
  620. }
  621. /*
  622. Lock IO cache and wait for all other threads to join.
  623. SYNOPSIS
  624. lock_io_cache()
  625. cache The cache of the thread entering the lock.
  626. pos File position of the block to read.
  627. Unused for the write thread.
  628. DESCRIPTION
  629. Wait for all threads to finish with the current buffer. We want
  630. all threads to proceed in concert. The last thread to join
  631. lock_io_cache() will read the block from file and all threads start
  632. to use it. Then they will join again for reading the next block.
  633. The waiting threads detect a fresh buffer by comparing
  634. cshare->pos_in_file with the position they want to process next.
  635. Since the first block may start at position 0, we take
  636. cshare->read_end as an additional condition. This variable is
  637. initialized to NULL and will be set after a block of data is written
  638. to the buffer.
  639. RETURN
  640. 1 OK, lock in place, go ahead and read.
  641. 0 OK, unlocked, another thread did the read.
  642. */
  643. static int lock_io_cache(IO_CACHE *cache, my_off_t pos) {
  644. IO_CACHE_SHARE *cshare = cache->share;
  645. DBUG_ENTER("lock_io_cache");
  646. /* Enter the lock. */
  647. mysql_mutex_lock(&cshare->mutex);
  648. cshare->running_threads--;
  649. DBUG_PRINT("io_cache_share",
  650. ("%s: %p pos: %lu running: %u",
  651. (cache == cshare->source_cache) ? "writer" : "reader", cache,
  652. (ulong)pos, cshare->running_threads));
  653. if (cshare->source_cache) {
  654. /* A write cache is synchronized to the read caches. */
  655. if (cache == cshare->source_cache) {
  656. /* The writer waits until all readers are here. */
  657. while (cshare->running_threads) {
  658. DBUG_PRINT("io_cache_share", ("writer waits in lock"));
  659. mysql_cond_wait(&cshare->cond_writer, &cshare->mutex);
  660. }
  661. DBUG_PRINT("io_cache_share", ("writer awoke, going to copy"));
  662. /* Stay locked. Leave the lock later by unlock_io_cache(). */
  663. DBUG_RETURN(1);
  664. }
  665. /* The last thread wakes the writer. */
  666. if (!cshare->running_threads) {
  667. DBUG_PRINT("io_cache_share", ("waking writer"));
  668. mysql_cond_signal(&cshare->cond_writer);
  669. }
  670. /*
  671. Readers wait until the data is copied from the writer. Another
  672. reason to stop waiting is the removal of the write thread. If this
  673. happens, we leave the lock with old data in the buffer.
  674. */
  675. while ((!cshare->read_end || (cshare->pos_in_file < pos)) &&
  676. cshare->source_cache) {
  677. DBUG_PRINT("io_cache_share", ("reader waits in lock"));
  678. mysql_cond_wait(&cshare->cond, &cshare->mutex);
  679. }
  680. /*
  681. If the writer was removed from the share while this thread was
  682. asleep, we need to simulate an EOF condition. The writer cannot
  683. reset the share variables as they might still be in use by readers
  684. of the last block. When we awake here then because the last
  685. joining thread signalled us. If the writer is not the last, it
  686. will not signal. So it is safe to clear the buffer here.
  687. */
  688. if (!cshare->read_end || (cshare->pos_in_file < pos)) {
  689. DBUG_PRINT("io_cache_share", ("reader found writer removed. EOF"));
  690. cshare->read_end = cshare->buffer; /* Empty buffer. */
  691. cshare->error = 0; /* EOF is not an error. */
  692. }
  693. } else {
  694. /*
  695. There are read caches only. The last thread arriving in
  696. lock_io_cache() continues with a locked cache and reads the block.
  697. */
  698. if (!cshare->running_threads) {
  699. DBUG_PRINT("io_cache_share", ("last thread joined, going to read"));
  700. /* Stay locked. Leave the lock later by unlock_io_cache(). */
  701. DBUG_RETURN(1);
  702. }
  703. /*
  704. All other threads wait until the requested block is read by the
  705. last thread arriving. Another reason to stop waiting is the
  706. removal of a thread. If this leads to all threads being in the
  707. lock, we have to continue also. The first of the awaken threads
  708. will then do the read.
  709. */
  710. while ((!cshare->read_end || (cshare->pos_in_file < pos)) &&
  711. cshare->running_threads) {
  712. DBUG_PRINT("io_cache_share", ("reader waits in lock"));
  713. mysql_cond_wait(&cshare->cond, &cshare->mutex);
  714. }
  715. /* If the block is not yet read, continue with a locked cache and read. */
  716. if (!cshare->read_end || (cshare->pos_in_file < pos)) {
  717. DBUG_PRINT("io_cache_share", ("reader awoke, going to read"));
  718. /* Stay locked. Leave the lock later by unlock_io_cache(). */
  719. DBUG_RETURN(1);
  720. }
  721. /* Another thread did read the block already. */
  722. }
  723. DBUG_PRINT(
  724. "io_cache_share",
  725. ("reader awoke, going to process %u bytes",
  726. (uint)(cshare->read_end ? (size_t)(cshare->read_end - cshare->buffer)
  727. : 0)));
  728. /*
  729. Leave the lock. Do not call unlock_io_cache() later. The thread that
  730. filled the buffer did this and marked all threads as running.
  731. */
  732. mysql_mutex_unlock(&cshare->mutex);
  733. DBUG_RETURN(0);
  734. }
  735. /*
  736. Unlock IO cache.
  737. SYNOPSIS
  738. unlock_io_cache()
  739. cache The cache of the thread leaving the lock.
  740. NOTE
  741. This is called by the thread that filled the buffer. It marks all
  742. threads as running and awakes them. This must not be done by any
  743. other thread.
  744. Do not signal cond_writer. Either there is no writer or the writer
  745. is the only one who can call this function.
  746. The reason for resetting running_threads to total_threads before
  747. waking all other threads is that it could be possible that this
  748. thread is so fast with processing the buffer that it enters the lock
  749. before even one other thread has left it. If every awoken thread
  750. would increase running_threads by one, this thread could think that
  751. he is again the last to join and would not wait for the other
  752. threads to process the data.
  753. RETURN
  754. void
  755. */
  756. static void unlock_io_cache(IO_CACHE *cache) {
  757. IO_CACHE_SHARE *cshare = cache->share;
  758. DBUG_ENTER("unlock_io_cache");
  759. DBUG_PRINT("io_cache_share",
  760. ("%s: %p pos: %lu running: %u",
  761. (cache == cshare->source_cache) ? "writer" : "reader", cache,
  762. (ulong)cshare->pos_in_file, cshare->total_threads));
  763. cshare->running_threads = cshare->total_threads;
  764. mysql_cond_broadcast(&cshare->cond);
  765. mysql_mutex_unlock(&cshare->mutex);
  766. DBUG_VOID_RETURN;
  767. }
  768. /*
  769. Read from IO_CACHE when it is shared between several threads.
  770. SYNOPSIS
  771. _my_b_read_r()
  772. cache IO_CACHE pointer
  773. Buffer Buffer to retrieve count bytes from file
  774. Count Number of bytes to read into Buffer
  775. NOTE
  776. This function is only called from the my_b_read() macro when there
  777. isn't enough characters in the buffer to satisfy the request.
  778. IMPLEMENTATION
  779. It works as follows: when a thread tries to read from a file (that
  780. is, after using all the data from the (shared) buffer), it just
  781. hangs on lock_io_cache(), waiting for other threads. When the very
  782. last thread attempts a read, lock_io_cache() returns 1, the thread
  783. does actual IO and unlock_io_cache(), which signals all the waiting
  784. threads that data is in the buffer.
  785. WARNING
  786. When changing this function, be careful with handling file offsets
  787. (end-of_file, pos_in_file). Do not cast them to possibly smaller
  788. types than my_off_t unless you can be sure that their value fits.
  789. Same applies to differences of file offsets. (Bug #11527)
  790. When changing this function, check _my_b_read(). It might need the
  791. same change.
  792. RETURN
  793. 0 we succeeded in reading all data
  794. 1 Error: can't read requested characters
  795. */
  796. int _my_b_read_r(IO_CACHE *cache, uchar *Buffer, size_t Count) {
  797. my_off_t pos_in_file;
  798. size_t length, diff_length, left_length;
  799. IO_CACHE_SHARE *cshare = cache->share;
  800. DBUG_ENTER("_my_b_read_r");
  801. if ((left_length = (size_t)(cache->read_end - cache->read_pos))) {
  802. DBUG_ASSERT(Count >= left_length); /* User is not using my_b_read() */
  803. memcpy(Buffer, cache->read_pos, left_length);
  804. Buffer += left_length;
  805. Count -= left_length;
  806. }
  807. while (Count) {
  808. size_t cnt, len;
  809. pos_in_file = cache->pos_in_file + (cache->read_end - cache->buffer);
  810. diff_length = (size_t)(pos_in_file & (IO_SIZE - 1));
  811. length = IO_ROUND_UP(Count + diff_length) - diff_length;
  812. length = ((length <= cache->read_length)
  813. ? length + IO_ROUND_DN(cache->read_length - length)
  814. : length - IO_ROUND_UP(length - cache->read_length));
  815. if (cache->type != READ_FIFO &&
  816. (length > (cache->end_of_file - pos_in_file)))
  817. length = (size_t)(cache->end_of_file - pos_in_file);
  818. if (length == 0) {
  819. cache->error = (int)left_length;
  820. DBUG_RETURN(1);
  821. }
  822. if (lock_io_cache(cache, pos_in_file)) {
  823. /* With a synchronized write/read cache we won't come here... */
  824. DBUG_ASSERT(!cshare->source_cache);
  825. /*
  826. ... unless the writer has gone before this thread entered the
  827. lock. Simulate EOF in this case. It can be distinguished by
  828. cache->file.
  829. */
  830. if (cache->file < 0)
  831. len = 0;
  832. else {
  833. /*
  834. Whenever a function which operates on IO_CACHE flushes/writes
  835. some part of the IO_CACHE to disk it will set the property
  836. "seek_not_done" to indicate this to other functions operating
  837. on the IO_CACHE.
  838. */
  839. if (cache->seek_not_done) {
  840. if (mysql_encryption_file_seek(cache, pos_in_file, MY_SEEK_SET,
  841. MYF(0)) == MY_FILEPOS_ERROR) {
  842. cache->error = -1;
  843. unlock_io_cache(cache);
  844. DBUG_RETURN(1);
  845. }
  846. }
  847. len = mysql_encryption_file_read(cache, cache->buffer, length,
  848. cache->myflags);
  849. }
  850. DBUG_PRINT("io_cache_share", ("read %lu bytes", (ulong)len));
  851. cache->read_end = cache->buffer + (len == (size_t)-1 ? 0 : len);
  852. cache->error = (len == length ? 0 : (int)len);
  853. cache->pos_in_file = pos_in_file;
  854. /* Copy important values to the share. */
  855. cshare->error = cache->error;
  856. cshare->read_end = cache->read_end;
  857. cshare->pos_in_file = pos_in_file;
  858. /* Mark all threads as running and wake them. */
  859. unlock_io_cache(cache);
  860. } else {
  861. /*
  862. With a synchronized write/read cache readers always come here.
  863. Copy important values from the share.
  864. */
  865. cache->error = cshare->error;
  866. cache->read_end = cshare->read_end;
  867. cache->pos_in_file = cshare->pos_in_file;
  868. len = ((cache->error == -1) ? (size_t)-1
  869. : (size_t)(cache->read_end - cache->buffer));
  870. }
  871. cache->read_pos = cache->buffer;
  872. cache->seek_not_done = false;
  873. if (len == 0 || len == (size_t)-1) {
  874. DBUG_PRINT("io_cache_share", ("reader error. len %lu left %lu",
  875. (ulong)len, (ulong)left_length));
  876. cache->error = (int)left_length;
  877. DBUG_RETURN(1);
  878. }
  879. cnt = (len > Count) ? Count : len;
  880. memcpy(Buffer, cache->read_pos, cnt);
  881. Count -= cnt;
  882. Buffer += cnt;
  883. left_length += cnt;
  884. cache->read_pos += cnt;
  885. }
  886. DBUG_RETURN(0);
  887. }
  888. /*
  889. Copy data from write cache to read cache.
  890. SYNOPSIS
  891. copy_to_read_buffer()
  892. write_cache The write cache.
  893. write_buffer The source of data, mostly the cache buffer.
  894. write_length The number of bytes to copy.
  895. NOTE
  896. The write thread will wait for all read threads to join the cache
  897. lock. Then it copies the data over and wakes the read threads.
  898. RETURN
  899. void
  900. */
  901. static void copy_to_read_buffer(IO_CACHE *write_cache,
  902. const uchar *write_buffer,
  903. size_t write_length) {
  904. IO_CACHE_SHARE *cshare = write_cache->share;
  905. DBUG_ASSERT(cshare->source_cache == write_cache);
  906. /*
  907. write_length is usually less or equal to buffer_length.
  908. It can be bigger if _my_b_write() is called with a big length.
  909. */
  910. while (write_length) {
  911. size_t copy_length = MY_MIN(write_length, write_cache->buffer_length);
  912. int MY_ATTRIBUTE((unused)) rc;
  913. rc = lock_io_cache(write_cache, write_cache->pos_in_file);
  914. /* The writing thread does always have the lock when it awakes. */
  915. DBUG_ASSERT(rc);
  916. memcpy(cshare->buffer, write_buffer, copy_length);
  917. cshare->error = 0;
  918. cshare->read_end = cshare->buffer + copy_length;
  919. cshare->pos_in_file = write_cache->pos_in_file;
  920. /* Mark all threads as running and wake them. */
  921. unlock_io_cache(write_cache);
  922. write_buffer += copy_length;
  923. write_length -= copy_length;
  924. }
  925. }
  926. /*
  927. Do sequential read from the SEQ_READ_APPEND cache.
  928. We do this in three stages:
  929. - first read from info->buffer
  930. - then if there are still data to read, try the file descriptor
  931. - afterwards, if there are still data to read, try append buffer
  932. RETURNS
  933. 0 Success
  934. 1 Failed to read
  935. */
  936. int _my_b_seq_read(IO_CACHE *info, uchar *Buffer, size_t Count) {
  937. size_t length, diff_length, left_length, save_count, max_length;
  938. my_off_t pos_in_file;
  939. save_count = Count;
  940. /* first, read the regular buffer */
  941. if ((left_length = (size_t)(info->read_end - info->read_pos))) {
  942. DBUG_ASSERT(Count > left_length); /* User is not using my_b_read() */
  943. memcpy(Buffer, info->read_pos, left_length);
  944. Buffer += left_length;
  945. Count -= left_length;
  946. }
  947. lock_append_buffer(info);
  948. /* pos_in_file always point on where info->buffer was read */
  949. if ((pos_in_file =
  950. info->pos_in_file + (size_t)(info->read_end - info->buffer)) >=
  951. info->end_of_file)
  952. goto read_append_buffer;
  953. /*
  954. With read-append cache we must always do a seek before we read,
  955. because the write could have moved the file pointer astray
  956. */
  957. if (mysql_encryption_file_seek(info, pos_in_file, MY_SEEK_SET, MYF(0)) ==
  958. MY_FILEPOS_ERROR) {
  959. info->error = -1;
  960. unlock_append_buffer(info);
  961. return (1);
  962. }
  963. info->seek_not_done = false;
  964. diff_length = (size_t)(pos_in_file & (IO_SIZE - 1));
  965. /* now the second stage begins - read from file descriptor */
  966. if (Count >= (size_t)(IO_SIZE + (IO_SIZE - diff_length))) {
  967. /* Fill first intern buffer */
  968. size_t read_length;
  969. length = (Count & (size_t) ~(IO_SIZE - 1)) - diff_length;
  970. if ((read_length = mysql_encryption_file_read(
  971. info, Buffer, length, info->myflags)) == (size_t)-1) {
  972. info->error = -1;
  973. unlock_append_buffer(info);
  974. return 1;
  975. }
  976. Count -= read_length;
  977. Buffer += read_length;
  978. pos_in_file += read_length;
  979. if (read_length != length) {
  980. /*
  981. We only got part of data; Read the rest of the data from the
  982. write buffer
  983. */
  984. goto read_append_buffer;
  985. }
  986. left_length += length;
  987. diff_length = 0;
  988. }
  989. max_length = info->read_length - diff_length;
  990. if (max_length > (info->end_of_file - pos_in_file))
  991. max_length = (size_t)(info->end_of_file - pos_in_file);
  992. if (!max_length) {
  993. if (Count) goto read_append_buffer;
  994. length = 0; /* Didn't read any more chars */
  995. } else {
  996. length = mysql_encryption_file_read(info, info->buffer, max_length,
  997. info->myflags);
  998. if (length == (size_t)-1) {
  999. info->error = -1;
  1000. unlock_append_buffer(info);
  1001. return 1;
  1002. }
  1003. if (length < Count) {
  1004. memcpy(Buffer, info->buffer, length);
  1005. Count -= length;
  1006. Buffer += length;
  1007. /*
  1008. added the line below to make
  1009. DBUG_ASSERT(pos_in_file==info->end_of_file) pass.
  1010. otherwise this does not appear to be needed
  1011. */
  1012. pos_in_file += length;
  1013. goto read_append_buffer;
  1014. }
  1015. }
  1016. unlock_append_buffer(info);
  1017. info->read_pos = info->buffer + Count;
  1018. info->read_end = info->buffer + length;
  1019. info->pos_in_file = pos_in_file;
  1020. memcpy(Buffer, info->buffer, (size_t)Count);
  1021. return 0;
  1022. read_append_buffer:
  1023. /*
  1024. Read data from the current write buffer.
  1025. Count should never be == 0 here (The code will work even if count is 0)
  1026. */
  1027. {
  1028. /* First copy the data to Count */
  1029. size_t len_in_buff = (size_t)(info->write_pos - info->append_read_pos);
  1030. size_t copy_len;
  1031. size_t transfer_len;
  1032. DBUG_ASSERT(info->append_read_pos <= info->write_pos);
  1033. /*
  1034. TODO: figure out if the assert below is needed or correct.
  1035. */
  1036. DBUG_ASSERT(pos_in_file == info->end_of_file);
  1037. copy_len = MY_MIN(Count, len_in_buff);
  1038. memcpy(Buffer, info->append_read_pos, copy_len);
  1039. info->append_read_pos += copy_len;
  1040. Count -= copy_len;
  1041. if (Count) info->error = (int)(save_count - Count);
  1042. /* Fill read buffer with data from write buffer */
  1043. memcpy(info->buffer, info->append_read_pos,
  1044. (size_t)(transfer_len = len_in_buff - copy_len));
  1045. info->read_pos = info->buffer;
  1046. info->read_end = info->buffer + transfer_len;
  1047. info->append_read_pos = info->write_pos;
  1048. info->pos_in_file = pos_in_file + copy_len;
  1049. info->end_of_file += len_in_buff;
  1050. }
  1051. unlock_append_buffer(info);
  1052. return Count ? 1 : 0;
  1053. }
  1054. /* Read one byte when buffer is empty */
  1055. int _my_b_get(IO_CACHE *info) {
  1056. uchar buff;
  1057. IO_CACHE_CALLBACK pre_read, post_read;
  1058. if ((pre_read = info->pre_read)) (*pre_read)(info);
  1059. if ((*(info)->read_function)(info, &buff, 1)) return my_b_EOF;
  1060. if ((post_read = info->post_read)) (*post_read)(info);
  1061. return (int)(uchar)buff;
  1062. }
  1063. /*
  1064. Write a byte buffer to IO_CACHE and flush to disk
  1065. if IO_CACHE is full.
  1066. RETURN VALUE
  1067. 1 On error on write
  1068. 0 On success
  1069. -1 On error; my_errno contains error code.
  1070. */
  1071. int _my_b_write(IO_CACHE *info, const uchar *Buffer, size_t Count) {
  1072. size_t rest_length, length;
  1073. my_off_t pos_in_file = info->pos_in_file;
  1074. DBUG_EXECUTE_IF("simulate_huge_load_data_file",
  1075. { pos_in_file = (my_off_t)(5000000000ULL); });
  1076. if (pos_in_file + info->buffer_length > info->end_of_file) {
  1077. errno = EFBIG;
  1078. set_my_errno(EFBIG);
  1079. return info->error = -1;
  1080. }
  1081. rest_length = (size_t)(info->write_end - info->write_pos);
  1082. memcpy(info->write_pos, Buffer, (size_t)rest_length);
  1083. Buffer += rest_length;
  1084. Count -= rest_length;
  1085. info->write_pos += rest_length;
  1086. if (my_b_flush_io_cache(info, 1)) return 1;
  1087. if (Count >= IO_SIZE) { /* Fill first intern buffer */
  1088. length = Count & (size_t) ~(IO_SIZE - 1);
  1089. if (info->seek_not_done) {
  1090. /*
  1091. Whenever a function which operates on IO_CACHE flushes/writes
  1092. some part of the IO_CACHE to disk it will set the property
  1093. "seek_not_done" to indicate this to other functions operating
  1094. on the IO_CACHE.
  1095. */
  1096. if (mysql_encryption_file_seek(info, info->pos_in_file, MY_SEEK_SET,
  1097. MYF(0))) {
  1098. info->error = -1;
  1099. return (1);
  1100. }
  1101. info->seek_not_done = false;
  1102. }
  1103. /*
  1104. Verify that the correct number of bytes are written by
  1105. mysql_encryption_file_write(...) if both MY_NABP and
  1106. MY_FNABP are not set.
  1107. */
  1108. DBUG_EXECUTE_IF("verify_mysql_encryption_file_write_bytes",
  1109. size_t write_bytes = mysql_encryption_file_write(
  1110. info, Buffer, length, info->myflags);
  1111. DBUG_ASSERT(write_bytes == length););
  1112. if (DBUG_EVALUATE_IF("verify_mysql_encryption_file_write_bytes", false,
  1113. true)) {
  1114. if (mysql_encryption_file_write(info, Buffer, length,
  1115. info->myflags | MY_NABP))
  1116. return info->error = -1;
  1117. }
  1118. /*
  1119. In case of a shared I/O cache with a writer we normally do direct
  1120. write cache to read cache copy. Simulate this here by direct
  1121. caller buffer to read cache copy. Do it after the write so that
  1122. the cache readers actions on the flushed part can go in parallel
  1123. with the write of the extra stuff. copy_to_read_buffer()
  1124. synchronizes writer and readers so that after this call the
  1125. readers can act on the extra stuff while the writer can go ahead
  1126. and prepare the next output. copy_to_read_buffer() relies on
  1127. info->pos_in_file.
  1128. */
  1129. if (info->share) copy_to_read_buffer(info, Buffer, length);
  1130. Count -= length;
  1131. Buffer += length;
  1132. info->pos_in_file += length;
  1133. }
  1134. memcpy(info->write_pos, Buffer, (size_t)Count);
  1135. info->write_pos += Count;
  1136. return 0;
  1137. }
  1138. /*
  1139. Append a block to the write buffer.
  1140. This is done with the buffer locked to ensure that we don't read from
  1141. the write buffer before we are ready with it.
  1142. */
  1143. int my_b_append(IO_CACHE *info, const uchar *Buffer, size_t Count) {
  1144. size_t rest_length, length;
  1145. /*
  1146. Assert that we cannot come here with a shared cache. If we do one
  1147. day, we might need to add a call to copy_to_read_buffer().
  1148. */
  1149. DBUG_ASSERT(!info->share);
  1150. lock_append_buffer(info);
  1151. rest_length = (size_t)(info->write_end - info->write_pos);
  1152. if (Count <= rest_length) goto end;
  1153. memcpy(info->write_pos, Buffer, rest_length);
  1154. Buffer += rest_length;
  1155. Count -= rest_length;
  1156. info->write_pos += rest_length;
  1157. if (my_b_flush_io_cache(info, 0)) {
  1158. unlock_append_buffer(info);
  1159. return 1;
  1160. }
  1161. if (Count >= IO_SIZE) { /* Fill first intern buffer */
  1162. length = Count & (size_t) ~(IO_SIZE - 1);
  1163. if (mysql_encryption_file_write(info, Buffer, length,
  1164. info->myflags | MY_NABP)) {
  1165. unlock_append_buffer(info);
  1166. return info->error = -1;
  1167. }
  1168. Count -= length;
  1169. Buffer += length;
  1170. info->end_of_file += length;
  1171. }
  1172. end:
  1173. memcpy(info->write_pos, Buffer, (size_t)Count);
  1174. info->write_pos += Count;
  1175. unlock_append_buffer(info);
  1176. return 0;
  1177. }
  1178. int my_b_safe_write(IO_CACHE *info, const uchar *Buffer, size_t Count) {
  1179. /*
  1180. Sasha: We are not writing this with the ? operator to avoid hitting
  1181. a possible compiler bug. At least gcc 2.95 cannot deal with
  1182. several layers of ternary operators that evaluated comma(,) operator
  1183. expressions inside - I do have a test case if somebody wants it
  1184. */
  1185. if (info->type == SEQ_READ_APPEND) return my_b_append(info, Buffer, Count);
  1186. return my_b_write(info, Buffer, Count);
  1187. }
  1188. /*
  1189. Write a block to disk where part of the data may be inside the record
  1190. buffer. As all write calls to the data goes through the cache,
  1191. we will never get a seek over the end of the buffer
  1192. */
  1193. int my_block_write(IO_CACHE *info, const uchar *Buffer, size_t Count,
  1194. my_off_t pos) {
  1195. size_t length;
  1196. int error = 0;
  1197. /*
  1198. Assert that we cannot come here with a shared cache. If we do one
  1199. day, we might need to add a call to copy_to_read_buffer().
  1200. */
  1201. DBUG_ASSERT(!info->share);
  1202. if (pos < info->pos_in_file) {
  1203. /* Of no overlap, write everything without buffering */
  1204. if (pos + Count <= info->pos_in_file)
  1205. return (int)mysql_file_pwrite(info->file, Buffer, Count, pos,
  1206. info->myflags | MY_NABP);
  1207. /* Write the part of the block that is before buffer */
  1208. length = (uint)(info->pos_in_file - pos);
  1209. if (mysql_file_pwrite(info->file, Buffer, length, pos,
  1210. info->myflags | MY_NABP))
  1211. info->error = error = -1;
  1212. Buffer += length;
  1213. pos += length;
  1214. Count -= length;
  1215. #ifdef _WIN32
  1216. info->seek_not_done = true;
  1217. #endif
  1218. }
  1219. /* Check if we want to write inside the used part of the buffer.*/
  1220. length = (size_t)(info->write_end - info->buffer);
  1221. if (pos < info->pos_in_file + length) {
  1222. size_t offset = (size_t)(pos - info->pos_in_file);
  1223. length -= offset;
  1224. if (length > Count) length = Count;
  1225. memcpy(info->buffer + offset, Buffer, length);
  1226. Buffer += length;
  1227. Count -= length;
  1228. /* Fix length of buffer if the new data was larger */
  1229. if (info->buffer + length > info->write_pos)
  1230. info->write_pos = info->buffer + length;
  1231. if (!Count) return (error);
  1232. }
  1233. /* Write at the end of the current buffer; This is the normal case */
  1234. if (_my_b_write(info, Buffer, Count)) error = -1;
  1235. return error;
  1236. }
  1237. /* Flush write cache */
  1238. #define LOCK_APPEND_BUFFER \
  1239. if (need_append_buffer_lock) lock_append_buffer(info);
  1240. #define UNLOCK_APPEND_BUFFER \
  1241. if (need_append_buffer_lock) unlock_append_buffer(info);
  1242. int my_b_flush_io_cache(IO_CACHE *info, int need_append_buffer_lock) {
  1243. size_t length;
  1244. my_off_t pos_in_file;
  1245. bool append_cache = (info->type == SEQ_READ_APPEND);
  1246. DBUG_ENTER("my_b_flush_io_cache");
  1247. DBUG_PRINT("enter", ("cache: %p", info));
  1248. DBUG_EXECUTE_IF("simulate_error_during_flush_cache_to_file",
  1249. { DBUG_RETURN(true); });
  1250. if (!append_cache) need_append_buffer_lock = 0;
  1251. if (info->type == WRITE_CACHE || append_cache) {
  1252. if (info->file == -1) {
  1253. if (real_open_cached_file(info)) DBUG_RETURN((info->error = -1));
  1254. }
  1255. LOCK_APPEND_BUFFER;
  1256. if ((length = (size_t)(info->write_pos - info->write_buffer))) {
  1257. /*
  1258. In case of a shared I/O cache with a writer we do direct write
  1259. cache to read cache copy. Do it before the write here so that
  1260. the readers can work in parallel with the write.
  1261. copy_to_read_buffer() relies on info->pos_in_file.
  1262. */
  1263. if (info->share) copy_to_read_buffer(info, info->write_buffer, length);
  1264. pos_in_file = info->pos_in_file;
  1265. /*
  1266. If we have append cache, we always open the file with
  1267. O_APPEND which moves the pos to EOF automatically on every write
  1268. */
  1269. if (!append_cache && info->seek_not_done) { /* File touched, do seek */
  1270. if (mysql_encryption_file_seek(info, pos_in_file, MY_SEEK_SET,
  1271. MYF(0)) == MY_FILEPOS_ERROR) {
  1272. UNLOCK_APPEND_BUFFER;
  1273. DBUG_RETURN((info->error = -1));
  1274. }
  1275. if (!append_cache) info->seek_not_done = false;
  1276. }
  1277. if (!append_cache) info->pos_in_file += length;
  1278. info->write_end = (info->write_buffer + info->buffer_length -
  1279. ((pos_in_file + length) & (IO_SIZE - 1)));
  1280. if (mysql_encryption_file_write(info, info->write_buffer, length,
  1281. info->myflags | MY_NABP))
  1282. info->error = -1;
  1283. else
  1284. info->error = 0;
  1285. if (!append_cache) {
  1286. set_if_bigger(info->end_of_file, (pos_in_file + length));
  1287. } else {
  1288. info->end_of_file += (info->write_pos - info->append_read_pos);
  1289. DBUG_ASSERT(info->end_of_file == mysql_file_tell(info->file, MYF(0)));
  1290. }
  1291. info->append_read_pos = info->write_pos = info->write_buffer;
  1292. ++info->disk_writes;
  1293. UNLOCK_APPEND_BUFFER;
  1294. DBUG_RETURN(info->error);
  1295. }
  1296. }
  1297. UNLOCK_APPEND_BUFFER;
  1298. DBUG_RETURN(0);
  1299. }
  1300. /*
  1301. Free an IO_CACHE object
  1302. SYNOPSOS
  1303. end_io_cache()
  1304. info IO_CACHE Handle to free
  1305. NOTES
  1306. It's currently safe to call this if one has called init_io_cache()
  1307. on the 'info' object, even if init_io_cache() failed.
  1308. This function is also safe to call twice with the same handle.
  1309. RETURN
  1310. 0 ok
  1311. # Error
  1312. */
  1313. int end_io_cache(IO_CACHE *info) {
  1314. int error = 0;
  1315. IO_CACHE_CALLBACK pre_close;
  1316. DBUG_ENTER("end_io_cache");
  1317. DBUG_PRINT("enter", ("cache: %p", info));
  1318. /*
  1319. Every thread must call remove_io_thread(). The last one destroys
  1320. the share elements.
  1321. */
  1322. DBUG_ASSERT(!info->share || !info->share->total_threads);
  1323. if ((pre_close = info->pre_close)) {
  1324. (*pre_close)(info);
  1325. info->pre_close = 0;
  1326. }
  1327. if (info->alloced_buffer) {
  1328. info->alloced_buffer = 0;
  1329. if (info->file != -1) /* File doesn't exist */
  1330. error = my_b_flush_io_cache(info, 1);
  1331. my_free(info->buffer);
  1332. info->buffer = info->read_pos = (uchar *)0;
  1333. }
  1334. if (info->m_encryptor != nullptr) delete info->m_encryptor;
  1335. if (info->m_decryptor != nullptr) delete info->m_decryptor;
  1336. if (info->type == SEQ_READ_APPEND) {
  1337. /* Destroy allocated mutex */
  1338. info->type = TYPE_NOT_SET;
  1339. mysql_mutex_destroy(&info->append_buffer_lock);
  1340. }
  1341. DBUG_RETURN(error);
  1342. } /* end_io_cache */
  1343. /**********************************************************************
  1344. Testing of MF_IOCACHE
  1345. **********************************************************************/
  1346. #ifdef MAIN
  1347. #include "my_dir.h"
  1348. void die(const char *fmt, ...) MY_ATTRIBUTE((format(printf, 1, 2)));
  1349. void die(const char *fmt, ...) {
  1350. va_list va_args;
  1351. va_start(va_args, fmt);
  1352. fprintf(stderr, "Error:");
  1353. vfprintf(stderr, fmt, va_args);
  1354. fprintf(stderr, ", errno=%d\n", errno);
  1355. va_end(va_args);
  1356. exit(1);
  1357. }
  1358. static int open_file(const char *fname, IO_CACHE *info, int cache_size) {
  1359. int fd;
  1360. if ((fd = my_open(fname, O_CREAT | O_RDWR, MYF(MY_WME))) < 0)
  1361. die("Could not open %s", fname);
  1362. if (init_io_cache(info, fd, cache_size, SEQ_READ_APPEND, 0, 0, MYF(MY_WME)))
  1363. die("failed in init_io_cache()");
  1364. return fd;
  1365. }
  1366. static void close_file(IO_CACHE *info) {
  1367. end_io_cache(info);
  1368. my_close(info->file, MYF(MY_WME));
  1369. }
  1370. int main(int, char **argv) {
  1371. IO_CACHE sra_cache; /* SEQ_READ_APPEND */
  1372. MY_STAT status;
  1373. const char *fname = "/tmp/iocache.test";
  1374. int cache_size = 16384;
  1375. char llstr_buf[22];
  1376. int max_block, total_bytes = 0;
  1377. int i, num_loops = 100, error = 0;
  1378. uchar *p;
  1379. uchar *block, *block_end;
  1380. MY_INIT(argv[0]);
  1381. max_block = cache_size * 3;
  1382. if (!(block =
  1383. (uchar *)my_malloc(PSI_NOT_INSTRUMENTED, max_block, MYF(MY_WME))))
  1384. die("Not enough memory to allocate test block");
  1385. block_end = block + max_block;
  1386. for (p = block, i = 0; p < block_end; i++) {
  1387. *p++ = (char)i;
  1388. }
  1389. if (my_stat(fname, &status, MYF(0)) && my_delete(fname, MYF(MY_WME))) {
  1390. die("Delete of %s failed, aborting", fname);
  1391. }
  1392. open_file(fname, &sra_cache, cache_size);
  1393. for (i = 0; i < num_loops; i++) {
  1394. uchar buf[4];
  1395. int block_size = abs(rand() % max_block);
  1396. int4store(buf, block_size);
  1397. if (my_b_append(&sra_cache, buf, 4) ||
  1398. my_b_append(&sra_cache, block, block_size))
  1399. die("write failed");
  1400. total_bytes += 4 + block_size;
  1401. }
  1402. close_file(&sra_cache);
  1403. my_free(block);
  1404. if (!my_stat(fname, &status, MYF(MY_WME)))
  1405. die("failed to stat, but I had just closed it,\
  1406. wonder how that happened");
  1407. printf("Final size of %s is %s, wrote %d bytes\n", fname,
  1408. llstr(status.st_size, llstr_buf), total_bytes);
  1409. my_delete(fname, MYF(MY_WME));
  1410. /* check correctness of tests */
  1411. if (total_bytes != status.st_size) {
  1412. fprintf(stderr,
  1413. "Not the same number of bytes actually in file as bytes \
  1414. supposedly written\n");
  1415. error = 1;
  1416. }
  1417. my_end(0);
  1418. exit(error);
  1419. return 0;
  1420. }
  1421. #endif
  1422. my_off_t mysql_encryption_file_seek(IO_CACHE *cache, my_off_t pos, int whence,
  1423. myf flags) {
  1424. if (cache->m_encryptor != nullptr) cache->m_encryptor->set_stream_offset(pos);
  1425. if (cache->m_decryptor != nullptr) cache->m_decryptor->set_stream_offset(pos);
  1426. return mysql_file_seek(cache->file, pos, whence, flags);
  1427. }
  1428. size_t mysql_encryption_file_read(IO_CACHE *cache, uchar *buffer, size_t count,
  1429. myf flags) {
  1430. size_t ret = mysql_file_read(cache->file, buffer, count, flags);
  1431. if (ret != MY_FILE_ERROR && cache->m_decryptor != nullptr)
  1432. cache->m_decryptor->decrypt(buffer, buffer, ret ? ret : count);
  1433. return ret;
  1434. }
  1435. size_t mysql_encryption_file_write(IO_CACHE *cache, const uchar *buffer,
  1436. size_t count, myf flags) {
  1437. size_t ret = 0;
  1438. if (cache->m_encryptor != nullptr) {
  1439. size_t written = 0;
  1440. const int ENCRYPT_BUFFER_SIZE = IO_SIZE * 2;
  1441. unsigned char encrypt_buffer[ENCRYPT_BUFFER_SIZE];
  1442. const unsigned char *ptr = buffer;
  1443. /*
  1444. Split the data in 'buffer' to ENCRYPT_BUFFER_SIZE bytes chunks and
  1445. encrypt them one by one.
  1446. */
  1447. while (count > 0) {
  1448. int encrypt_len =
  1449. std::min(count, static_cast<size_t>(ENCRYPT_BUFFER_SIZE));
  1450. if (cache->m_encryptor->encrypt(encrypt_buffer, ptr, encrypt_len) ||
  1451. DBUG_EVALUATE_IF("simulate_binlog_cache_temp_file_encrypt_fail", true,
  1452. false))
  1453. return MY_FILE_ERROR;
  1454. ret = mysql_file_write(cache->file, encrypt_buffer, encrypt_len, flags);
  1455. if (ret == MY_FILE_ERROR) return ret;
  1456. DBUG_EXECUTE_IF("ensure_binlog_cache_temporary_file_is_encrypted",
  1457. { binlog_cache_temporary_file_is_encrypted = true; };);
  1458. if (!(flags & (MY_NABP | MY_FNABP))) {
  1459. written = written + ret;
  1460. }
  1461. ptr += encrypt_len;
  1462. count -= encrypt_len;
  1463. }
  1464. ret = written;
  1465. } else
  1466. ret = mysql_file_write(cache->file, buffer, count, flags);
  1467. return ret;
  1468. }