1
/* Copyright (C) 2000 MySQL AB & MySQL Finland AB & TCX DataKonsult AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; either version 2 of the License, or
6
(at your option) any later version.
8
This program is distributed in the hope that it will be useful,
9
but WITHOUT ANY WARRANTY; without even the implied warranty of
10
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
GNU General Public License for more details.
13
You should have received a copy of the GNU General Public License
14
along with this program; if not, write to the Free Software
15
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
18
locking of isam-tables.
19
reads info from a isam-table. Must be first request before doing any furter
20
calls to any isamfunktion. Is used to allow many process use the same
24
#include "myisamdef.h"
28
#if !defined(HAVE_PREAD) && defined(THREAD)
29
pthread_mutex_t THR_LOCK_keycache;
32
/* lock table by F_UNLCK, F_RDLCK or F_WRLCK */
34
int mi_lock_database(MI_INFO *info, int lock_type)
38
MYISAM_SHARE *share=info->s;
40
DBUG_ENTER("mi_lock_database");
42
if (share->options & HA_OPTION_READ_ONLY_DATA ||
43
info->lock_type == lock_type)
46
pthread_mutex_lock(&share->intern_lock);
47
if (share->kfile >= 0) /* May only be false on windows */
52
if (info->lock_type == F_RDLCK)
53
count= --share->r_locks;
55
count= --share->w_locks;
56
if (info->lock_type == F_WRLCK && !share->w_locks &&
57
!share->delay_key_write && flush_key_blocks(share->kfile,FLUSH_KEEP))
60
mi_mark_crashed(info); /* Mark that table must be checked */
62
if (info->opt_flag & (READ_CACHE_USED | WRITE_CACHE_USED))
64
if (end_io_cache(&info->rec_cache))
67
mi_mark_crashed(info);
72
if (share->changed && !share->w_locks)
74
share->state.process= share->last_process=share->this_process;
75
share->state.unique= info->last_unique= info->this_unique;
77
pthread_mutex_lock(&THR_LOCK_keycache); // QQ; Has to be removed!
79
if (mi_state_info_write(share->kfile, &share->state, 1))
82
pthread_mutex_unlock(&THR_LOCK_keycache);// QQ; Has to be removed!
88
if (_commit(share->kfile))
90
if (_commit(info->dfile))
92
#elif defined(HAVE_FDATASYNC)
93
if (fdatasync(share->kfile))
95
if (fdatasync(share->dfile))
97
#elif defined(HAVE_FSYNC)
98
if (fsync(share->kfile))
100
if (fsync(share->dfile))
105
share->not_flushed=1;
107
mi_mark_crashed(info);
110
{ /* Only read locks left */
112
if (my_lock(share->kfile,F_RDLCK,0L,F_TO_EOF,
113
MYF(MY_WME | MY_SEEK_NOT_DONE)) && !error)
116
else if (!share->w_locks)
117
{ /* No more locks */
119
if (my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,
120
MYF(MY_WME | MY_SEEK_NOT_DONE)) && !error)
124
info->opt_flag&= ~(READ_CACHE_USED | WRITE_CACHE_USED);
125
info->lock_type= F_UNLCK;
128
if (info->lock_type == F_WRLCK)
129
{ /* Change RW to READONLY */
130
if (share->w_locks == 1)
133
if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
134
MYF(MY_SEEK_NOT_DONE)))
142
info->lock_type=lock_type;
145
if (!share->r_locks && !share->w_locks)
148
if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
149
info->lock_wait | MY_SEEK_NOT_DONE))
154
if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
157
VOID(my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,MYF(MY_SEEK_NOT_DONE)));
162
VOID(_mi_test_if_changed(info));
164
info->lock_type=lock_type;
167
if (info->lock_type == F_RDLCK)
168
{ /* Change READONLY to RW */
169
if (share->r_locks == 1)
172
if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
173
MYF(info->lock_wait | MY_SEEK_NOT_DONE)))
180
info->lock_type=lock_type;
184
if (!(share->options & HA_OPTION_READ_ONLY_DATA))
189
VOID(my_seek(share->kfile,0L,MY_SEEK_SET,MYF(0)));
190
if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,info->lock_wait))
197
if (mi_state_info_read_dsk(share->kfile, &share->state, 0))
200
VOID(my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,info->lock_wait));
207
VOID(_mi_test_if_changed(info));
208
info->lock_type=lock_type;
212
break; /* Impossible */
215
pthread_mutex_unlock(&share->intern_lock);
216
#if defined(FULL_LOG) || defined(_lint)
217
lock_type|=(int) (flag << 8); /* Set bit to set if real lock */
218
myisam_log_command(MI_LOG_LOCK,info,(byte*) &lock_type,sizeof(lock_type),
222
} /* mi_lock_database */
225
/****************************************************************************
226
** The following functions are called by thr_lock() in threaded applications
227
****************************************************************************/
229
void mi_get_status(void* param)
231
MI_INFO *info=(MI_INFO*) param;
232
DBUG_ENTER("mi_get_status");
233
DBUG_PRINT("info",("key_file: %ld data_file: %ld",
234
(long) info->s->state.state.key_file_length,
235
(long) info->s->state.state.data_file_length));
237
if (info->state->key_file_length > info->s->state.state.key_file_length ||
238
info->state->data_file_length > info->s->state.state.data_file_length)
239
DBUG_PRINT("warning",("old info: key_file: %ld data_file: %ld",
240
(long) info->state->key_file_length,
241
(long) info->state->data_file_length));
243
info->save_state=info->s->state.state;
244
info->state= &info->save_state;
248
void mi_update_status(void* param)
250
MI_INFO *info=(MI_INFO*) param;
252
Because someone may have closed the table we point at, we only
253
update the state if its our own state. This isn't a problem as
254
we are always pointing at our own lock or at a read lock.
255
(This is enforced by thr_multi_lock.c)
257
if (info->state == &info->save_state)
260
DBUG_PRINT("info",("updating status: key_file: %ld data_file: %ld",
261
(long) info->state->key_file_length,
262
(long) info->state->data_file_length));
263
if (info->state->key_file_length < info->s->state.state.key_file_length ||
264
info->state->data_file_length < info->s->state.state.data_file_length)
265
DBUG_PRINT("warning",("old info: key_file: %ld data_file: %ld",
266
(long) info->s->state.state.key_file_length,
267
(long) info->s->state.state.data_file_length));
269
info->s->state.state= *info->state;
270
info->state= &info->s->state.state;
274
We have to flush the write cache here as other threads may start
275
reading the table before mi_lock_database() is called
277
if (info->opt_flag & WRITE_CACHE_USED)
279
if (end_io_cache(&info->rec_cache))
281
mi_mark_crashed(info);
283
info->opt_flag&= ~WRITE_CACHE_USED;
287
void mi_copy_status(void* to,void *from)
289
((MI_INFO*) to)->state= &((MI_INFO*) from)->save_state;
292
my_bool mi_check_status(void* param)
294
MI_INFO *info=(MI_INFO*) param;
295
return (my_bool) (info->s->state.dellink != HA_OFFSET_ERROR);
299
/****************************************************************************
300
** functions to read / write the state
301
****************************************************************************/
303
int _mi_readinfo(register MI_INFO *info, int lock_type, int check_keybuffer)
306
DBUG_ENTER("_mi_readinfo");
309
if (info->lock_type == F_UNLCK)
311
if (!share->r_locks && !share->w_locks)
313
if ((info->tmp_lock_type=lock_type) != F_RDLCK)
314
if (my_lock(share->kfile,lock_type,0L,F_TO_EOF,
315
info->lock_wait | MY_SEEK_NOT_DONE))
317
if (mi_state_info_read_dsk(share->kfile, &share->state, 1))
319
int error=my_errno ? my_errno : -1;
320
VOID(my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,
321
MYF(MY_SEEK_NOT_DONE)));
327
VOID(_mi_test_if_changed(info));
329
else if (lock_type == F_WRLCK && info->lock_type == F_RDLCK)
331
my_errno=EACCES; /* Not allowed to change */
332
DBUG_RETURN(-1); /* when have read_lock() */
338
/* Every isam-function that uppdates the isam-database must! end */
339
/* with this request */
342
int _mi_writeinfo(register MI_INFO *info, uint operation)
346
DBUG_ENTER("_mi_writeinfo");
350
if (share->r_locks == 0 && share->w_locks == 0)
352
olderror=my_errno; /* Remember last error */
354
{ /* Two threads can't be here */
355
share->state.process= share->last_process= share->this_process;
356
share->state.unique= info->last_unique= info->this_unique;
357
if ((error=mi_state_info_write(share->kfile, &share->state, 1)))
362
_commit(share->kfile);
363
_commit(info->dfile);
367
if (!(operation & WRITEINFO_NO_UNLOCK) &&
368
my_lock(share->kfile,F_UNLCK,0L,F_TO_EOF,
369
MYF(MY_WME | MY_SEEK_NOT_DONE)) && !error)
375
share->changed= 1; /* Mark keyfile changed */
378
} /* _mi_writeinfo */
381
/* Test if someone has changed the database */
382
/* (Should be called after readinfo) */
384
int _mi_test_if_changed(register MI_INFO *info)
386
MYISAM_SHARE *share=info->s;
387
if (share->state.process != share->last_process ||
388
share->state.unique != info->last_unique)
389
{ /* Keyfile has changed */
390
if (share->state.process != share->this_process)
391
VOID(flush_key_blocks(share->kfile,FLUSH_RELEASE));
392
share->last_process=share->state.process;
393
info->last_unique= share->state.unique;
394
info->update|= HA_STATE_WRITTEN; /* Must use file on next */
395
info->data_changed= 1; /* For mi_is_changed */
398
return (!(info->update & HA_STATE_AKTIV) ||
399
(info->update & (HA_STATE_WRITTEN | HA_STATE_DELETED |
400
HA_STATE_KEY_CHANGED)));
401
} /* _mi_test_if_changed */
404
/* Put a mark in the .ISM file that someone is updating the table */
406
int _mi_mark_file_changed(MI_INFO *info)
409
register MYISAM_SHARE *share=info->s;
410
if (!share->state.changed || ! share->global_changed)
412
share->state.changed|=1;
413
if (!share->global_changed)
415
share->global_changed=1;
416
share->state.open_count++;
418
mi_int2store(buff,share->state.open_count);
419
buff[2]=1; /* Mark that it's changed */
420
return (my_pwrite(share->kfile,buff,sizeof(buff),
421
sizeof(share->state.header),
429
This is only called by close or by extra(HA_FLUSH) if the OS has the pwrite()
430
call. In these context the following code should be safe!
433
int _mi_decrement_open_count(MI_INFO *info)
436
register MYISAM_SHARE *share=info->s;
437
int lock_error=0,write_error=0;
438
if (share->global_changed)
440
uint old_lock=info->lock_type;
441
share->global_changed=0;
442
lock_error=mi_lock_database(info,F_WRLCK);
443
/* Its not fatal even if we couldn't get the lock ! */
444
if (share->state.open_count > 0)
446
share->state.open_count--;
447
mi_int2store(buff,share->state.open_count);
448
write_error=my_pwrite(share->kfile,buff,sizeof(buff),
449
sizeof(share->state.header),
453
lock_error=mi_lock_database(info,old_lock);
455
return test(lock_error || write_error);