drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** 2004 May 22 |
| 3 | ** |
| 4 | ** The author disclaims copyright to this source code. In place of |
| 5 | ** a legal notice, here is a blessing: |
| 6 | ** |
| 7 | ** May you do good and not evil. |
| 8 | ** May you find forgiveness for yourself and forgive others. |
| 9 | ** May you share freely, never taking more than you give. |
| 10 | ** |
| 11 | ****************************************************************************** |
| 12 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 13 | ** This file contains the VFS implementation for unix-like operating systems |
| 14 | ** include Linux, MacOSX, *BSD, QNX, VxWorks, AIX, HPUX, and others. |
danielk1977 | 822a516 | 2008-05-16 04:51:54 +0000 | [diff] [blame] | 15 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 16 | ** There are actually several different VFS implementations in this file. |
| 17 | ** The differences are in the way that file locking is done. The default |
| 18 | ** implementation uses Posix Advisory Locks. Alternative implementations |
| 19 | ** use flock(), dot-files, various proprietary locking schemas, or simply |
| 20 | ** skip locking all together. |
| 21 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 22 | ** This source file is organized into divisions where the logic for various |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 23 | ** subfunctions is contained within the appropriate division. PLEASE |
| 24 | ** KEEP THE STRUCTURE OF THIS FILE INTACT. New code should be placed |
| 25 | ** in the correct division and should be clearly labeled. |
| 26 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 27 | ** The layout of divisions is as follows: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 28 | ** |
| 29 | ** * General-purpose declarations and utility functions. |
| 30 | ** * Unique file ID logic used by VxWorks. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 31 | ** * Various locking primitive implementations (all except proxy locking): |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 32 | ** + for Posix Advisory Locks |
| 33 | ** + for no-op locks |
| 34 | ** + for dot-file locks |
| 35 | ** + for flock() locking |
| 36 | ** + for named semaphore locks (VxWorks only) |
| 37 | ** + for AFP filesystem locks (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 38 | ** * sqlite3_file methods not associated with locking. |
| 39 | ** * Definitions of sqlite3_io_methods objects for all locking |
| 40 | ** methods plus "finder" functions for each locking method. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 41 | ** * sqlite3_vfs method implementations. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 42 | ** * Locking primitives for the proxy uber-locking-method. (MacOSX only) |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 43 | ** * Definitions of sqlite3_vfs objects for all locking methods |
| 44 | ** plus implementations of sqlite3_os_init() and sqlite3_os_end(). |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 45 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 46 | #include "sqliteInt.h" |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 47 | #if SQLITE_OS_UNIX /* This file is used on unix only */ |
drh | 66560ad | 2006-01-06 14:32:19 +0000 | [diff] [blame] | 48 | |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 49 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 50 | ** There are various methods for file locking used for concurrency |
| 51 | ** control: |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 52 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 53 | ** 1. POSIX locking (the default), |
| 54 | ** 2. No locking, |
| 55 | ** 3. Dot-file locking, |
| 56 | ** 4. flock() locking, |
| 57 | ** 5. AFP locking (OSX only), |
| 58 | ** 6. Named POSIX semaphores (VXWorks only), |
| 59 | ** 7. proxy locking. (OSX only) |
| 60 | ** |
| 61 | ** Styles 4, 5, and 7 are only available of SQLITE_ENABLE_LOCKING_STYLE |
| 62 | ** is defined to 1. The SQLITE_ENABLE_LOCKING_STYLE also enables automatic |
| 63 | ** selection of the appropriate locking style based on the filesystem |
| 64 | ** where the database is located. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 65 | */ |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 66 | #if !defined(SQLITE_ENABLE_LOCKING_STYLE) |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 67 | # if defined(__APPLE__) |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 68 | # define SQLITE_ENABLE_LOCKING_STYLE 1 |
| 69 | # else |
| 70 | # define SQLITE_ENABLE_LOCKING_STYLE 0 |
| 71 | # endif |
| 72 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 73 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 74 | /* |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 75 | ** Define the OS_VXWORKS pre-processor macro to 1 if building on |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 76 | ** vxworks, or 0 otherwise. |
| 77 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 78 | #ifndef OS_VXWORKS |
| 79 | # if defined(__RTP__) || defined(_WRS_KERNEL) |
| 80 | # define OS_VXWORKS 1 |
| 81 | # else |
| 82 | # define OS_VXWORKS 0 |
| 83 | # endif |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 84 | #endif |
| 85 | |
| 86 | /* |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 87 | ** These #defines should enable >2GB file support on Posix if the |
| 88 | ** underlying operating system supports it. If the OS lacks |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 89 | ** large file support, these should be no-ops. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 90 | ** |
| 91 | ** Large file support can be disabled using the -DSQLITE_DISABLE_LFS switch |
| 92 | ** on the compiler command line. This is necessary if you are compiling |
| 93 | ** on a recent machine (ex: RedHat 7.2) but you want your code to work |
| 94 | ** on an older machine (ex: RedHat 6.0). If you compile on RedHat 7.2 |
| 95 | ** without this option, LFS is enable. But LFS does not exist in the kernel |
| 96 | ** in RedHat 6.0, so the code won't work. Hence, for maximum binary |
| 97 | ** portability you should omit LFS. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 98 | ** |
| 99 | ** The previous paragraph was written in 2005. (This paragraph is written |
| 100 | ** on 2008-11-28.) These days, all Linux kernels support large files, so |
| 101 | ** you should probably leave LFS enabled. But some embedded platforms might |
| 102 | ** lack LFS in which case the SQLITE_DISABLE_LFS macro might still be useful. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 103 | */ |
| 104 | #ifndef SQLITE_DISABLE_LFS |
| 105 | # define _LARGE_FILE 1 |
| 106 | # ifndef _FILE_OFFSET_BITS |
| 107 | # define _FILE_OFFSET_BITS 64 |
| 108 | # endif |
| 109 | # define _LARGEFILE_SOURCE 1 |
| 110 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 111 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 112 | /* |
| 113 | ** standard include files. |
| 114 | */ |
| 115 | #include <sys/types.h> |
| 116 | #include <sys/stat.h> |
| 117 | #include <fcntl.h> |
| 118 | #include <unistd.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 119 | #include <time.h> |
drh | 19e2d37 | 2005-08-29 23:00:03 +0000 | [diff] [blame] | 120 | #include <sys/time.h> |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 121 | #include <errno.h> |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 122 | #include <sys/mman.h> |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 123 | |
drh | 40bbb0a | 2008-09-23 10:23:26 +0000 | [diff] [blame] | 124 | #if SQLITE_ENABLE_LOCKING_STYLE |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 125 | # include <sys/ioctl.h> |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 126 | # if OS_VXWORKS |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 127 | # include <semaphore.h> |
| 128 | # include <limits.h> |
| 129 | # else |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 130 | # include <sys/file.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 131 | # include <sys/param.h> |
danielk1977 | c70dfc4 | 2008-11-19 13:52:30 +0000 | [diff] [blame] | 132 | # endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 133 | #endif /* SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 134 | |
drh | f8b4d8c | 2010-03-05 13:53:22 +0000 | [diff] [blame] | 135 | #if defined(__APPLE__) || (SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS) |
drh | 84a2bf6 | 2010-03-05 13:41:06 +0000 | [diff] [blame] | 136 | # include <sys/mount.h> |
| 137 | #endif |
| 138 | |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 139 | /* |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 140 | ** Allowed values of unixFile.fsFlags |
| 141 | */ |
| 142 | #define SQLITE_FSFLAGS_IS_MSDOS 0x1 |
| 143 | |
| 144 | /* |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 145 | ** If we are to be thread-safe, include the pthreads header and define |
| 146 | ** the SQLITE_UNIX_THREADS macro. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 147 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 148 | #if SQLITE_THREADSAFE |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 149 | # include <pthread.h> |
| 150 | # define SQLITE_UNIX_THREADS 1 |
| 151 | #endif |
| 152 | |
| 153 | /* |
| 154 | ** Default permissions when creating a new file |
| 155 | */ |
| 156 | #ifndef SQLITE_DEFAULT_FILE_PERMISSIONS |
| 157 | # define SQLITE_DEFAULT_FILE_PERMISSIONS 0644 |
| 158 | #endif |
| 159 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 160 | /* |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 161 | ** Default permissions when creating auto proxy dir |
| 162 | */ |
| 163 | #ifndef SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 164 | # define SQLITE_DEFAULT_PROXYDIR_PERMISSIONS 0755 |
| 165 | #endif |
| 166 | |
| 167 | /* |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 168 | ** Maximum supported path-length. |
| 169 | */ |
| 170 | #define MAX_PATHNAME 512 |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 171 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 172 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 173 | ** Only set the lastErrno if the error code is a real error and not |
| 174 | ** a normal expected return code of SQLITE_BUSY or SQLITE_OK |
| 175 | */ |
| 176 | #define IS_LOCK_ERROR(x) ((x != SQLITE_OK) && (x != SQLITE_BUSY)) |
| 177 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 178 | /* Forward references */ |
| 179 | typedef struct unixShm unixShm; /* Connection shared memory */ |
| 180 | typedef struct unixShmNode unixShmNode; /* Shared memory instance */ |
| 181 | typedef struct unixInodeInfo unixInodeInfo; /* An i-node */ |
| 182 | typedef struct UnixUnusedFd UnixUnusedFd; /* An unused file descriptor */ |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 183 | |
| 184 | /* |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 185 | ** Sometimes, after a file handle is closed by SQLite, the file descriptor |
| 186 | ** cannot be closed immediately. In these cases, instances of the following |
| 187 | ** structure are used to store the file descriptor while waiting for an |
| 188 | ** opportunity to either close or reuse it. |
| 189 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 190 | struct UnixUnusedFd { |
| 191 | int fd; /* File descriptor to close */ |
| 192 | int flags; /* Flags this file descriptor was opened with */ |
| 193 | UnixUnusedFd *pNext; /* Next unused file descriptor on same file */ |
| 194 | }; |
| 195 | |
| 196 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 197 | ** The unixFile structure is subclass of sqlite3_file specific to the unix |
| 198 | ** VFS implementations. |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 199 | */ |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 200 | typedef struct unixFile unixFile; |
| 201 | struct unixFile { |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 202 | sqlite3_io_methods const *pMethod; /* Always the first entry */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 203 | unixInodeInfo *pInode; /* Info about locks on this inode */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 204 | int h; /* The file descriptor */ |
| 205 | int dirfd; /* File descriptor for the directory */ |
| 206 | unsigned char eFileLock; /* The type of lock held on this fd */ |
| 207 | int lastErrno; /* The unix errno from last I/O error */ |
| 208 | void *lockingContext; /* Locking style specific state */ |
| 209 | UnixUnusedFd *pUnused; /* Pre-allocated UnixUnusedFd */ |
| 210 | int fileFlags; /* Miscellanous flags */ |
| 211 | const char *zPath; /* Name of the file */ |
| 212 | unixShm *pShm; /* Shared memory segment information */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 213 | int szChunk; /* Configured by FCNTL_CHUNK_SIZE */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 214 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 215 | int openFlags; /* The flags specified at open() */ |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 216 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 217 | #if SQLITE_ENABLE_LOCKING_STYLE || defined(__APPLE__) |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 218 | unsigned fsFlags; /* cached details from statfs() */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 219 | #endif |
| 220 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 221 | int isDelete; /* Delete on close if true */ |
| 222 | struct vxworksFileId *pId; /* Unique file ID */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 223 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 224 | #ifndef NDEBUG |
| 225 | /* The next group of variables are used to track whether or not the |
| 226 | ** transaction counter in bytes 24-27 of database files are updated |
| 227 | ** whenever any part of the database changes. An assertion fault will |
| 228 | ** occur if a file is updated without also updating the transaction |
| 229 | ** counter. This test is made to avoid new problems similar to the |
| 230 | ** one described by ticket #3584. |
| 231 | */ |
| 232 | unsigned char transCntrChng; /* True if the transaction counter changed */ |
| 233 | unsigned char dbUpdate; /* True if any part of database file changed */ |
| 234 | unsigned char inNormalWrite; /* True if in a normal write operation */ |
| 235 | #endif |
danielk1977 | 967a4a1 | 2007-08-20 14:23:44 +0000 | [diff] [blame] | 236 | #ifdef SQLITE_TEST |
| 237 | /* In test mode, increase the size of this structure a bit so that |
| 238 | ** it is larger than the struct CrashFile defined in test6.c. |
| 239 | */ |
| 240 | char aPadding[32]; |
| 241 | #endif |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 242 | }; |
| 243 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 244 | /* |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 245 | ** The following macros define bits in unixFile.fileFlags |
| 246 | */ |
| 247 | #define SQLITE_WHOLE_FILE_LOCKING 0x0001 /* Use whole-file locking */ |
| 248 | |
| 249 | /* |
drh | 198bf39 | 2006-01-06 21:52:49 +0000 | [diff] [blame] | 250 | ** Include code that is common to all os_*.c files |
| 251 | */ |
| 252 | #include "os_common.h" |
| 253 | |
| 254 | /* |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 255 | ** Define various macros that are missing from some systems. |
| 256 | */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 257 | #ifndef O_LARGEFILE |
| 258 | # define O_LARGEFILE 0 |
| 259 | #endif |
| 260 | #ifdef SQLITE_DISABLE_LFS |
| 261 | # undef O_LARGEFILE |
| 262 | # define O_LARGEFILE 0 |
| 263 | #endif |
| 264 | #ifndef O_NOFOLLOW |
| 265 | # define O_NOFOLLOW 0 |
| 266 | #endif |
| 267 | #ifndef O_BINARY |
| 268 | # define O_BINARY 0 |
| 269 | #endif |
| 270 | |
| 271 | /* |
| 272 | ** The DJGPP compiler environment looks mostly like Unix, but it |
| 273 | ** lacks the fcntl() system call. So redefine fcntl() to be something |
| 274 | ** that always succeeds. This means that locking does not occur under |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 275 | ** DJGPP. But it is DOS - what did you expect? |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 276 | */ |
| 277 | #ifdef __DJGPP__ |
| 278 | # define fcntl(A,B,C) 0 |
| 279 | #endif |
| 280 | |
| 281 | /* |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 282 | ** The threadid macro resolves to the thread-id or to 0. Used for |
| 283 | ** testing and debugging only. |
| 284 | */ |
drh | d677b3d | 2007-08-20 22:48:41 +0000 | [diff] [blame] | 285 | #if SQLITE_THREADSAFE |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 286 | #define threadid pthread_self() |
| 287 | #else |
| 288 | #define threadid 0 |
| 289 | #endif |
| 290 | |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 291 | |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 292 | /* |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 293 | ** Helper functions to obtain and relinquish the global mutex. The |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 294 | ** global mutex is used to protect the unixInodeInfo and |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 295 | ** vxworksFileId objects used by this file, all of which may be |
| 296 | ** shared by multiple threads. |
| 297 | ** |
| 298 | ** Function unixMutexHeld() is used to assert() that the global mutex |
| 299 | ** is held when required. This function is only used as part of assert() |
| 300 | ** statements. e.g. |
| 301 | ** |
| 302 | ** unixEnterMutex() |
| 303 | ** assert( unixMutexHeld() ); |
| 304 | ** unixEnterLeave() |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 305 | */ |
| 306 | static void unixEnterMutex(void){ |
| 307 | sqlite3_mutex_enter(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 308 | } |
| 309 | static void unixLeaveMutex(void){ |
| 310 | sqlite3_mutex_leave(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 311 | } |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 312 | #ifdef SQLITE_DEBUG |
| 313 | static int unixMutexHeld(void) { |
| 314 | return sqlite3_mutex_held(sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER)); |
| 315 | } |
| 316 | #endif |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 317 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 318 | |
| 319 | #ifdef SQLITE_DEBUG |
| 320 | /* |
| 321 | ** Helper function for printing out trace information from debugging |
| 322 | ** binaries. This returns the string represetation of the supplied |
| 323 | ** integer lock-type. |
| 324 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 325 | static const char *azFileLock(int eFileLock){ |
| 326 | switch( eFileLock ){ |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 327 | case NO_LOCK: return "NONE"; |
| 328 | case SHARED_LOCK: return "SHARED"; |
| 329 | case RESERVED_LOCK: return "RESERVED"; |
| 330 | case PENDING_LOCK: return "PENDING"; |
| 331 | case EXCLUSIVE_LOCK: return "EXCLUSIVE"; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 332 | } |
| 333 | return "ERROR"; |
| 334 | } |
| 335 | #endif |
| 336 | |
| 337 | #ifdef SQLITE_LOCK_TRACE |
| 338 | /* |
| 339 | ** Print out information about all locking operations. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 340 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 341 | ** This routine is used for troubleshooting locks on multithreaded |
| 342 | ** platforms. Enable by compiling with the -DSQLITE_LOCK_TRACE |
| 343 | ** command-line option on the compiler. This code is normally |
| 344 | ** turned off. |
| 345 | */ |
| 346 | static int lockTrace(int fd, int op, struct flock *p){ |
| 347 | char *zOpName, *zType; |
| 348 | int s; |
| 349 | int savedErrno; |
| 350 | if( op==F_GETLK ){ |
| 351 | zOpName = "GETLK"; |
| 352 | }else if( op==F_SETLK ){ |
| 353 | zOpName = "SETLK"; |
| 354 | }else{ |
| 355 | s = fcntl(fd, op, p); |
| 356 | sqlite3DebugPrintf("fcntl unknown %d %d %d\n", fd, op, s); |
| 357 | return s; |
| 358 | } |
| 359 | if( p->l_type==F_RDLCK ){ |
| 360 | zType = "RDLCK"; |
| 361 | }else if( p->l_type==F_WRLCK ){ |
| 362 | zType = "WRLCK"; |
| 363 | }else if( p->l_type==F_UNLCK ){ |
| 364 | zType = "UNLCK"; |
| 365 | }else{ |
| 366 | assert( 0 ); |
| 367 | } |
| 368 | assert( p->l_whence==SEEK_SET ); |
| 369 | s = fcntl(fd, op, p); |
| 370 | savedErrno = errno; |
| 371 | sqlite3DebugPrintf("fcntl %d %d %s %s %d %d %d %d\n", |
| 372 | threadid, fd, zOpName, zType, (int)p->l_start, (int)p->l_len, |
| 373 | (int)p->l_pid, s); |
| 374 | if( s==(-1) && op==F_SETLK && (p->l_type==F_RDLCK || p->l_type==F_WRLCK) ){ |
| 375 | struct flock l2; |
| 376 | l2 = *p; |
| 377 | fcntl(fd, F_GETLK, &l2); |
| 378 | if( l2.l_type==F_RDLCK ){ |
| 379 | zType = "RDLCK"; |
| 380 | }else if( l2.l_type==F_WRLCK ){ |
| 381 | zType = "WRLCK"; |
| 382 | }else if( l2.l_type==F_UNLCK ){ |
| 383 | zType = "UNLCK"; |
| 384 | }else{ |
| 385 | assert( 0 ); |
| 386 | } |
| 387 | sqlite3DebugPrintf("fcntl-failure-reason: %s %d %d %d\n", |
| 388 | zType, (int)l2.l_start, (int)l2.l_len, (int)l2.l_pid); |
| 389 | } |
| 390 | errno = savedErrno; |
| 391 | return s; |
| 392 | } |
| 393 | #define fcntl lockTrace |
| 394 | #endif /* SQLITE_LOCK_TRACE */ |
| 395 | |
| 396 | |
| 397 | |
| 398 | /* |
| 399 | ** This routine translates a standard POSIX errno code into something |
| 400 | ** useful to the clients of the sqlite3 functions. Specifically, it is |
| 401 | ** intended to translate a variety of "try again" errors into SQLITE_BUSY |
| 402 | ** and a variety of "please close the file descriptor NOW" errors into |
| 403 | ** SQLITE_IOERR |
| 404 | ** |
| 405 | ** Errors during initialization of locks, or file system support for locks, |
| 406 | ** should handle ENOLCK, ENOTSUP, EOPNOTSUPP separately. |
| 407 | */ |
| 408 | static int sqliteErrorFromPosixError(int posixError, int sqliteIOErr) { |
| 409 | switch (posixError) { |
| 410 | case 0: |
| 411 | return SQLITE_OK; |
| 412 | |
| 413 | case EAGAIN: |
| 414 | case ETIMEDOUT: |
| 415 | case EBUSY: |
| 416 | case EINTR: |
| 417 | case ENOLCK: |
| 418 | /* random NFS retry error, unless during file system support |
| 419 | * introspection, in which it actually means what it says */ |
| 420 | return SQLITE_BUSY; |
| 421 | |
| 422 | case EACCES: |
| 423 | /* EACCES is like EAGAIN during locking operations, but not any other time*/ |
| 424 | if( (sqliteIOErr == SQLITE_IOERR_LOCK) || |
| 425 | (sqliteIOErr == SQLITE_IOERR_UNLOCK) || |
| 426 | (sqliteIOErr == SQLITE_IOERR_RDLOCK) || |
| 427 | (sqliteIOErr == SQLITE_IOERR_CHECKRESERVEDLOCK) ){ |
| 428 | return SQLITE_BUSY; |
| 429 | } |
| 430 | /* else fall through */ |
| 431 | case EPERM: |
| 432 | return SQLITE_PERM; |
| 433 | |
| 434 | case EDEADLK: |
| 435 | return SQLITE_IOERR_BLOCKED; |
| 436 | |
| 437 | #if EOPNOTSUPP!=ENOTSUP |
| 438 | case EOPNOTSUPP: |
| 439 | /* something went terribly awry, unless during file system support |
| 440 | * introspection, in which it actually means what it says */ |
| 441 | #endif |
| 442 | #ifdef ENOTSUP |
| 443 | case ENOTSUP: |
| 444 | /* invalid fd, unless during file system support introspection, in which |
| 445 | * it actually means what it says */ |
| 446 | #endif |
| 447 | case EIO: |
| 448 | case EBADF: |
| 449 | case EINVAL: |
| 450 | case ENOTCONN: |
| 451 | case ENODEV: |
| 452 | case ENXIO: |
| 453 | case ENOENT: |
| 454 | case ESTALE: |
| 455 | case ENOSYS: |
| 456 | /* these should force the client to close the file and reconnect */ |
| 457 | |
| 458 | default: |
| 459 | return sqliteIOErr; |
| 460 | } |
| 461 | } |
| 462 | |
| 463 | |
| 464 | |
| 465 | /****************************************************************************** |
| 466 | ****************** Begin Unique File ID Utility Used By VxWorks *************** |
| 467 | ** |
| 468 | ** On most versions of unix, we can get a unique ID for a file by concatenating |
| 469 | ** the device number and the inode number. But this does not work on VxWorks. |
| 470 | ** On VxWorks, a unique file id must be based on the canonical filename. |
| 471 | ** |
| 472 | ** A pointer to an instance of the following structure can be used as a |
| 473 | ** unique file ID in VxWorks. Each instance of this structure contains |
| 474 | ** a copy of the canonical filename. There is also a reference count. |
| 475 | ** The structure is reclaimed when the number of pointers to it drops to |
| 476 | ** zero. |
| 477 | ** |
| 478 | ** There are never very many files open at one time and lookups are not |
| 479 | ** a performance-critical path, so it is sufficient to put these |
| 480 | ** structures on a linked list. |
| 481 | */ |
| 482 | struct vxworksFileId { |
| 483 | struct vxworksFileId *pNext; /* Next in a list of them all */ |
| 484 | int nRef; /* Number of references to this one */ |
| 485 | int nName; /* Length of the zCanonicalName[] string */ |
| 486 | char *zCanonicalName; /* Canonical filename */ |
| 487 | }; |
| 488 | |
| 489 | #if OS_VXWORKS |
| 490 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 491 | ** All unique filenames are held on a linked list headed by this |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 492 | ** variable: |
| 493 | */ |
| 494 | static struct vxworksFileId *vxworksFileList = 0; |
| 495 | |
| 496 | /* |
| 497 | ** Simplify a filename into its canonical form |
| 498 | ** by making the following changes: |
| 499 | ** |
| 500 | ** * removing any trailing and duplicate / |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 501 | ** * convert /./ into just / |
| 502 | ** * convert /A/../ where A is any simple name into just / |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 503 | ** |
| 504 | ** Changes are made in-place. Return the new name length. |
| 505 | ** |
| 506 | ** The original filename is in z[0..n-1]. Return the number of |
| 507 | ** characters in the simplified name. |
| 508 | */ |
| 509 | static int vxworksSimplifyName(char *z, int n){ |
| 510 | int i, j; |
| 511 | while( n>1 && z[n-1]=='/' ){ n--; } |
| 512 | for(i=j=0; i<n; i++){ |
| 513 | if( z[i]=='/' ){ |
| 514 | if( z[i+1]=='/' ) continue; |
| 515 | if( z[i+1]=='.' && i+2<n && z[i+2]=='/' ){ |
| 516 | i += 1; |
| 517 | continue; |
| 518 | } |
| 519 | if( z[i+1]=='.' && i+3<n && z[i+2]=='.' && z[i+3]=='/' ){ |
| 520 | while( j>0 && z[j-1]!='/' ){ j--; } |
| 521 | if( j>0 ){ j--; } |
| 522 | i += 2; |
| 523 | continue; |
| 524 | } |
| 525 | } |
| 526 | z[j++] = z[i]; |
| 527 | } |
| 528 | z[j] = 0; |
| 529 | return j; |
| 530 | } |
| 531 | |
| 532 | /* |
| 533 | ** Find a unique file ID for the given absolute pathname. Return |
| 534 | ** a pointer to the vxworksFileId object. This pointer is the unique |
| 535 | ** file ID. |
| 536 | ** |
| 537 | ** The nRef field of the vxworksFileId object is incremented before |
| 538 | ** the object is returned. A new vxworksFileId object is created |
| 539 | ** and added to the global list if necessary. |
| 540 | ** |
| 541 | ** If a memory allocation error occurs, return NULL. |
| 542 | */ |
| 543 | static struct vxworksFileId *vxworksFindFileId(const char *zAbsoluteName){ |
| 544 | struct vxworksFileId *pNew; /* search key and new file ID */ |
| 545 | struct vxworksFileId *pCandidate; /* For looping over existing file IDs */ |
| 546 | int n; /* Length of zAbsoluteName string */ |
| 547 | |
| 548 | assert( zAbsoluteName[0]=='/' ); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 549 | n = (int)strlen(zAbsoluteName); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 550 | pNew = sqlite3_malloc( sizeof(*pNew) + (n+1) ); |
| 551 | if( pNew==0 ) return 0; |
| 552 | pNew->zCanonicalName = (char*)&pNew[1]; |
| 553 | memcpy(pNew->zCanonicalName, zAbsoluteName, n+1); |
| 554 | n = vxworksSimplifyName(pNew->zCanonicalName, n); |
| 555 | |
| 556 | /* Search for an existing entry that matching the canonical name. |
| 557 | ** If found, increment the reference count and return a pointer to |
| 558 | ** the existing file ID. |
| 559 | */ |
| 560 | unixEnterMutex(); |
| 561 | for(pCandidate=vxworksFileList; pCandidate; pCandidate=pCandidate->pNext){ |
| 562 | if( pCandidate->nName==n |
| 563 | && memcmp(pCandidate->zCanonicalName, pNew->zCanonicalName, n)==0 |
| 564 | ){ |
| 565 | sqlite3_free(pNew); |
| 566 | pCandidate->nRef++; |
| 567 | unixLeaveMutex(); |
| 568 | return pCandidate; |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | /* No match was found. We will make a new file ID */ |
| 573 | pNew->nRef = 1; |
| 574 | pNew->nName = n; |
| 575 | pNew->pNext = vxworksFileList; |
| 576 | vxworksFileList = pNew; |
| 577 | unixLeaveMutex(); |
| 578 | return pNew; |
| 579 | } |
| 580 | |
| 581 | /* |
| 582 | ** Decrement the reference count on a vxworksFileId object. Free |
| 583 | ** the object when the reference count reaches zero. |
| 584 | */ |
| 585 | static void vxworksReleaseFileId(struct vxworksFileId *pId){ |
| 586 | unixEnterMutex(); |
| 587 | assert( pId->nRef>0 ); |
| 588 | pId->nRef--; |
| 589 | if( pId->nRef==0 ){ |
| 590 | struct vxworksFileId **pp; |
| 591 | for(pp=&vxworksFileList; *pp && *pp!=pId; pp = &((*pp)->pNext)){} |
| 592 | assert( *pp==pId ); |
| 593 | *pp = pId->pNext; |
| 594 | sqlite3_free(pId); |
| 595 | } |
| 596 | unixLeaveMutex(); |
| 597 | } |
| 598 | #endif /* OS_VXWORKS */ |
| 599 | /*************** End of Unique File ID Utility Used By VxWorks **************** |
| 600 | ******************************************************************************/ |
| 601 | |
| 602 | |
| 603 | /****************************************************************************** |
| 604 | *************************** Posix Advisory Locking **************************** |
| 605 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 606 | ** POSIX advisory locks are broken by design. ANSI STD 1003.1 (1996) |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 607 | ** section 6.5.2.2 lines 483 through 490 specify that when a process |
| 608 | ** sets or clears a lock, that operation overrides any prior locks set |
| 609 | ** by the same process. It does not explicitly say so, but this implies |
| 610 | ** that it overrides locks set by the same process using a different |
| 611 | ** file descriptor. Consider this test case: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 612 | ** |
| 613 | ** int fd1 = open("./file1", O_RDWR|O_CREAT, 0644); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 614 | ** int fd2 = open("./file2", O_RDWR|O_CREAT, 0644); |
| 615 | ** |
| 616 | ** Suppose ./file1 and ./file2 are really the same file (because |
| 617 | ** one is a hard or symbolic link to the other) then if you set |
| 618 | ** an exclusive lock on fd1, then try to get an exclusive lock |
| 619 | ** on fd2, it works. I would have expected the second lock to |
| 620 | ** fail since there was already a lock on the file due to fd1. |
| 621 | ** But not so. Since both locks came from the same process, the |
| 622 | ** second overrides the first, even though they were on different |
| 623 | ** file descriptors opened on different file names. |
| 624 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 625 | ** This means that we cannot use POSIX locks to synchronize file access |
| 626 | ** among competing threads of the same process. POSIX locks will work fine |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 627 | ** to synchronize access for threads in separate processes, but not |
| 628 | ** threads within the same process. |
| 629 | ** |
| 630 | ** To work around the problem, SQLite has to manage file locks internally |
| 631 | ** on its own. Whenever a new database is opened, we have to find the |
| 632 | ** specific inode of the database file (the inode is determined by the |
| 633 | ** st_dev and st_ino fields of the stat structure that fstat() fills in) |
| 634 | ** and check for locks already existing on that inode. When locks are |
| 635 | ** created or removed, we have to look at our own internal record of the |
| 636 | ** locks to see if another thread has previously set a lock on that same |
| 637 | ** inode. |
| 638 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 639 | ** (Aside: The use of inode numbers as unique IDs does not work on VxWorks. |
| 640 | ** For VxWorks, we have to use the alternative unique ID system based on |
| 641 | ** canonical filename and implemented in the previous division.) |
| 642 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 643 | ** The sqlite3_file structure for POSIX is no longer just an integer file |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 644 | ** descriptor. It is now a structure that holds the integer file |
| 645 | ** descriptor and a pointer to a structure that describes the internal |
| 646 | ** locks on the corresponding inode. There is one locking structure |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 647 | ** per inode, so if the same inode is opened twice, both unixFile structures |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 648 | ** point to the same locking structure. The locking structure keeps |
| 649 | ** a reference count (so we will know when to delete it) and a "cnt" |
| 650 | ** field that tells us its internal lock status. cnt==0 means the |
| 651 | ** file is unlocked. cnt==-1 means the file has an exclusive lock. |
| 652 | ** cnt>0 means there are cnt shared locks on the file. |
| 653 | ** |
| 654 | ** Any attempt to lock or unlock a file first checks the locking |
| 655 | ** structure. The fcntl() system call is only invoked to set a |
| 656 | ** POSIX lock if the internal lock structure transitions between |
| 657 | ** a locked and an unlocked state. |
| 658 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 659 | ** But wait: there are yet more problems with POSIX advisory locks. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 660 | ** |
| 661 | ** If you close a file descriptor that points to a file that has locks, |
| 662 | ** all locks on that file that are owned by the current process are |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 663 | ** released. To work around this problem, each unixInodeInfo object |
| 664 | ** maintains a count of the number of pending locks on tha inode. |
| 665 | ** When an attempt is made to close an unixFile, if there are |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 666 | ** other unixFile open on the same inode that are holding locks, the call |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 667 | ** to close() the file descriptor is deferred until all of the locks clear. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 668 | ** The unixInodeInfo structure keeps a list of file descriptors that need to |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 669 | ** be closed and that list is walked (and cleared) when the last lock |
| 670 | ** clears. |
| 671 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 672 | ** Yet another problem: LinuxThreads do not play well with posix locks. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 673 | ** |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 674 | ** Many older versions of linux use the LinuxThreads library which is |
| 675 | ** not posix compliant. Under LinuxThreads, a lock created by thread |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 676 | ** A cannot be modified or overridden by a different thread B. |
| 677 | ** Only thread A can modify the lock. Locking behavior is correct |
| 678 | ** if the appliation uses the newer Native Posix Thread Library (NPTL) |
| 679 | ** on linux - with NPTL a lock created by thread A can override locks |
| 680 | ** in thread B. But there is no way to know at compile-time which |
| 681 | ** threading library is being used. So there is no way to know at |
| 682 | ** compile-time whether or not thread A can override locks on thread B. |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 683 | ** One has to do a run-time check to discover the behavior of the |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 684 | ** current process. |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 685 | ** |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 686 | ** SQLite used to support LinuxThreads. But support for LinuxThreads |
| 687 | ** was dropped beginning with version 3.7.0. SQLite will still work with |
| 688 | ** LinuxThreads provided that (1) there is no more than one connection |
| 689 | ** per database file in the same process and (2) database connections |
| 690 | ** do not move across threads. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 691 | */ |
| 692 | |
| 693 | /* |
| 694 | ** An instance of the following structure serves as the key used |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 695 | ** to locate a particular unixInodeInfo object. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 696 | */ |
| 697 | struct unixFileId { |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 698 | dev_t dev; /* Device number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 699 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 700 | struct vxworksFileId *pId; /* Unique file ID for vxworks. */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 701 | #else |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 702 | ino_t ino; /* Inode number */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 703 | #endif |
| 704 | }; |
| 705 | |
| 706 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 707 | ** An instance of the following structure is allocated for each open |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 708 | ** inode. Or, on LinuxThreads, there is one of these structures for |
| 709 | ** each inode opened by each thread. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 710 | ** |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 711 | ** A single inode can have multiple file descriptors, so each unixFile |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 712 | ** structure contains a pointer to an instance of this object and this |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 713 | ** object keeps a count of the number of unixFile pointing to it. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 714 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 715 | struct unixInodeInfo { |
| 716 | struct unixFileId fileId; /* The lookup key */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 717 | int nShared; /* Number of SHARED locks held */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 718 | int eFileLock; /* One of SHARED_LOCK, RESERVED_LOCK etc. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 719 | int nRef; /* Number of pointers to this structure */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 720 | unixShmNode *pShmNode; /* Shared memory associated with this inode */ |
| 721 | int nLock; /* Number of outstanding file locks */ |
| 722 | UnixUnusedFd *pUnused; /* Unused file descriptors to close */ |
| 723 | unixInodeInfo *pNext; /* List of all unixInodeInfo objects */ |
| 724 | unixInodeInfo *pPrev; /* .... doubly linked */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 725 | #if defined(SQLITE_ENABLE_LOCKING_STYLE) |
| 726 | unsigned long long sharedByte; /* for AFP simulated shared lock */ |
| 727 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 728 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 729 | sem_t *pSem; /* Named POSIX semaphore */ |
| 730 | char aSemName[MAX_PATHNAME+2]; /* Name of that semaphore */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 731 | #endif |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 732 | }; |
| 733 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 734 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 735 | ** A lists of all unixInodeInfo objects. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 736 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 737 | static unixInodeInfo *inodeList = 0; |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 738 | |
drh | 5fdae77 | 2004-06-29 03:29:00 +0000 | [diff] [blame] | 739 | /* |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 740 | ** Close all file descriptors accumuated in the unixInodeInfo->pUnused list. |
| 741 | ** If all such file descriptors are closed without error, the list is |
| 742 | ** cleared and SQLITE_OK returned. |
| 743 | ** |
| 744 | ** Otherwise, if an error occurs, then successfully closed file descriptor |
| 745 | ** entries are removed from the list, and SQLITE_IOERR_CLOSE returned. |
| 746 | ** not deleted and SQLITE_IOERR_CLOSE returned. |
| 747 | */ |
| 748 | static int closePendingFds(unixFile *pFile){ |
| 749 | int rc = SQLITE_OK; |
| 750 | unixInodeInfo *pInode = pFile->pInode; |
| 751 | UnixUnusedFd *pError = 0; |
| 752 | UnixUnusedFd *p; |
| 753 | UnixUnusedFd *pNext; |
| 754 | for(p=pInode->pUnused; p; p=pNext){ |
| 755 | pNext = p->pNext; |
| 756 | if( close(p->fd) ){ |
| 757 | pFile->lastErrno = errno; |
| 758 | rc = SQLITE_IOERR_CLOSE; |
| 759 | p->pNext = pError; |
| 760 | pError = p; |
| 761 | }else{ |
| 762 | sqlite3_free(p); |
| 763 | } |
| 764 | } |
| 765 | pInode->pUnused = pError; |
| 766 | return rc; |
| 767 | } |
| 768 | |
| 769 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 770 | ** Release a unixInodeInfo structure previously allocated by findInodeInfo(). |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 771 | ** |
| 772 | ** The mutex entered using the unixEnterMutex() function must be held |
| 773 | ** when this function is called. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 774 | */ |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 775 | static void releaseInodeInfo(unixFile *pFile){ |
| 776 | unixInodeInfo *pInode = pFile->pInode; |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 777 | assert( unixMutexHeld() ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 778 | if( pInode ){ |
| 779 | pInode->nRef--; |
| 780 | if( pInode->nRef==0 ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 781 | assert( pInode->pShmNode==0 ); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 782 | closePendingFds(pFile); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 783 | if( pInode->pPrev ){ |
| 784 | assert( pInode->pPrev->pNext==pInode ); |
| 785 | pInode->pPrev->pNext = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 786 | }else{ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 787 | assert( inodeList==pInode ); |
| 788 | inodeList = pInode->pNext; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 789 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 790 | if( pInode->pNext ){ |
| 791 | assert( pInode->pNext->pPrev==pInode ); |
| 792 | pInode->pNext->pPrev = pInode->pPrev; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 793 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 794 | sqlite3_free(pInode); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 795 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 796 | } |
| 797 | } |
| 798 | |
| 799 | /* |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 800 | ** Given a file descriptor, locate the unixInodeInfo object that |
| 801 | ** describes that file descriptor. Create a new one if necessary. The |
| 802 | ** return value might be uninitialized if an error occurs. |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 803 | ** |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 804 | ** The mutex entered using the unixEnterMutex() function must be held |
| 805 | ** when this function is called. |
| 806 | ** |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 807 | ** Return an appropriate error code. |
| 808 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 809 | static int findInodeInfo( |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 810 | unixFile *pFile, /* Unix file with file desc used in the key */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 811 | unixInodeInfo **ppInode /* Return the unixInodeInfo object here */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 812 | ){ |
| 813 | int rc; /* System call return code */ |
| 814 | int fd; /* The file descriptor for pFile */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 815 | struct unixFileId fileId; /* Lookup key for the unixInodeInfo */ |
| 816 | struct stat statbuf; /* Low-level file information */ |
| 817 | unixInodeInfo *pInode = 0; /* Candidate unixInodeInfo object */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 818 | |
dan | 9359c7b | 2009-08-21 08:29:10 +0000 | [diff] [blame] | 819 | assert( unixMutexHeld() ); |
| 820 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 821 | /* Get low-level information about the file that we can used to |
| 822 | ** create a unique name for the file. |
| 823 | */ |
| 824 | fd = pFile->h; |
| 825 | rc = fstat(fd, &statbuf); |
| 826 | if( rc!=0 ){ |
| 827 | pFile->lastErrno = errno; |
| 828 | #ifdef EOVERFLOW |
| 829 | if( pFile->lastErrno==EOVERFLOW ) return SQLITE_NOLFS; |
| 830 | #endif |
| 831 | return SQLITE_IOERR; |
| 832 | } |
| 833 | |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 834 | #ifdef __APPLE__ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 835 | /* On OS X on an msdos filesystem, the inode number is reported |
| 836 | ** incorrectly for zero-size files. See ticket #3260. To work |
| 837 | ** around this problem (we consider it a bug in OS X, not SQLite) |
| 838 | ** we always increase the file size to 1 by writing a single byte |
| 839 | ** prior to accessing the inode number. The one byte written is |
| 840 | ** an ASCII 'S' character which also happens to be the first byte |
| 841 | ** in the header of every SQLite database. In this way, if there |
| 842 | ** is a race condition such that another thread has already populated |
| 843 | ** the first page of the database, no damage is done. |
| 844 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 845 | if( statbuf.st_size==0 && (pFile->fsFlags & SQLITE_FSFLAGS_IS_MSDOS)!=0 ){ |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 846 | rc = write(fd, "S", 1); |
| 847 | if( rc!=1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 848 | pFile->lastErrno = errno; |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 849 | return SQLITE_IOERR; |
| 850 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 851 | rc = fstat(fd, &statbuf); |
| 852 | if( rc!=0 ){ |
| 853 | pFile->lastErrno = errno; |
| 854 | return SQLITE_IOERR; |
| 855 | } |
| 856 | } |
drh | eb0d74f | 2009-02-03 15:27:02 +0000 | [diff] [blame] | 857 | #endif |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 858 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 859 | memset(&fileId, 0, sizeof(fileId)); |
| 860 | fileId.dev = statbuf.st_dev; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 861 | #if OS_VXWORKS |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 862 | fileId.pId = pFile->pId; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 863 | #else |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 864 | fileId.ino = statbuf.st_ino; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 865 | #endif |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 866 | pInode = inodeList; |
| 867 | while( pInode && memcmp(&fileId, &pInode->fileId, sizeof(fileId)) ){ |
| 868 | pInode = pInode->pNext; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 869 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 870 | if( pInode==0 ){ |
| 871 | pInode = sqlite3_malloc( sizeof(*pInode) ); |
| 872 | if( pInode==0 ){ |
| 873 | return SQLITE_NOMEM; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 874 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 875 | memset(pInode, 0, sizeof(*pInode)); |
| 876 | memcpy(&pInode->fileId, &fileId, sizeof(fileId)); |
| 877 | pInode->nRef = 1; |
| 878 | pInode->pNext = inodeList; |
| 879 | pInode->pPrev = 0; |
| 880 | if( inodeList ) inodeList->pPrev = pInode; |
| 881 | inodeList = pInode; |
| 882 | }else{ |
| 883 | pInode->nRef++; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 884 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 885 | *ppInode = pInode; |
| 886 | return SQLITE_OK; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 887 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 888 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 889 | |
| 890 | /* |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 891 | ** This routine checks if there is a RESERVED lock held on the specified |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 892 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 893 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 894 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 895 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 896 | static int unixCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 897 | int rc = SQLITE_OK; |
| 898 | int reserved = 0; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 899 | unixFile *pFile = (unixFile*)id; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 900 | |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 901 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 902 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 903 | assert( pFile ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 904 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 905 | |
| 906 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 907 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 908 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 909 | } |
| 910 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 911 | /* Otherwise see if some other process holds it. |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 912 | */ |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 913 | #ifndef __DJGPP__ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 914 | if( !reserved ){ |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 915 | struct flock lock; |
| 916 | lock.l_whence = SEEK_SET; |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 917 | lock.l_start = RESERVED_BYTE; |
| 918 | lock.l_len = 1; |
| 919 | lock.l_type = F_WRLCK; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 920 | if (-1 == fcntl(pFile->h, F_GETLK, &lock)) { |
| 921 | int tErrno = errno; |
| 922 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 923 | pFile->lastErrno = tErrno; |
| 924 | } else if( lock.l_type!=F_UNLCK ){ |
| 925 | reserved = 1; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 926 | } |
| 927 | } |
danielk1977 | 09480a9 | 2009-02-09 05:32:32 +0000 | [diff] [blame] | 928 | #endif |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 929 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 930 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 931 | OSTRACE(("TEST WR-LOCK %d %d %d (unix)\n", pFile->h, rc, reserved)); |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 932 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 933 | *pResOut = reserved; |
| 934 | return rc; |
danielk1977 | 13adf8a | 2004-06-03 16:08:41 +0000 | [diff] [blame] | 935 | } |
| 936 | |
| 937 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 938 | ** Lock the file with the lock specified by parameter eFileLock - one |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 939 | ** of the following: |
| 940 | ** |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 941 | ** (1) SHARED_LOCK |
| 942 | ** (2) RESERVED_LOCK |
| 943 | ** (3) PENDING_LOCK |
| 944 | ** (4) EXCLUSIVE_LOCK |
| 945 | ** |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 946 | ** Sometimes when requesting one lock state, additional lock states |
| 947 | ** are inserted in between. The locking might fail on one of the later |
| 948 | ** transitions leaving the lock state different from what it started but |
| 949 | ** still short of its goal. The following chart shows the allowed |
| 950 | ** transitions and the inserted intermediate states: |
| 951 | ** |
| 952 | ** UNLOCKED -> SHARED |
| 953 | ** SHARED -> RESERVED |
| 954 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 955 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 956 | ** PENDING -> EXCLUSIVE |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 957 | ** |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 958 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 959 | ** routine to lower a locking level. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 960 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 961 | static int unixLock(sqlite3_file *id, int eFileLock){ |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 962 | /* The following describes the implementation of the various locks and |
| 963 | ** lock transitions in terms of the POSIX advisory shared and exclusive |
| 964 | ** lock primitives (called read-locks and write-locks below, to avoid |
| 965 | ** confusion with SQLite lock names). The algorithms are complicated |
| 966 | ** slightly in order to be compatible with windows systems simultaneously |
| 967 | ** accessing the same database file, in case that is ever required. |
| 968 | ** |
| 969 | ** Symbols defined in os.h indentify the 'pending byte' and the 'reserved |
| 970 | ** byte', each single bytes at well known offsets, and the 'shared byte |
| 971 | ** range', a range of 510 bytes at a well known offset. |
| 972 | ** |
| 973 | ** To obtain a SHARED lock, a read-lock is obtained on the 'pending |
| 974 | ** byte'. If this is successful, a random byte from the 'shared byte |
| 975 | ** range' is read-locked and the lock on the 'pending byte' released. |
| 976 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 977 | ** A process may only obtain a RESERVED lock after it has a SHARED lock. |
| 978 | ** A RESERVED lock is implemented by grabbing a write-lock on the |
| 979 | ** 'reserved byte'. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 980 | ** |
| 981 | ** A process may only obtain a PENDING lock after it has obtained a |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 982 | ** SHARED lock. A PENDING lock is implemented by obtaining a write-lock |
| 983 | ** on the 'pending byte'. This ensures that no new SHARED locks can be |
| 984 | ** obtained, but existing SHARED locks are allowed to persist. A process |
| 985 | ** does not have to obtain a RESERVED lock on the way to a PENDING lock. |
| 986 | ** This property is used by the algorithm for rolling back a journal file |
| 987 | ** after a crash. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 988 | ** |
danielk1977 | 90ba3bd | 2004-06-25 08:32:25 +0000 | [diff] [blame] | 989 | ** An EXCLUSIVE lock, obtained after a PENDING lock is held, is |
| 990 | ** implemented by obtaining a write-lock on the entire 'shared byte |
| 991 | ** range'. Since all other locks require a read-lock on one of the bytes |
| 992 | ** within this range, this ensures that no other locks are held on the |
| 993 | ** database. |
danielk1977 | f42f25c | 2004-06-25 07:21:28 +0000 | [diff] [blame] | 994 | ** |
| 995 | ** The reason a single byte cannot be used instead of the 'shared byte |
| 996 | ** range' is that some versions of windows do not support read-locks. By |
| 997 | ** locking a random byte from a range, concurrent SHARED locks may exist |
| 998 | ** even if the locking primitive used is always a write-lock. |
| 999 | */ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1000 | int rc = SQLITE_OK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1001 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1002 | unixInodeInfo *pInode = pFile->pInode; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1003 | struct flock lock; |
drh | 3f02218 | 2009-09-09 16:10:50 +0000 | [diff] [blame] | 1004 | int s = 0; |
drh | 383d30f | 2010-02-26 13:07:37 +0000 | [diff] [blame] | 1005 | int tErrno = 0; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1006 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1007 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1008 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (unix)\n", pFile->h, |
| 1009 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1010 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1011 | |
| 1012 | /* If there is already a lock of this type or more restrictive on the |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1013 | ** unixFile, do nothing. Don't use the end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1014 | ** unixEnterMutex() hasn't been called yet. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1015 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1016 | if( pFile->eFileLock>=eFileLock ){ |
| 1017 | OSTRACE(("LOCK %d %s ok (already held) (unix)\n", pFile->h, |
| 1018 | azFileLock(eFileLock))); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1019 | return SQLITE_OK; |
| 1020 | } |
| 1021 | |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1022 | /* Make sure the locking sequence is correct. |
| 1023 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 1024 | ** (2) SQLite never explicitly requests a pendig lock. |
| 1025 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1026 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1027 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 1028 | assert( eFileLock!=PENDING_LOCK ); |
| 1029 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1030 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1031 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | b3e0434 | 2004-06-08 00:47:47 +0000 | [diff] [blame] | 1032 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1033 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1034 | pInode = pFile->pInode; |
drh | 029b44b | 2006-01-15 00:13:15 +0000 | [diff] [blame] | 1035 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 1036 | /* If some thread using this PID has a lock via a different unixFile* |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1037 | ** handle that precludes the requested lock, return BUSY. |
| 1038 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1039 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 1040 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1041 | ){ |
| 1042 | rc = SQLITE_BUSY; |
| 1043 | goto end_lock; |
| 1044 | } |
| 1045 | |
| 1046 | /* If a SHARED lock is requested, and some thread using this PID already |
| 1047 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 1048 | ** return SQLITE_OK. |
| 1049 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1050 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1051 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1052 | assert( eFileLock==SHARED_LOCK ); |
| 1053 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1054 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1055 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1056 | pInode->nShared++; |
| 1057 | pInode->nLock++; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1058 | goto end_lock; |
| 1059 | } |
| 1060 | |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1061 | |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1062 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
| 1063 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 1064 | ** be released. |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1065 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1066 | lock.l_len = 1L; |
| 1067 | lock.l_whence = SEEK_SET; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1068 | if( eFileLock==SHARED_LOCK |
| 1069 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1070 | ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1071 | lock.l_type = (eFileLock==SHARED_LOCK?F_RDLCK:F_WRLCK); |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1072 | lock.l_start = PENDING_BYTE; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1073 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1074 | if( s==(-1) ){ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1075 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1076 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1077 | if( IS_LOCK_ERROR(rc) ){ |
| 1078 | pFile->lastErrno = tErrno; |
| 1079 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1080 | goto end_lock; |
| 1081 | } |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1082 | } |
| 1083 | |
| 1084 | |
| 1085 | /* If control gets to this point, then actually go ahead and make |
| 1086 | ** operating system calls for the specified lock. |
| 1087 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1088 | if( eFileLock==SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1089 | assert( pInode->nShared==0 ); |
| 1090 | assert( pInode->eFileLock==0 ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1091 | |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1092 | /* Now get the read-lock */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1093 | lock.l_start = SHARED_FIRST; |
| 1094 | lock.l_len = SHARED_SIZE; |
| 1095 | if( (s = fcntl(pFile->h, F_SETLK, &lock))==(-1) ){ |
| 1096 | tErrno = errno; |
| 1097 | } |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1098 | /* Drop the temporary PENDING lock */ |
| 1099 | lock.l_start = PENDING_BYTE; |
| 1100 | lock.l_len = 1L; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1101 | lock.l_type = F_UNLCK; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1102 | if( fcntl(pFile->h, F_SETLK, &lock)!=0 ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1103 | if( s != -1 ){ |
| 1104 | /* This could happen with a network mount */ |
| 1105 | tErrno = errno; |
| 1106 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1107 | if( IS_LOCK_ERROR(rc) ){ |
| 1108 | pFile->lastErrno = tErrno; |
| 1109 | } |
| 1110 | goto end_lock; |
| 1111 | } |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1112 | } |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1113 | if( s==(-1) ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1114 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1115 | if( IS_LOCK_ERROR(rc) ){ |
| 1116 | pFile->lastErrno = tErrno; |
| 1117 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1118 | }else{ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1119 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1120 | pInode->nLock++; |
| 1121 | pInode->nShared = 1; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1122 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1123 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1124 | /* We are trying for an exclusive lock but another thread in this |
| 1125 | ** same process is still holding a shared lock. */ |
| 1126 | rc = SQLITE_BUSY; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1127 | }else{ |
drh | 3cde3bb | 2004-06-12 02:17:14 +0000 | [diff] [blame] | 1128 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1129 | ** assumed that there is a SHARED or greater lock on the file |
| 1130 | ** already. |
| 1131 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1132 | assert( 0!=pFile->eFileLock ); |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1133 | lock.l_type = F_WRLCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1134 | switch( eFileLock ){ |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1135 | case RESERVED_LOCK: |
drh | 2ac3ee9 | 2004-06-07 16:27:46 +0000 | [diff] [blame] | 1136 | lock.l_start = RESERVED_BYTE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1137 | break; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1138 | case EXCLUSIVE_LOCK: |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1139 | lock.l_start = SHARED_FIRST; |
| 1140 | lock.l_len = SHARED_SIZE; |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | default: |
| 1143 | assert(0); |
| 1144 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1145 | s = fcntl(pFile->h, F_SETLK, &lock); |
drh | e2396a1 | 2007-03-29 20:19:58 +0000 | [diff] [blame] | 1146 | if( s==(-1) ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1147 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1148 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1149 | if( IS_LOCK_ERROR(rc) ){ |
| 1150 | pFile->lastErrno = tErrno; |
| 1151 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1152 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1153 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1154 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1155 | |
| 1156 | #ifndef NDEBUG |
| 1157 | /* Set up the transaction-counter change checking flags when |
| 1158 | ** transitioning from a SHARED to a RESERVED lock. The change |
| 1159 | ** from SHARED to RESERVED marks the beginning of a normal |
| 1160 | ** write operation (not a hot journal rollback). |
| 1161 | */ |
| 1162 | if( rc==SQLITE_OK |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1163 | && pFile->eFileLock<=SHARED_LOCK |
| 1164 | && eFileLock==RESERVED_LOCK |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1165 | ){ |
| 1166 | pFile->transCntrChng = 0; |
| 1167 | pFile->dbUpdate = 0; |
| 1168 | pFile->inNormalWrite = 1; |
| 1169 | } |
| 1170 | #endif |
| 1171 | |
| 1172 | |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1173 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1174 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1175 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1176 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 1177 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1178 | pInode->eFileLock = PENDING_LOCK; |
danielk1977 | ecb2a96 | 2004-06-02 06:30:16 +0000 | [diff] [blame] | 1179 | } |
danielk1977 | 9a1d0ab | 2004-06-01 14:09:28 +0000 | [diff] [blame] | 1180 | |
| 1181 | end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1182 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1183 | OSTRACE(("LOCK %d %s %s (unix)\n", pFile->h, azFileLock(eFileLock), |
| 1184 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1185 | return rc; |
| 1186 | } |
| 1187 | |
| 1188 | /* |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1189 | ** Add the file descriptor used by file handle pFile to the corresponding |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1190 | ** pUnused list. |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1191 | */ |
| 1192 | static void setPendingFd(unixFile *pFile){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1193 | unixInodeInfo *pInode = pFile->pInode; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1194 | UnixUnusedFd *p = pFile->pUnused; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1195 | p->pNext = pInode->pUnused; |
| 1196 | pInode->pUnused = p; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1197 | pFile->h = -1; |
| 1198 | pFile->pUnused = 0; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1202 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1203 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1204 | ** |
| 1205 | ** If the locking level of the file descriptor is already at or below |
| 1206 | ** the requested locking level, this routine is a no-op. |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1207 | ** |
| 1208 | ** If handleNFSUnlock is true, then on downgrading an EXCLUSIVE_LOCK to SHARED |
| 1209 | ** the byte range is divided into 2 parts and the first part is unlocked then |
| 1210 | ** set to a read lock, then the other part is simply unlocked. This works |
| 1211 | ** around a bug in BSD NFS lockd (also seen on MacOSX 10.3+) that fails to |
| 1212 | ** remove the write lock on a region when a read lock is set. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1213 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1214 | static int _posixUnlock(sqlite3_file *id, int eFileLock, int handleNFSUnlock){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1215 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 1216 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1217 | struct flock lock; |
| 1218 | int rc = SQLITE_OK; |
| 1219 | int h; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1220 | int tErrno; /* Error code from system call errors */ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1221 | |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 1222 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1223 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (unix)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1224 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1225 | getpid())); |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1226 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1227 | assert( eFileLock<=SHARED_LOCK ); |
| 1228 | if( pFile->eFileLock<=eFileLock ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1229 | return SQLITE_OK; |
| 1230 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1231 | unixEnterMutex(); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1232 | h = pFile->h; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1233 | pInode = pFile->pInode; |
| 1234 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1235 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1236 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1237 | SimulateIOErrorBenign(1); |
| 1238 | SimulateIOError( h=(-1) ) |
| 1239 | SimulateIOErrorBenign(0); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1240 | |
| 1241 | #ifndef NDEBUG |
| 1242 | /* When reducing a lock such that other processes can start |
| 1243 | ** reading the database file again, make sure that the |
| 1244 | ** transaction counter was updated if any part of the database |
| 1245 | ** file changed. If the transaction counter is not updated, |
| 1246 | ** other connections to the same file might not realize that |
| 1247 | ** the file has changed and hence might not know to flush their |
| 1248 | ** cache. The use of a stale cache can lead to database corruption. |
| 1249 | */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1250 | #if 0 |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1251 | assert( pFile->inNormalWrite==0 |
| 1252 | || pFile->dbUpdate==0 |
| 1253 | || pFile->transCntrChng==1 ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 1254 | #endif |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 1255 | pFile->inNormalWrite = 0; |
| 1256 | #endif |
| 1257 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1258 | /* downgrading to a shared lock on NFS involves clearing the write lock |
| 1259 | ** before establishing the readlock - to avoid a race condition we downgrade |
| 1260 | ** the lock in 2 blocks, so that part of the range will be covered by a |
| 1261 | ** write lock until the rest is covered by a read lock: |
| 1262 | ** 1: [WWWWW] |
| 1263 | ** 2: [....W] |
| 1264 | ** 3: [RRRRW] |
| 1265 | ** 4: [RRRR.] |
| 1266 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1267 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1268 | if( handleNFSUnlock ){ |
| 1269 | off_t divSize = SHARED_SIZE - 1; |
| 1270 | |
| 1271 | lock.l_type = F_UNLCK; |
| 1272 | lock.l_whence = SEEK_SET; |
| 1273 | lock.l_start = SHARED_FIRST; |
| 1274 | lock.l_len = divSize; |
| 1275 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1276 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1277 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1278 | if( IS_LOCK_ERROR(rc) ){ |
| 1279 | pFile->lastErrno = tErrno; |
| 1280 | } |
| 1281 | goto end_unlock; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1282 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1283 | lock.l_type = F_RDLCK; |
| 1284 | lock.l_whence = SEEK_SET; |
| 1285 | lock.l_start = SHARED_FIRST; |
| 1286 | lock.l_len = divSize; |
| 1287 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1288 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1289 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1290 | if( IS_LOCK_ERROR(rc) ){ |
| 1291 | pFile->lastErrno = tErrno; |
| 1292 | } |
| 1293 | goto end_unlock; |
| 1294 | } |
| 1295 | lock.l_type = F_UNLCK; |
| 1296 | lock.l_whence = SEEK_SET; |
| 1297 | lock.l_start = SHARED_FIRST+divSize; |
| 1298 | lock.l_len = SHARED_SIZE-divSize; |
| 1299 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1300 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1301 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1302 | if( IS_LOCK_ERROR(rc) ){ |
| 1303 | pFile->lastErrno = tErrno; |
| 1304 | } |
| 1305 | goto end_unlock; |
| 1306 | } |
| 1307 | }else{ |
| 1308 | lock.l_type = F_RDLCK; |
| 1309 | lock.l_whence = SEEK_SET; |
| 1310 | lock.l_start = SHARED_FIRST; |
| 1311 | lock.l_len = SHARED_SIZE; |
| 1312 | if( fcntl(h, F_SETLK, &lock)==(-1) ){ |
drh | c05a9a8 | 2010-03-04 16:12:34 +0000 | [diff] [blame] | 1313 | tErrno = errno; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1314 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_RDLOCK); |
| 1315 | if( IS_LOCK_ERROR(rc) ){ |
| 1316 | pFile->lastErrno = tErrno; |
| 1317 | } |
| 1318 | goto end_unlock; |
| 1319 | } |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1322 | lock.l_type = F_UNLCK; |
| 1323 | lock.l_whence = SEEK_SET; |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1324 | lock.l_start = PENDING_BYTE; |
| 1325 | lock.l_len = 2L; assert( PENDING_BYTE+1==RESERVED_BYTE ); |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1326 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1327 | pInode->eFileLock = SHARED_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1328 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1329 | tErrno = errno; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1330 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1331 | if( IS_LOCK_ERROR(rc) ){ |
| 1332 | pFile->lastErrno = tErrno; |
| 1333 | } |
drh | cd731cf | 2009-03-28 23:23:02 +0000 | [diff] [blame] | 1334 | goto end_unlock; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1335 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1336 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1337 | if( eFileLock==NO_LOCK ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1338 | /* Decrement the shared lock counter. Release the lock using an |
| 1339 | ** OS call only when all threads in this same process have released |
| 1340 | ** the lock. |
| 1341 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1342 | pInode->nShared--; |
| 1343 | if( pInode->nShared==0 ){ |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1344 | lock.l_type = F_UNLCK; |
| 1345 | lock.l_whence = SEEK_SET; |
| 1346 | lock.l_start = lock.l_len = 0L; |
drh | 1aa5af1 | 2008-03-07 19:51:14 +0000 | [diff] [blame] | 1347 | SimulateIOErrorBenign(1); |
| 1348 | SimulateIOError( h=(-1) ) |
| 1349 | SimulateIOErrorBenign(0); |
| 1350 | if( fcntl(h, F_SETLK, &lock)!=(-1) ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1351 | pInode->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1352 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1353 | tErrno = errno; |
danielk1977 | 5ad6a88 | 2008-09-15 04:20:31 +0000 | [diff] [blame] | 1354 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1355 | if( IS_LOCK_ERROR(rc) ){ |
| 1356 | pFile->lastErrno = tErrno; |
| 1357 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1358 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1359 | pFile->eFileLock = NO_LOCK; |
drh | 2b4b596 | 2005-06-15 17:47:55 +0000 | [diff] [blame] | 1360 | } |
drh | a6abd04 | 2004-06-09 17:37:22 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1363 | /* Decrement the count of locks against this same file. When the |
| 1364 | ** count reaches zero, close any other file descriptors whose close |
| 1365 | ** was deferred because of outstanding locks. |
| 1366 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1367 | pInode->nLock--; |
| 1368 | assert( pInode->nLock>=0 ); |
| 1369 | if( pInode->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1370 | int rc2 = closePendingFds(pFile); |
| 1371 | if( rc==SQLITE_OK ){ |
| 1372 | rc = rc2; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1373 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1374 | } |
| 1375 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 1376 | |
| 1377 | end_unlock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1378 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1379 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | 9c105bb | 2004-10-02 20:38:28 +0000 | [diff] [blame] | 1380 | return rc; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1384 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1385 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1386 | ** |
| 1387 | ** If the locking level of the file descriptor is already at or below |
| 1388 | ** the requested locking level, this routine is a no-op. |
| 1389 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1390 | static int unixUnlock(sqlite3_file *id, int eFileLock){ |
| 1391 | return _posixUnlock(id, eFileLock, 0); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 1392 | } |
| 1393 | |
| 1394 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1395 | ** This function performs the parts of the "close file" operation |
| 1396 | ** common to all locking schemes. It closes the directory and file |
| 1397 | ** handles, if they are valid, and sets all fields of the unixFile |
| 1398 | ** structure to 0. |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1399 | ** |
| 1400 | ** It is *not* necessary to hold the mutex when this routine is called, |
| 1401 | ** even on VxWorks. A mutex will be acquired on VxWorks by the |
| 1402 | ** vxworksReleaseFileId() routine. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1403 | */ |
| 1404 | static int closeUnixFile(sqlite3_file *id){ |
| 1405 | unixFile *pFile = (unixFile*)id; |
| 1406 | if( pFile ){ |
| 1407 | if( pFile->dirfd>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1408 | int err = close(pFile->dirfd); |
| 1409 | if( err ){ |
| 1410 | pFile->lastErrno = errno; |
| 1411 | return SQLITE_IOERR_DIR_CLOSE; |
| 1412 | }else{ |
| 1413 | pFile->dirfd=-1; |
| 1414 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1415 | } |
| 1416 | if( pFile->h>=0 ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1417 | int err = close(pFile->h); |
| 1418 | if( err ){ |
| 1419 | pFile->lastErrno = errno; |
| 1420 | return SQLITE_IOERR_CLOSE; |
| 1421 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1422 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1423 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1424 | if( pFile->pId ){ |
| 1425 | if( pFile->isDelete ){ |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1426 | unlink(pFile->pId->zCanonicalName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1427 | } |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 1428 | vxworksReleaseFileId(pFile->pId); |
| 1429 | pFile->pId = 0; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 1430 | } |
| 1431 | #endif |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1432 | OSTRACE(("CLOSE %-3d\n", pFile->h)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1433 | OpenCounter(-1); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1434 | sqlite3_free(pFile->pUnused); |
drh | ff59a11 | 2010-05-14 20:15:51 +0000 | [diff] [blame] | 1435 | memset(pFile, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1436 | } |
| 1437 | return SQLITE_OK; |
| 1438 | } |
| 1439 | |
| 1440 | /* |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1441 | ** Close a file. |
| 1442 | */ |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 1443 | static int unixClose(sqlite3_file *id){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1444 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1445 | if( id ){ |
| 1446 | unixFile *pFile = (unixFile *)id; |
| 1447 | unixUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1448 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1449 | if( pFile->pInode && pFile->pInode->nLock ){ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1450 | /* If there are outstanding locks, do not actually close the file just |
| 1451 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1452 | ** descriptor to pInode->pUnused list. It will be automatically closed |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 1453 | ** when the last lock is cleared. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 1454 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 1455 | setPendingFd(pFile); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1456 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 1457 | releaseInodeInfo(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1458 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 1459 | unixLeaveMutex(); |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1460 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 1461 | return rc; |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 1462 | } |
| 1463 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1464 | /************** End of the posix advisory lock implementation ***************** |
| 1465 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1466 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1467 | /****************************************************************************** |
| 1468 | ****************************** No-op Locking ********************************** |
| 1469 | ** |
| 1470 | ** Of the various locking implementations available, this is by far the |
| 1471 | ** simplest: locking is ignored. No attempt is made to lock the database |
| 1472 | ** file for reading or writing. |
| 1473 | ** |
| 1474 | ** This locking mode is appropriate for use on read-only databases |
| 1475 | ** (ex: databases that are burned into CD-ROM, for example.) It can |
| 1476 | ** also be used if the application employs some external mechanism to |
| 1477 | ** prevent simultaneous access of the same database by two or more |
| 1478 | ** database connections. But there is a serious risk of database |
| 1479 | ** corruption if this locking mode is used in situations where multiple |
| 1480 | ** database connections are accessing the same database file at the same |
| 1481 | ** time and one or more of those connections are writing. |
| 1482 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 1483 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1484 | static int nolockCheckReservedLock(sqlite3_file *NotUsed, int *pResOut){ |
| 1485 | UNUSED_PARAMETER(NotUsed); |
| 1486 | *pResOut = 0; |
| 1487 | return SQLITE_OK; |
| 1488 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1489 | static int nolockLock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1490 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1491 | return SQLITE_OK; |
| 1492 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1493 | static int nolockUnlock(sqlite3_file *NotUsed, int NotUsed2){ |
| 1494 | UNUSED_PARAMETER2(NotUsed, NotUsed2); |
| 1495 | return SQLITE_OK; |
| 1496 | } |
| 1497 | |
| 1498 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1499 | ** Close the file. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1500 | */ |
| 1501 | static int nolockClose(sqlite3_file *id) { |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1502 | return closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1503 | } |
| 1504 | |
| 1505 | /******************* End of the no-op lock implementation ********************* |
| 1506 | ******************************************************************************/ |
| 1507 | |
| 1508 | /****************************************************************************** |
| 1509 | ************************* Begin dot-file Locking ****************************** |
| 1510 | ** |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 1511 | ** The dotfile locking implementation uses the existance of separate lock |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1512 | ** files in order to control access to the database. This works on just |
| 1513 | ** about every filesystem imaginable. But there are serious downsides: |
| 1514 | ** |
| 1515 | ** (1) There is zero concurrency. A single reader blocks all other |
| 1516 | ** connections from reading or writing the database. |
| 1517 | ** |
| 1518 | ** (2) An application crash or power loss can leave stale lock files |
| 1519 | ** sitting around that need to be cleared manually. |
| 1520 | ** |
| 1521 | ** Nevertheless, a dotlock is an appropriate locking mode for use if no |
| 1522 | ** other locking strategy is available. |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1523 | ** |
| 1524 | ** Dotfile locking works by creating a file in the same directory as the |
| 1525 | ** database and with the same name but with a ".lock" extension added. |
| 1526 | ** The existance of a lock file implies an EXCLUSIVE lock. All other lock |
| 1527 | ** types (SHARED, RESERVED, PENDING) are mapped into EXCLUSIVE. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1528 | */ |
| 1529 | |
| 1530 | /* |
| 1531 | ** The file suffix added to the data base filename in order to create the |
| 1532 | ** lock file. |
| 1533 | */ |
| 1534 | #define DOTLOCK_SUFFIX ".lock" |
| 1535 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1536 | /* |
| 1537 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1538 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1539 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1540 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1541 | ** |
| 1542 | ** In dotfile locking, either a lock exists or it does not. So in this |
| 1543 | ** variation of CheckReservedLock(), *pResOut is set to true if any lock |
| 1544 | ** is held on the file and false if the file is unlocked. |
| 1545 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1546 | static int dotlockCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1547 | int rc = SQLITE_OK; |
| 1548 | int reserved = 0; |
| 1549 | unixFile *pFile = (unixFile*)id; |
| 1550 | |
| 1551 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1552 | |
| 1553 | assert( pFile ); |
| 1554 | |
| 1555 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1556 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1557 | /* Either this connection or some other connection in the same process |
| 1558 | ** holds a lock on the file. No need to check further. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1559 | reserved = 1; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1560 | }else{ |
| 1561 | /* The lock is held if and only if the lockfile exists */ |
| 1562 | const char *zLockFile = (const char*)pFile->lockingContext; |
| 1563 | reserved = access(zLockFile, 0)==0; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1564 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1565 | OSTRACE(("TEST WR-LOCK %d %d %d (dotlock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1566 | *pResOut = reserved; |
| 1567 | return rc; |
| 1568 | } |
| 1569 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1570 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1571 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1572 | ** of the following: |
| 1573 | ** |
| 1574 | ** (1) SHARED_LOCK |
| 1575 | ** (2) RESERVED_LOCK |
| 1576 | ** (3) PENDING_LOCK |
| 1577 | ** (4) EXCLUSIVE_LOCK |
| 1578 | ** |
| 1579 | ** Sometimes when requesting one lock state, additional lock states |
| 1580 | ** are inserted in between. The locking might fail on one of the later |
| 1581 | ** transitions leaving the lock state different from what it started but |
| 1582 | ** still short of its goal. The following chart shows the allowed |
| 1583 | ** transitions and the inserted intermediate states: |
| 1584 | ** |
| 1585 | ** UNLOCKED -> SHARED |
| 1586 | ** SHARED -> RESERVED |
| 1587 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1588 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1589 | ** PENDING -> EXCLUSIVE |
| 1590 | ** |
| 1591 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1592 | ** routine to lower a locking level. |
| 1593 | ** |
| 1594 | ** With dotfile locking, we really only support state (4): EXCLUSIVE. |
| 1595 | ** But we track the other locking levels internally. |
| 1596 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1597 | static int dotlockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1598 | unixFile *pFile = (unixFile*)id; |
| 1599 | int fd; |
| 1600 | char *zLockFile = (char *)pFile->lockingContext; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1601 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1602 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1603 | |
| 1604 | /* If we have any lock, then the lock file already exists. All we have |
| 1605 | ** to do is adjust our internal record of the lock level. |
| 1606 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1607 | if( pFile->eFileLock > NO_LOCK ){ |
| 1608 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1609 | #if !OS_VXWORKS |
| 1610 | /* Always update the timestamp on the old file */ |
| 1611 | utimes(zLockFile, NULL); |
| 1612 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1613 | return SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | /* grab an exclusive lock */ |
| 1617 | fd = open(zLockFile,O_RDONLY|O_CREAT|O_EXCL,0600); |
| 1618 | if( fd<0 ){ |
| 1619 | /* failed to open/create the file, someone else may have stolen the lock */ |
| 1620 | int tErrno = errno; |
| 1621 | if( EEXIST == tErrno ){ |
| 1622 | rc = SQLITE_BUSY; |
| 1623 | } else { |
| 1624 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1625 | if( IS_LOCK_ERROR(rc) ){ |
| 1626 | pFile->lastErrno = tErrno; |
| 1627 | } |
| 1628 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1629 | return rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1630 | } |
| 1631 | if( close(fd) ){ |
| 1632 | pFile->lastErrno = errno; |
| 1633 | rc = SQLITE_IOERR_CLOSE; |
| 1634 | } |
| 1635 | |
| 1636 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1637 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1638 | return rc; |
| 1639 | } |
| 1640 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1641 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1642 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1643 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1644 | ** |
| 1645 | ** If the locking level of the file descriptor is already at or below |
| 1646 | ** the requested locking level, this routine is a no-op. |
| 1647 | ** |
| 1648 | ** When the locking level reaches NO_LOCK, delete the lock file. |
| 1649 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1650 | static int dotlockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1651 | unixFile *pFile = (unixFile*)id; |
| 1652 | char *zLockFile = (char *)pFile->lockingContext; |
| 1653 | |
| 1654 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1655 | OSTRACE(("UNLOCK %d %d was %d pid=%d (dotlock)\n", pFile->h, eFileLock, |
| 1656 | pFile->eFileLock, getpid())); |
| 1657 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1658 | |
| 1659 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1660 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1661 | return SQLITE_OK; |
| 1662 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1663 | |
| 1664 | /* To downgrade to shared, simply update our internal notion of the |
| 1665 | ** lock state. No need to mess with the file on disk. |
| 1666 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1667 | if( eFileLock==SHARED_LOCK ){ |
| 1668 | pFile->eFileLock = SHARED_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1669 | return SQLITE_OK; |
| 1670 | } |
| 1671 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1672 | /* To fully unlock the database, delete the lock file */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1673 | assert( eFileLock==NO_LOCK ); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 1674 | if( unlink(zLockFile) ){ |
drh | 0d588bb | 2009-06-17 13:09:38 +0000 | [diff] [blame] | 1675 | int rc = 0; |
| 1676 | int tErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1677 | if( ENOENT != tErrno ){ |
| 1678 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1679 | } |
| 1680 | if( IS_LOCK_ERROR(rc) ){ |
| 1681 | pFile->lastErrno = tErrno; |
| 1682 | } |
| 1683 | return rc; |
| 1684 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1685 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1686 | return SQLITE_OK; |
| 1687 | } |
| 1688 | |
| 1689 | /* |
drh | 9b35ea6 | 2008-11-29 02:20:26 +0000 | [diff] [blame] | 1690 | ** Close a file. Make sure the lock has been released before closing. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1691 | */ |
| 1692 | static int dotlockClose(sqlite3_file *id) { |
| 1693 | int rc; |
| 1694 | if( id ){ |
| 1695 | unixFile *pFile = (unixFile*)id; |
| 1696 | dotlockUnlock(id, NO_LOCK); |
| 1697 | sqlite3_free(pFile->lockingContext); |
| 1698 | } |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1699 | rc = closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1700 | return rc; |
| 1701 | } |
| 1702 | /****************** End of the dot-file lock implementation ******************* |
| 1703 | ******************************************************************************/ |
| 1704 | |
| 1705 | /****************************************************************************** |
| 1706 | ************************** Begin flock Locking ******************************** |
| 1707 | ** |
| 1708 | ** Use the flock() system call to do file locking. |
| 1709 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1710 | ** flock() locking is like dot-file locking in that the various |
| 1711 | ** fine-grain locking levels supported by SQLite are collapsed into |
| 1712 | ** a single exclusive lock. In other words, SHARED, RESERVED, and |
| 1713 | ** PENDING locks are the same thing as an EXCLUSIVE lock. SQLite |
| 1714 | ** still works when you do this, but concurrency is reduced since |
| 1715 | ** only a single process can be reading the database at a time. |
| 1716 | ** |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1717 | ** Omit this section if SQLITE_ENABLE_LOCKING_STYLE is turned off or if |
| 1718 | ** compiling for VXWORKS. |
| 1719 | */ |
| 1720 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1721 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1722 | /* |
| 1723 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1724 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1725 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1726 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1727 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1728 | static int flockCheckReservedLock(sqlite3_file *id, int *pResOut){ |
| 1729 | int rc = SQLITE_OK; |
| 1730 | int reserved = 0; |
| 1731 | unixFile *pFile = (unixFile*)id; |
| 1732 | |
| 1733 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1734 | |
| 1735 | assert( pFile ); |
| 1736 | |
| 1737 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1738 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1739 | reserved = 1; |
| 1740 | } |
| 1741 | |
| 1742 | /* Otherwise see if some other process holds it. */ |
| 1743 | if( !reserved ){ |
| 1744 | /* attempt to get the lock */ |
| 1745 | int lrc = flock(pFile->h, LOCK_EX | LOCK_NB); |
| 1746 | if( !lrc ){ |
| 1747 | /* got the lock, unlock it */ |
| 1748 | lrc = flock(pFile->h, LOCK_UN); |
| 1749 | if ( lrc ) { |
| 1750 | int tErrno = errno; |
| 1751 | /* unlock failed with an error */ |
| 1752 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1753 | if( IS_LOCK_ERROR(lrc) ){ |
| 1754 | pFile->lastErrno = tErrno; |
| 1755 | rc = lrc; |
| 1756 | } |
| 1757 | } |
| 1758 | } else { |
| 1759 | int tErrno = errno; |
| 1760 | reserved = 1; |
| 1761 | /* someone else might have it reserved */ |
| 1762 | lrc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1763 | if( IS_LOCK_ERROR(lrc) ){ |
| 1764 | pFile->lastErrno = tErrno; |
| 1765 | rc = lrc; |
| 1766 | } |
| 1767 | } |
| 1768 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1769 | OSTRACE(("TEST WR-LOCK %d %d %d (flock)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1770 | |
| 1771 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1772 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1773 | rc = SQLITE_OK; |
| 1774 | reserved=1; |
| 1775 | } |
| 1776 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1777 | *pResOut = reserved; |
| 1778 | return rc; |
| 1779 | } |
| 1780 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1781 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1782 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1783 | ** of the following: |
| 1784 | ** |
| 1785 | ** (1) SHARED_LOCK |
| 1786 | ** (2) RESERVED_LOCK |
| 1787 | ** (3) PENDING_LOCK |
| 1788 | ** (4) EXCLUSIVE_LOCK |
| 1789 | ** |
| 1790 | ** Sometimes when requesting one lock state, additional lock states |
| 1791 | ** are inserted in between. The locking might fail on one of the later |
| 1792 | ** transitions leaving the lock state different from what it started but |
| 1793 | ** still short of its goal. The following chart shows the allowed |
| 1794 | ** transitions and the inserted intermediate states: |
| 1795 | ** |
| 1796 | ** UNLOCKED -> SHARED |
| 1797 | ** SHARED -> RESERVED |
| 1798 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1799 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1800 | ** PENDING -> EXCLUSIVE |
| 1801 | ** |
| 1802 | ** flock() only really support EXCLUSIVE locks. We track intermediate |
| 1803 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1804 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1805 | ** access the file. |
| 1806 | ** |
| 1807 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1808 | ** routine to lower a locking level. |
| 1809 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1810 | static int flockLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1811 | int rc = SQLITE_OK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1812 | unixFile *pFile = (unixFile*)id; |
| 1813 | |
| 1814 | assert( pFile ); |
| 1815 | |
| 1816 | /* if we already have a lock, it is exclusive. |
| 1817 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1818 | if (pFile->eFileLock > NO_LOCK) { |
| 1819 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1820 | return SQLITE_OK; |
| 1821 | } |
| 1822 | |
| 1823 | /* grab an exclusive lock */ |
| 1824 | |
| 1825 | if (flock(pFile->h, LOCK_EX | LOCK_NB)) { |
| 1826 | int tErrno = errno; |
| 1827 | /* didn't get, must be busy */ |
| 1828 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_LOCK); |
| 1829 | if( IS_LOCK_ERROR(rc) ){ |
| 1830 | pFile->lastErrno = tErrno; |
| 1831 | } |
| 1832 | } else { |
| 1833 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1834 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1835 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1836 | OSTRACE(("LOCK %d %s %s (flock)\n", pFile->h, azFileLock(eFileLock), |
| 1837 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1838 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1839 | if( (rc & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1840 | rc = SQLITE_BUSY; |
| 1841 | } |
| 1842 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1843 | return rc; |
| 1844 | } |
| 1845 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1846 | |
| 1847 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1848 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1849 | ** must be either NO_LOCK or SHARED_LOCK. |
| 1850 | ** |
| 1851 | ** If the locking level of the file descriptor is already at or below |
| 1852 | ** the requested locking level, this routine is a no-op. |
| 1853 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1854 | static int flockUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1855 | unixFile *pFile = (unixFile*)id; |
| 1856 | |
| 1857 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1858 | OSTRACE(("UNLOCK %d %d was %d pid=%d (flock)\n", pFile->h, eFileLock, |
| 1859 | pFile->eFileLock, getpid())); |
| 1860 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1861 | |
| 1862 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1863 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1864 | return SQLITE_OK; |
| 1865 | } |
| 1866 | |
| 1867 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1868 | if (eFileLock==SHARED_LOCK) { |
| 1869 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1870 | return SQLITE_OK; |
| 1871 | } |
| 1872 | |
| 1873 | /* no, really, unlock. */ |
| 1874 | int rc = flock(pFile->h, LOCK_UN); |
| 1875 | if (rc) { |
| 1876 | int r, tErrno = errno; |
| 1877 | r = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 1878 | if( IS_LOCK_ERROR(r) ){ |
| 1879 | pFile->lastErrno = tErrno; |
| 1880 | } |
| 1881 | #ifdef SQLITE_IGNORE_FLOCK_LOCK_ERRORS |
| 1882 | if( (r & SQLITE_IOERR) == SQLITE_IOERR ){ |
| 1883 | r = SQLITE_BUSY; |
| 1884 | } |
| 1885 | #endif /* SQLITE_IGNORE_FLOCK_LOCK_ERRORS */ |
| 1886 | |
| 1887 | return r; |
| 1888 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1889 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1890 | return SQLITE_OK; |
| 1891 | } |
| 1892 | } |
| 1893 | |
| 1894 | /* |
| 1895 | ** Close a file. |
| 1896 | */ |
| 1897 | static int flockClose(sqlite3_file *id) { |
| 1898 | if( id ){ |
| 1899 | flockUnlock(id, NO_LOCK); |
| 1900 | } |
| 1901 | return closeUnixFile(id); |
| 1902 | } |
| 1903 | |
| 1904 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORK */ |
| 1905 | |
| 1906 | /******************* End of the flock lock implementation ********************* |
| 1907 | ******************************************************************************/ |
| 1908 | |
| 1909 | /****************************************************************************** |
| 1910 | ************************ Begin Named Semaphore Locking ************************ |
| 1911 | ** |
| 1912 | ** Named semaphore locking is only supported on VxWorks. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1913 | ** |
| 1914 | ** Semaphore locking is like dot-lock and flock in that it really only |
| 1915 | ** supports EXCLUSIVE locking. Only a single process can read or write |
| 1916 | ** the database file at a time. This reduces potential concurrency, but |
| 1917 | ** makes the lock implementation much easier. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1918 | */ |
| 1919 | #if OS_VXWORKS |
| 1920 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1921 | /* |
| 1922 | ** This routine checks if there is a RESERVED lock held on the specified |
| 1923 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 1924 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 1925 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 1926 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1927 | static int semCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 1928 | int rc = SQLITE_OK; |
| 1929 | int reserved = 0; |
| 1930 | unixFile *pFile = (unixFile*)id; |
| 1931 | |
| 1932 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 1933 | |
| 1934 | assert( pFile ); |
| 1935 | |
| 1936 | /* Check if a thread in this process holds such a lock */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1937 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1938 | reserved = 1; |
| 1939 | } |
| 1940 | |
| 1941 | /* Otherwise see if some other process holds it. */ |
| 1942 | if( !reserved ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1943 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1944 | struct stat statBuf; |
| 1945 | |
| 1946 | if( sem_trywait(pSem)==-1 ){ |
| 1947 | int tErrno = errno; |
| 1948 | if( EAGAIN != tErrno ){ |
| 1949 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_CHECKRESERVEDLOCK); |
| 1950 | pFile->lastErrno = tErrno; |
| 1951 | } else { |
| 1952 | /* someone else has the lock when we are in NO_LOCK */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1953 | reserved = (pFile->eFileLock < SHARED_LOCK); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1954 | } |
| 1955 | }else{ |
| 1956 | /* we could have it if we want it */ |
| 1957 | sem_post(pSem); |
| 1958 | } |
| 1959 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1960 | OSTRACE(("TEST WR-LOCK %d %d %d (sem)\n", pFile->h, rc, reserved)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1961 | |
| 1962 | *pResOut = reserved; |
| 1963 | return rc; |
| 1964 | } |
| 1965 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1966 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1967 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 1968 | ** of the following: |
| 1969 | ** |
| 1970 | ** (1) SHARED_LOCK |
| 1971 | ** (2) RESERVED_LOCK |
| 1972 | ** (3) PENDING_LOCK |
| 1973 | ** (4) EXCLUSIVE_LOCK |
| 1974 | ** |
| 1975 | ** Sometimes when requesting one lock state, additional lock states |
| 1976 | ** are inserted in between. The locking might fail on one of the later |
| 1977 | ** transitions leaving the lock state different from what it started but |
| 1978 | ** still short of its goal. The following chart shows the allowed |
| 1979 | ** transitions and the inserted intermediate states: |
| 1980 | ** |
| 1981 | ** UNLOCKED -> SHARED |
| 1982 | ** SHARED -> RESERVED |
| 1983 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 1984 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 1985 | ** PENDING -> EXCLUSIVE |
| 1986 | ** |
| 1987 | ** Semaphore locks only really support EXCLUSIVE locks. We track intermediate |
| 1988 | ** lock states in the sqlite3_file structure, but all locks SHARED or |
| 1989 | ** above are really EXCLUSIVE locks and exclude all other processes from |
| 1990 | ** access the file. |
| 1991 | ** |
| 1992 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 1993 | ** routine to lower a locking level. |
| 1994 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 1995 | static int semLock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1996 | unixFile *pFile = (unixFile*)id; |
| 1997 | int fd; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 1998 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 1999 | int rc = SQLITE_OK; |
| 2000 | |
| 2001 | /* if we already have a lock, it is exclusive. |
| 2002 | ** Just adjust level and punt on outta here. */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2003 | if (pFile->eFileLock > NO_LOCK) { |
| 2004 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2005 | rc = SQLITE_OK; |
| 2006 | goto sem_end_lock; |
| 2007 | } |
| 2008 | |
| 2009 | /* lock semaphore now but bail out when already locked. */ |
| 2010 | if( sem_trywait(pSem)==-1 ){ |
| 2011 | rc = SQLITE_BUSY; |
| 2012 | goto sem_end_lock; |
| 2013 | } |
| 2014 | |
| 2015 | /* got it, set the type and return ok */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2016 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2017 | |
| 2018 | sem_end_lock: |
| 2019 | return rc; |
| 2020 | } |
| 2021 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2022 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2023 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2024 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2025 | ** |
| 2026 | ** If the locking level of the file descriptor is already at or below |
| 2027 | ** the requested locking level, this routine is a no-op. |
| 2028 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2029 | static int semUnlock(sqlite3_file *id, int eFileLock) { |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2030 | unixFile *pFile = (unixFile*)id; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2031 | sem_t *pSem = pFile->pInode->pSem; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2032 | |
| 2033 | assert( pFile ); |
| 2034 | assert( pSem ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2035 | OSTRACE(("UNLOCK %d %d was %d pid=%d (sem)\n", pFile->h, eFileLock, |
| 2036 | pFile->eFileLock, getpid())); |
| 2037 | assert( eFileLock<=SHARED_LOCK ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2038 | |
| 2039 | /* no-op if possible */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2040 | if( pFile->eFileLock==eFileLock ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2041 | return SQLITE_OK; |
| 2042 | } |
| 2043 | |
| 2044 | /* shared can just be set because we always have an exclusive */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2045 | if (eFileLock==SHARED_LOCK) { |
| 2046 | pFile->eFileLock = eFileLock; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2047 | return SQLITE_OK; |
| 2048 | } |
| 2049 | |
| 2050 | /* no, really unlock. */ |
| 2051 | if ( sem_post(pSem)==-1 ) { |
| 2052 | int rc, tErrno = errno; |
| 2053 | rc = sqliteErrorFromPosixError(tErrno, SQLITE_IOERR_UNLOCK); |
| 2054 | if( IS_LOCK_ERROR(rc) ){ |
| 2055 | pFile->lastErrno = tErrno; |
| 2056 | } |
| 2057 | return rc; |
| 2058 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2059 | pFile->eFileLock = NO_LOCK; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2060 | return SQLITE_OK; |
| 2061 | } |
| 2062 | |
| 2063 | /* |
| 2064 | ** Close a file. |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2065 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2066 | static int semClose(sqlite3_file *id) { |
| 2067 | if( id ){ |
| 2068 | unixFile *pFile = (unixFile*)id; |
| 2069 | semUnlock(id, NO_LOCK); |
| 2070 | assert( pFile ); |
| 2071 | unixEnterMutex(); |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2072 | releaseInodeInfo(pFile); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2073 | unixLeaveMutex(); |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 2074 | closeUnixFile(id); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2075 | } |
| 2076 | return SQLITE_OK; |
| 2077 | } |
| 2078 | |
| 2079 | #endif /* OS_VXWORKS */ |
| 2080 | /* |
| 2081 | ** Named semaphore locking is only available on VxWorks. |
| 2082 | ** |
| 2083 | *************** End of the named semaphore lock implementation **************** |
| 2084 | ******************************************************************************/ |
| 2085 | |
| 2086 | |
| 2087 | /****************************************************************************** |
| 2088 | *************************** Begin AFP Locking ********************************* |
| 2089 | ** |
| 2090 | ** AFP is the Apple Filing Protocol. AFP is a network filesystem found |
| 2091 | ** on Apple Macintosh computers - both OS9 and OSX. |
| 2092 | ** |
| 2093 | ** Third-party implementations of AFP are available. But this code here |
| 2094 | ** only works on OSX. |
| 2095 | */ |
| 2096 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2097 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2098 | /* |
| 2099 | ** The afpLockingContext structure contains all afp lock specific state |
| 2100 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2101 | typedef struct afpLockingContext afpLockingContext; |
| 2102 | struct afpLockingContext { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2103 | int reserved; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2104 | const char *dbPath; /* Name of the open file */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2105 | }; |
| 2106 | |
| 2107 | struct ByteRangeLockPB2 |
| 2108 | { |
| 2109 | unsigned long long offset; /* offset to first byte to lock */ |
| 2110 | unsigned long long length; /* nbr of bytes to lock */ |
| 2111 | unsigned long long retRangeStart; /* nbr of 1st byte locked if successful */ |
| 2112 | unsigned char unLockFlag; /* 1 = unlock, 0 = lock */ |
| 2113 | unsigned char startEndFlag; /* 1=rel to end of fork, 0=rel to start */ |
| 2114 | int fd; /* file desc to assoc this lock with */ |
| 2115 | }; |
| 2116 | |
drh | fd131da | 2007-08-07 17:13:03 +0000 | [diff] [blame] | 2117 | #define afpfsByteRangeLock2FSCTL _IOWR('z', 23, struct ByteRangeLockPB2) |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2118 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2119 | /* |
| 2120 | ** This is a utility for setting or clearing a bit-range lock on an |
| 2121 | ** AFP filesystem. |
| 2122 | ** |
| 2123 | ** Return SQLITE_OK on success, SQLITE_BUSY on failure. |
| 2124 | */ |
| 2125 | static int afpSetLock( |
| 2126 | const char *path, /* Name of the file to be locked or unlocked */ |
| 2127 | unixFile *pFile, /* Open file descriptor on path */ |
| 2128 | unsigned long long offset, /* First byte to be locked */ |
| 2129 | unsigned long long length, /* Number of bytes to lock */ |
| 2130 | int setLockFlag /* True to set lock. False to clear lock */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 2131 | ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2132 | struct ByteRangeLockPB2 pb; |
| 2133 | int err; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2134 | |
| 2135 | pb.unLockFlag = setLockFlag ? 0 : 1; |
| 2136 | pb.startEndFlag = 0; |
| 2137 | pb.offset = offset; |
| 2138 | pb.length = length; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2139 | pb.fd = pFile->h; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2140 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2141 | OSTRACE(("AFPSETLOCK [%s] for %d%s in range %llx:%llx\n", |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2142 | (setLockFlag?"ON":"OFF"), pFile->h, (pb.fd==-1?"[testval-1]":""), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2143 | offset, length)); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2144 | err = fsctl(path, afpfsByteRangeLock2FSCTL, &pb, 0); |
| 2145 | if ( err==-1 ) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2146 | int rc; |
| 2147 | int tErrno = errno; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2148 | OSTRACE(("AFPSETLOCK failed to fsctl() '%s' %d %s\n", |
| 2149 | path, tErrno, strerror(tErrno))); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2150 | #ifdef SQLITE_IGNORE_AFP_LOCK_ERRORS |
| 2151 | rc = SQLITE_BUSY; |
| 2152 | #else |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2153 | rc = sqliteErrorFromPosixError(tErrno, |
| 2154 | setLockFlag ? SQLITE_IOERR_LOCK : SQLITE_IOERR_UNLOCK); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2155 | #endif /* SQLITE_IGNORE_AFP_LOCK_ERRORS */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2156 | if( IS_LOCK_ERROR(rc) ){ |
| 2157 | pFile->lastErrno = tErrno; |
| 2158 | } |
| 2159 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2160 | } else { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2161 | return SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2162 | } |
| 2163 | } |
| 2164 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2165 | /* |
| 2166 | ** This routine checks if there is a RESERVED lock held on the specified |
| 2167 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 2168 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 2169 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 2170 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2171 | static int afpCheckReservedLock(sqlite3_file *id, int *pResOut){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2172 | int rc = SQLITE_OK; |
| 2173 | int reserved = 0; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2174 | unixFile *pFile = (unixFile*)id; |
| 2175 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2176 | SimulateIOError( return SQLITE_IOERR_CHECKRESERVEDLOCK; ); |
| 2177 | |
| 2178 | assert( pFile ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2179 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2180 | if( context->reserved ){ |
| 2181 | *pResOut = 1; |
| 2182 | return SQLITE_OK; |
| 2183 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2184 | unixEnterMutex(); /* Because pFile->pInode is shared across threads */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2185 | |
| 2186 | /* Check if a thread in this process holds such a lock */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2187 | if( pFile->pInode->eFileLock>SHARED_LOCK ){ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2188 | reserved = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | /* Otherwise see if some other process holds it. |
| 2192 | */ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2193 | if( !reserved ){ |
| 2194 | /* lock the RESERVED byte */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2195 | int lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2196 | if( SQLITE_OK==lrc ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2197 | /* if we succeeded in taking the reserved lock, unlock it to restore |
| 2198 | ** the original state */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2199 | lrc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2200 | } else { |
| 2201 | /* if we failed to get the lock then someone else must have it */ |
| 2202 | reserved = 1; |
| 2203 | } |
| 2204 | if( IS_LOCK_ERROR(lrc) ){ |
| 2205 | rc=lrc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2206 | } |
| 2207 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2208 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2209 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2210 | OSTRACE(("TEST WR-LOCK %d %d %d (afp)\n", pFile->h, rc, reserved)); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2211 | |
| 2212 | *pResOut = reserved; |
| 2213 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2216 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2217 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2218 | ** of the following: |
| 2219 | ** |
| 2220 | ** (1) SHARED_LOCK |
| 2221 | ** (2) RESERVED_LOCK |
| 2222 | ** (3) PENDING_LOCK |
| 2223 | ** (4) EXCLUSIVE_LOCK |
| 2224 | ** |
| 2225 | ** Sometimes when requesting one lock state, additional lock states |
| 2226 | ** are inserted in between. The locking might fail on one of the later |
| 2227 | ** transitions leaving the lock state different from what it started but |
| 2228 | ** still short of its goal. The following chart shows the allowed |
| 2229 | ** transitions and the inserted intermediate states: |
| 2230 | ** |
| 2231 | ** UNLOCKED -> SHARED |
| 2232 | ** SHARED -> RESERVED |
| 2233 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 2234 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 2235 | ** PENDING -> EXCLUSIVE |
| 2236 | ** |
| 2237 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 2238 | ** routine to lower a locking level. |
| 2239 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2240 | static int afpLock(sqlite3_file *id, int eFileLock){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2241 | int rc = SQLITE_OK; |
| 2242 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2243 | unixInodeInfo *pInode = pFile->pInode; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2244 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2245 | |
| 2246 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2247 | OSTRACE(("LOCK %d %s was %s(%s,%d) pid=%d (afp)\n", pFile->h, |
| 2248 | azFileLock(eFileLock), azFileLock(pFile->eFileLock), |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2249 | azFileLock(pInode->eFileLock), pInode->nShared , getpid())); |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2250 | |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2251 | /* If there is already a lock of this type or more restrictive on the |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2252 | ** unixFile, do nothing. Don't use the afp_end_lock: exit path, as |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2253 | ** unixEnterMutex() hasn't been called yet. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2254 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2255 | if( pFile->eFileLock>=eFileLock ){ |
| 2256 | OSTRACE(("LOCK %d %s ok (already held) (afp)\n", pFile->h, |
| 2257 | azFileLock(eFileLock))); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2258 | return SQLITE_OK; |
| 2259 | } |
| 2260 | |
| 2261 | /* Make sure the locking sequence is correct |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2262 | ** (1) We never move from unlocked to anything higher than shared lock. |
| 2263 | ** (2) SQLite never explicitly requests a pendig lock. |
| 2264 | ** (3) A shared lock is always held when a reserve lock is requested. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2265 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2266 | assert( pFile->eFileLock!=NO_LOCK || eFileLock==SHARED_LOCK ); |
| 2267 | assert( eFileLock!=PENDING_LOCK ); |
| 2268 | assert( eFileLock!=RESERVED_LOCK || pFile->eFileLock==SHARED_LOCK ); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2269 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2270 | /* This mutex is needed because pFile->pInode is shared across threads |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2271 | */ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2272 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2273 | pInode = pFile->pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2274 | |
| 2275 | /* If some thread using this PID has a lock via a different unixFile* |
| 2276 | ** handle that precludes the requested lock, return BUSY. |
| 2277 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2278 | if( (pFile->eFileLock!=pInode->eFileLock && |
| 2279 | (pInode->eFileLock>=PENDING_LOCK || eFileLock>SHARED_LOCK)) |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2280 | ){ |
| 2281 | rc = SQLITE_BUSY; |
| 2282 | goto afp_end_lock; |
| 2283 | } |
| 2284 | |
| 2285 | /* If a SHARED lock is requested, and some thread using this PID already |
| 2286 | ** has a SHARED or RESERVED lock, then increment reference counts and |
| 2287 | ** return SQLITE_OK. |
| 2288 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2289 | if( eFileLock==SHARED_LOCK && |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2290 | (pInode->eFileLock==SHARED_LOCK || pInode->eFileLock==RESERVED_LOCK) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2291 | assert( eFileLock==SHARED_LOCK ); |
| 2292 | assert( pFile->eFileLock==0 ); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2293 | assert( pInode->nShared>0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2294 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2295 | pInode->nShared++; |
| 2296 | pInode->nLock++; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2297 | goto afp_end_lock; |
| 2298 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2299 | |
| 2300 | /* A PENDING lock is needed before acquiring a SHARED lock and before |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2301 | ** acquiring an EXCLUSIVE lock. For the SHARED lock, the PENDING will |
| 2302 | ** be released. |
| 2303 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2304 | if( eFileLock==SHARED_LOCK |
| 2305 | || (eFileLock==EXCLUSIVE_LOCK && pFile->eFileLock<PENDING_LOCK) |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2306 | ){ |
| 2307 | int failed; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2308 | failed = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2309 | if (failed) { |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2310 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2311 | goto afp_end_lock; |
| 2312 | } |
| 2313 | } |
| 2314 | |
| 2315 | /* If control gets to this point, then actually go ahead and make |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2316 | ** operating system calls for the specified lock. |
| 2317 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2318 | if( eFileLock==SHARED_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2319 | int lrc1, lrc2, lrc1Errno; |
| 2320 | long lk, mask; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2321 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2322 | assert( pInode->nShared==0 ); |
| 2323 | assert( pInode->eFileLock==0 ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2324 | |
| 2325 | mask = (sizeof(long)==8) ? LARGEST_INT64 : 0x7fffffff; |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2326 | /* Now get the read-lock SHARED_LOCK */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2327 | /* note that the quality of the randomness doesn't matter that much */ |
| 2328 | lk = random(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2329 | pInode->sharedByte = (lk & mask)%(SHARED_SIZE - 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2330 | lrc1 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2331 | SHARED_FIRST+pInode->sharedByte, 1, 1); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2332 | if( IS_LOCK_ERROR(lrc1) ){ |
| 2333 | lrc1Errno = pFile->lastErrno; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2334 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2335 | /* Drop the temporary PENDING lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2336 | lrc2 = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2337 | |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2338 | if( IS_LOCK_ERROR(lrc1) ) { |
| 2339 | pFile->lastErrno = lrc1Errno; |
| 2340 | rc = lrc1; |
| 2341 | goto afp_end_lock; |
| 2342 | } else if( IS_LOCK_ERROR(lrc2) ){ |
| 2343 | rc = lrc2; |
| 2344 | goto afp_end_lock; |
| 2345 | } else if( lrc1 != SQLITE_OK ) { |
| 2346 | rc = lrc1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2347 | } else { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2348 | pFile->eFileLock = SHARED_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2349 | pInode->nLock++; |
| 2350 | pInode->nShared = 1; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2351 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2352 | }else if( eFileLock==EXCLUSIVE_LOCK && pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2353 | /* We are trying for an exclusive lock but another thread in this |
| 2354 | ** same process is still holding a shared lock. */ |
| 2355 | rc = SQLITE_BUSY; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2356 | }else{ |
| 2357 | /* The request was for a RESERVED or EXCLUSIVE lock. It is |
| 2358 | ** assumed that there is a SHARED or greater lock on the file |
| 2359 | ** already. |
| 2360 | */ |
| 2361 | int failed = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2362 | assert( 0!=pFile->eFileLock ); |
| 2363 | if (eFileLock >= RESERVED_LOCK && pFile->eFileLock < RESERVED_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2364 | /* Acquire a RESERVED lock */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2365 | failed = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1,1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2366 | if( !failed ){ |
| 2367 | context->reserved = 1; |
| 2368 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2369 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2370 | if (!failed && eFileLock == EXCLUSIVE_LOCK) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2371 | /* Acquire an EXCLUSIVE lock */ |
| 2372 | |
| 2373 | /* Remove the shared lock before trying the range. we'll need to |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2374 | ** reestablish the shared lock if we can't get the afpUnlock |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2375 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2376 | if( !(failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST + |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2377 | pInode->sharedByte, 1, 0)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2378 | int failed2 = SQLITE_OK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2379 | /* now attemmpt to get the exclusive lock range */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2380 | failed = afpSetLock(context->dbPath, pFile, SHARED_FIRST, |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2381 | SHARED_SIZE, 1); |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2382 | if( failed && (failed2 = afpSetLock(context->dbPath, pFile, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2383 | SHARED_FIRST + pInode->sharedByte, 1, 1)) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2384 | /* Can't reestablish the shared lock. Sqlite can't deal, this is |
| 2385 | ** a critical I/O error |
| 2386 | */ |
| 2387 | rc = ((failed & SQLITE_IOERR) == SQLITE_IOERR) ? failed2 : |
| 2388 | SQLITE_IOERR_LOCK; |
| 2389 | goto afp_end_lock; |
| 2390 | } |
| 2391 | }else{ |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2392 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2393 | } |
| 2394 | } |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2395 | if( failed ){ |
| 2396 | rc = failed; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | if( rc==SQLITE_OK ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2401 | pFile->eFileLock = eFileLock; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2402 | pInode->eFileLock = eFileLock; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2403 | }else if( eFileLock==EXCLUSIVE_LOCK ){ |
| 2404 | pFile->eFileLock = PENDING_LOCK; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2405 | pInode->eFileLock = PENDING_LOCK; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2406 | } |
| 2407 | |
| 2408 | afp_end_lock: |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2409 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2410 | OSTRACE(("LOCK %d %s %s (afp)\n", pFile->h, azFileLock(eFileLock), |
| 2411 | rc==SQLITE_OK ? "ok" : "failed")); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2412 | return rc; |
| 2413 | } |
| 2414 | |
| 2415 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2416 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2417 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2418 | ** |
| 2419 | ** If the locking level of the file descriptor is already at or below |
| 2420 | ** the requested locking level, this routine is a no-op. |
| 2421 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2422 | static int afpUnlock(sqlite3_file *id, int eFileLock) { |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2423 | int rc = SQLITE_OK; |
| 2424 | unixFile *pFile = (unixFile*)id; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 2425 | unixInodeInfo *pInode; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2426 | afpLockingContext *context = (afpLockingContext *) pFile->lockingContext; |
| 2427 | int skipShared = 0; |
| 2428 | #ifdef SQLITE_TEST |
| 2429 | int h = pFile->h; |
| 2430 | #endif |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2431 | |
| 2432 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2433 | OSTRACE(("UNLOCK %d %d was %d(%d,%d) pid=%d (afp)\n", pFile->h, eFileLock, |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2434 | pFile->eFileLock, pFile->pInode->eFileLock, pFile->pInode->nShared, |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2435 | getpid())); |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 2436 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2437 | assert( eFileLock<=SHARED_LOCK ); |
| 2438 | if( pFile->eFileLock<=eFileLock ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2439 | return SQLITE_OK; |
| 2440 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2441 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2442 | pInode = pFile->pInode; |
| 2443 | assert( pInode->nShared!=0 ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2444 | if( pFile->eFileLock>SHARED_LOCK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2445 | assert( pInode->eFileLock==pFile->eFileLock ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2446 | SimulateIOErrorBenign(1); |
| 2447 | SimulateIOError( h=(-1) ) |
| 2448 | SimulateIOErrorBenign(0); |
| 2449 | |
| 2450 | #ifndef NDEBUG |
| 2451 | /* When reducing a lock such that other processes can start |
| 2452 | ** reading the database file again, make sure that the |
| 2453 | ** transaction counter was updated if any part of the database |
| 2454 | ** file changed. If the transaction counter is not updated, |
| 2455 | ** other connections to the same file might not realize that |
| 2456 | ** the file has changed and hence might not know to flush their |
| 2457 | ** cache. The use of a stale cache can lead to database corruption. |
| 2458 | */ |
| 2459 | assert( pFile->inNormalWrite==0 |
| 2460 | || pFile->dbUpdate==0 |
| 2461 | || pFile->transCntrChng==1 ); |
| 2462 | pFile->inNormalWrite = 0; |
| 2463 | #endif |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2464 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2465 | if( pFile->eFileLock==EXCLUSIVE_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2466 | rc = afpSetLock(context->dbPath, pFile, SHARED_FIRST, SHARED_SIZE, 0); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2467 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1) ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2468 | /* only re-establish the shared lock if necessary */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2469 | int sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2470 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 1); |
| 2471 | } else { |
| 2472 | skipShared = 1; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2473 | } |
| 2474 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2475 | if( rc==SQLITE_OK && pFile->eFileLock>=PENDING_LOCK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2476 | rc = afpSetLock(context->dbPath, pFile, PENDING_BYTE, 1, 0); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2477 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2478 | if( rc==SQLITE_OK && pFile->eFileLock>=RESERVED_LOCK && context->reserved ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2479 | rc = afpSetLock(context->dbPath, pFile, RESERVED_BYTE, 1, 0); |
| 2480 | if( !rc ){ |
| 2481 | context->reserved = 0; |
| 2482 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2483 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2484 | if( rc==SQLITE_OK && (eFileLock==SHARED_LOCK || pInode->nShared>1)){ |
| 2485 | pInode->eFileLock = SHARED_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2486 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2487 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2488 | if( rc==SQLITE_OK && eFileLock==NO_LOCK ){ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2489 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2490 | /* Decrement the shared lock counter. Release the lock using an |
| 2491 | ** OS call only when all threads in this same process have released |
| 2492 | ** the lock. |
| 2493 | */ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2494 | unsigned long long sharedLockByte = SHARED_FIRST+pInode->sharedByte; |
| 2495 | pInode->nShared--; |
| 2496 | if( pInode->nShared==0 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2497 | SimulateIOErrorBenign(1); |
| 2498 | SimulateIOError( h=(-1) ) |
| 2499 | SimulateIOErrorBenign(0); |
| 2500 | if( !skipShared ){ |
| 2501 | rc = afpSetLock(context->dbPath, pFile, sharedLockByte, 1, 0); |
| 2502 | } |
| 2503 | if( !rc ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2504 | pInode->eFileLock = NO_LOCK; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2505 | pFile->eFileLock = NO_LOCK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2506 | } |
| 2507 | } |
| 2508 | if( rc==SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2509 | pInode->nLock--; |
| 2510 | assert( pInode->nLock>=0 ); |
| 2511 | if( pInode->nLock==0 ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2512 | rc = closePendingFds(pFile); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2513 | } |
| 2514 | } |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2515 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2516 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2517 | unixLeaveMutex(); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2518 | if( rc==SQLITE_OK ) pFile->eFileLock = eFileLock; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2519 | return rc; |
| 2520 | } |
| 2521 | |
| 2522 | /* |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2523 | ** Close a file & cleanup AFP specific locking context |
| 2524 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2525 | static int afpClose(sqlite3_file *id) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2526 | int rc = SQLITE_OK; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2527 | if( id ){ |
| 2528 | unixFile *pFile = (unixFile*)id; |
| 2529 | afpUnlock(id, NO_LOCK); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2530 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2531 | if( pFile->pInode && pFile->pInode->nLock ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2532 | /* If there are outstanding locks, do not actually close the file just |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2533 | ** yet because that would clear those locks. Instead, add the file |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 2534 | ** descriptor to pInode->aPending. It will be automatically closed when |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2535 | ** the last lock is cleared. |
| 2536 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2537 | setPendingFd(pFile); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 2538 | } |
dan | b0ac3e3 | 2010-06-16 10:55:42 +0000 | [diff] [blame] | 2539 | releaseInodeInfo(pFile); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2540 | sqlite3_free(pFile->lockingContext); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2541 | rc = closeUnixFile(id); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 2542 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 2543 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2544 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 2547 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2548 | /* |
| 2549 | ** The code above is the AFP lock implementation. The code is specific |
| 2550 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2551 | ** is available. If you don't compile for a mac, then the "unix-afp" |
| 2552 | ** VFS is not available. |
| 2553 | ** |
| 2554 | ********************* End of the AFP lock implementation ********************** |
| 2555 | ******************************************************************************/ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2556 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2557 | /****************************************************************************** |
| 2558 | *************************** Begin NFS Locking ********************************/ |
| 2559 | |
| 2560 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 2561 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2562 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2563 | ** must be either NO_LOCK or SHARED_LOCK. |
| 2564 | ** |
| 2565 | ** If the locking level of the file descriptor is already at or below |
| 2566 | ** the requested locking level, this routine is a no-op. |
| 2567 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2568 | static int nfsUnlock(sqlite3_file *id, int eFileLock){ |
| 2569 | return _posixUnlock(id, eFileLock, 1); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2570 | } |
| 2571 | |
| 2572 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
| 2573 | /* |
| 2574 | ** The code above is the NFS lock implementation. The code is specific |
| 2575 | ** to MacOSX and does not work on other unix platforms. No alternative |
| 2576 | ** is available. |
| 2577 | ** |
| 2578 | ********************* End of the NFS lock implementation ********************** |
| 2579 | ******************************************************************************/ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2580 | |
| 2581 | /****************************************************************************** |
| 2582 | **************** Non-locking sqlite3_file methods ***************************** |
| 2583 | ** |
| 2584 | ** The next division contains implementations for all methods of the |
| 2585 | ** sqlite3_file object other than the locking methods. The locking |
| 2586 | ** methods were defined in divisions above (one locking method per |
| 2587 | ** division). Those methods that are common to all locking modes |
| 2588 | ** are gather together into this division. |
| 2589 | */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2590 | |
| 2591 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2592 | ** Seek to the offset passed as the second argument, then read cnt |
| 2593 | ** bytes into pBuf. Return the number of bytes actually read. |
| 2594 | ** |
| 2595 | ** NB: If you define USE_PREAD or USE_PREAD64, then it might also |
| 2596 | ** be necessary to define _XOPEN_SOURCE to be 500. This varies from |
| 2597 | ** one system to another. Since SQLite does not define USE_PREAD |
| 2598 | ** any any form by default, we will not attempt to define _XOPEN_SOURCE. |
| 2599 | ** See tickets #2741 and #2681. |
| 2600 | ** |
| 2601 | ** To avoid stomping the errno value on a failed read the lastErrno value |
| 2602 | ** is set before returning. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2603 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2604 | static int seekAndRead(unixFile *id, sqlite3_int64 offset, void *pBuf, int cnt){ |
| 2605 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2606 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2607 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2608 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2609 | TIMER_START; |
| 2610 | #if defined(USE_PREAD) |
| 2611 | got = pread(id->h, pBuf, cnt, offset); |
| 2612 | SimulateIOError( got = -1 ); |
| 2613 | #elif defined(USE_PREAD64) |
| 2614 | got = pread64(id->h, pBuf, cnt, offset); |
| 2615 | SimulateIOError( got = -1 ); |
| 2616 | #else |
| 2617 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2618 | SimulateIOError( newOffset-- ); |
| 2619 | if( newOffset!=offset ){ |
| 2620 | if( newOffset == -1 ){ |
| 2621 | ((unixFile*)id)->lastErrno = errno; |
| 2622 | }else{ |
| 2623 | ((unixFile*)id)->lastErrno = 0; |
| 2624 | } |
| 2625 | return -1; |
| 2626 | } |
| 2627 | got = read(id->h, pBuf, cnt); |
| 2628 | #endif |
| 2629 | TIMER_END; |
| 2630 | if( got<0 ){ |
| 2631 | ((unixFile*)id)->lastErrno = errno; |
| 2632 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2633 | OSTRACE(("READ %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2634 | return got; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2635 | } |
| 2636 | |
| 2637 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2638 | ** Read data from a file into a buffer. Return SQLITE_OK if all |
| 2639 | ** bytes were read successfully and SQLITE_IOERR if anything goes |
| 2640 | ** wrong. |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 2641 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2642 | static int unixRead( |
| 2643 | sqlite3_file *id, |
| 2644 | void *pBuf, |
| 2645 | int amt, |
| 2646 | sqlite3_int64 offset |
| 2647 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2648 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2649 | int got; |
| 2650 | assert( id ); |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2651 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2652 | /* If this is a database file (not a journal, master-journal or temp |
| 2653 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2654 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2655 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2656 | || offset>=PENDING_BYTE+512 |
| 2657 | || offset+amt<=PENDING_BYTE |
| 2658 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2659 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2660 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2661 | got = seekAndRead(pFile, offset, pBuf, amt); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2662 | if( got==amt ){ |
| 2663 | return SQLITE_OK; |
| 2664 | }else if( got<0 ){ |
| 2665 | /* lastErrno set by seekAndRead */ |
| 2666 | return SQLITE_IOERR_READ; |
| 2667 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2668 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2669 | /* Unread parts of the buffer must be zero-filled */ |
| 2670 | memset(&((char*)pBuf)[got], 0, amt-got); |
| 2671 | return SQLITE_IOERR_SHORT_READ; |
| 2672 | } |
| 2673 | } |
| 2674 | |
| 2675 | /* |
| 2676 | ** Seek to the offset in id->offset then read cnt bytes into pBuf. |
| 2677 | ** Return the number of bytes actually read. Update the offset. |
| 2678 | ** |
| 2679 | ** To avoid stomping the errno value on a failed write the lastErrno value |
| 2680 | ** is set before returning. |
| 2681 | */ |
| 2682 | static int seekAndWrite(unixFile *id, i64 offset, const void *pBuf, int cnt){ |
| 2683 | int got; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2684 | #if (!defined(USE_PREAD) && !defined(USE_PREAD64)) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2685 | i64 newOffset; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2686 | #endif |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2687 | TIMER_START; |
| 2688 | #if defined(USE_PREAD) |
| 2689 | got = pwrite(id->h, pBuf, cnt, offset); |
| 2690 | #elif defined(USE_PREAD64) |
| 2691 | got = pwrite64(id->h, pBuf, cnt, offset); |
| 2692 | #else |
| 2693 | newOffset = lseek(id->h, offset, SEEK_SET); |
| 2694 | if( newOffset!=offset ){ |
| 2695 | if( newOffset == -1 ){ |
| 2696 | ((unixFile*)id)->lastErrno = errno; |
| 2697 | }else{ |
| 2698 | ((unixFile*)id)->lastErrno = 0; |
| 2699 | } |
| 2700 | return -1; |
| 2701 | } |
| 2702 | got = write(id->h, pBuf, cnt); |
| 2703 | #endif |
| 2704 | TIMER_END; |
| 2705 | if( got<0 ){ |
| 2706 | ((unixFile*)id)->lastErrno = errno; |
| 2707 | } |
| 2708 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2709 | OSTRACE(("WRITE %-3d %5d %7lld %llu\n", id->h, got, offset, TIMER_ELAPSED)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2710 | return got; |
| 2711 | } |
| 2712 | |
| 2713 | |
| 2714 | /* |
| 2715 | ** Write data from a buffer into a file. Return SQLITE_OK on success |
| 2716 | ** or some other error code on failure. |
| 2717 | */ |
| 2718 | static int unixWrite( |
| 2719 | sqlite3_file *id, |
| 2720 | const void *pBuf, |
| 2721 | int amt, |
| 2722 | sqlite3_int64 offset |
| 2723 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2724 | unixFile *pFile = (unixFile*)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2725 | int wrote = 0; |
| 2726 | assert( id ); |
| 2727 | assert( amt>0 ); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2728 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2729 | /* If this is a database file (not a journal, master-journal or temp |
| 2730 | ** file), the bytes in the locking range should never be read or written. */ |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2731 | #if 0 |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 2732 | assert( pFile->pUnused==0 |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2733 | || offset>=PENDING_BYTE+512 |
| 2734 | || offset+amt<=PENDING_BYTE |
| 2735 | ); |
dan | 7c24610 | 2010-04-12 19:00:29 +0000 | [diff] [blame] | 2736 | #endif |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 2737 | |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2738 | #ifndef NDEBUG |
| 2739 | /* If we are doing a normal write to a database file (as opposed to |
| 2740 | ** doing a hot-journal rollback or a write to some file other than a |
| 2741 | ** normal database file) then record the fact that the database |
| 2742 | ** has changed. If the transaction counter is modified, record that |
| 2743 | ** fact too. |
| 2744 | */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2745 | if( pFile->inNormalWrite ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2746 | pFile->dbUpdate = 1; /* The database has been modified */ |
| 2747 | if( offset<=24 && offset+amt>=27 ){ |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2748 | int rc; |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2749 | char oldCntr[4]; |
| 2750 | SimulateIOErrorBenign(1); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2751 | rc = seekAndRead(pFile, 24, oldCntr, 4); |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2752 | SimulateIOErrorBenign(0); |
drh | a6d90f0 | 2009-01-16 23:47:42 +0000 | [diff] [blame] | 2753 | if( rc!=4 || memcmp(oldCntr, &((char*)pBuf)[24-offset], 4)!=0 ){ |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 2754 | pFile->transCntrChng = 1; /* The transaction counter has changed */ |
| 2755 | } |
| 2756 | } |
| 2757 | } |
| 2758 | #endif |
| 2759 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2760 | while( amt>0 && (wrote = seekAndWrite(pFile, offset, pBuf, amt))>0 ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2761 | amt -= wrote; |
| 2762 | offset += wrote; |
| 2763 | pBuf = &((char*)pBuf)[wrote]; |
| 2764 | } |
| 2765 | SimulateIOError(( wrote=(-1), amt=1 )); |
| 2766 | SimulateDiskfullError(( wrote=0, amt=1 )); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2767 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2768 | if( amt>0 ){ |
| 2769 | if( wrote<0 ){ |
| 2770 | /* lastErrno set by seekAndWrite */ |
| 2771 | return SQLITE_IOERR_WRITE; |
| 2772 | }else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 2773 | pFile->lastErrno = 0; /* not a system error */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2774 | return SQLITE_FULL; |
| 2775 | } |
| 2776 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2777 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2778 | return SQLITE_OK; |
| 2779 | } |
| 2780 | |
| 2781 | #ifdef SQLITE_TEST |
| 2782 | /* |
| 2783 | ** Count the number of fullsyncs and normal syncs. This is used to test |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2784 | ** that syncs and fullsyncs are occurring at the right times. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2785 | */ |
| 2786 | int sqlite3_sync_count = 0; |
| 2787 | int sqlite3_fullsync_count = 0; |
| 2788 | #endif |
| 2789 | |
| 2790 | /* |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2791 | ** We do not trust systems to provide a working fdatasync(). Some do. |
| 2792 | ** Others do no. To be safe, we will stick with the (slower) fsync(). |
| 2793 | ** If you know that your system does support fdatasync() correctly, |
| 2794 | ** then simply compile with -Dfdatasync=fdatasync |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2795 | */ |
drh | 8924043 | 2009-03-25 01:06:01 +0000 | [diff] [blame] | 2796 | #if !defined(fdatasync) && !defined(__linux__) |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2797 | # define fdatasync fsync |
| 2798 | #endif |
| 2799 | |
| 2800 | /* |
| 2801 | ** Define HAVE_FULLFSYNC to 0 or 1 depending on whether or not |
| 2802 | ** the F_FULLFSYNC macro is defined. F_FULLFSYNC is currently |
| 2803 | ** only available on Mac OS X. But that could change. |
| 2804 | */ |
| 2805 | #ifdef F_FULLFSYNC |
| 2806 | # define HAVE_FULLFSYNC 1 |
| 2807 | #else |
| 2808 | # define HAVE_FULLFSYNC 0 |
| 2809 | #endif |
| 2810 | |
| 2811 | |
| 2812 | /* |
| 2813 | ** The fsync() system call does not work as advertised on many |
| 2814 | ** unix systems. The following procedure is an attempt to make |
| 2815 | ** it work better. |
| 2816 | ** |
| 2817 | ** The SQLITE_NO_SYNC macro disables all fsync()s. This is useful |
| 2818 | ** for testing when we want to run through the test suite quickly. |
| 2819 | ** You are strongly advised *not* to deploy with SQLITE_NO_SYNC |
| 2820 | ** enabled, however, since with SQLITE_NO_SYNC enabled, an OS crash |
| 2821 | ** or power failure will likely corrupt the database file. |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2822 | ** |
| 2823 | ** SQLite sets the dataOnly flag if the size of the file is unchanged. |
| 2824 | ** The idea behind dataOnly is that it should only write the file content |
| 2825 | ** to disk, not the inode. We only set dataOnly if the file size is |
| 2826 | ** unchanged since the file size is part of the inode. However, |
| 2827 | ** Ted Ts'o tells us that fdatasync() will also write the inode if the |
| 2828 | ** file size has changed. The only real difference between fdatasync() |
| 2829 | ** and fsync(), Ted tells us, is that fdatasync() will not flush the |
| 2830 | ** inode if the mtime or owner or other inode attributes have changed. |
| 2831 | ** We only care about the file size, not the other file attributes, so |
| 2832 | ** as far as SQLite is concerned, an fdatasync() is always adequate. |
| 2833 | ** So, we always use fdatasync() if it is available, regardless of |
| 2834 | ** the value of the dataOnly flag. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2835 | */ |
| 2836 | static int full_fsync(int fd, int fullSync, int dataOnly){ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2837 | int rc; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2838 | |
| 2839 | /* The following "ifdef/elif/else/" block has the same structure as |
| 2840 | ** the one below. It is replicated here solely to avoid cluttering |
| 2841 | ** up the real code with the UNUSED_PARAMETER() macros. |
| 2842 | */ |
| 2843 | #ifdef SQLITE_NO_SYNC |
| 2844 | UNUSED_PARAMETER(fd); |
| 2845 | UNUSED_PARAMETER(fullSync); |
| 2846 | UNUSED_PARAMETER(dataOnly); |
| 2847 | #elif HAVE_FULLFSYNC |
| 2848 | UNUSED_PARAMETER(dataOnly); |
| 2849 | #else |
| 2850 | UNUSED_PARAMETER(fullSync); |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2851 | UNUSED_PARAMETER(dataOnly); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2852 | #endif |
| 2853 | |
| 2854 | /* Record the number of times that we do a normal fsync() and |
| 2855 | ** FULLSYNC. This is used during testing to verify that this procedure |
| 2856 | ** gets called with the correct arguments. |
| 2857 | */ |
| 2858 | #ifdef SQLITE_TEST |
| 2859 | if( fullSync ) sqlite3_fullsync_count++; |
| 2860 | sqlite3_sync_count++; |
| 2861 | #endif |
| 2862 | |
| 2863 | /* If we compiled with the SQLITE_NO_SYNC flag, then syncing is a |
| 2864 | ** no-op |
| 2865 | */ |
| 2866 | #ifdef SQLITE_NO_SYNC |
| 2867 | rc = SQLITE_OK; |
| 2868 | #elif HAVE_FULLFSYNC |
| 2869 | if( fullSync ){ |
| 2870 | rc = fcntl(fd, F_FULLFSYNC, 0); |
| 2871 | }else{ |
| 2872 | rc = 1; |
| 2873 | } |
| 2874 | /* If the FULLFSYNC failed, fall back to attempting an fsync(). |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 2875 | ** It shouldn't be possible for fullfsync to fail on the local |
| 2876 | ** file system (on OSX), so failure indicates that FULLFSYNC |
| 2877 | ** isn't supported for this file system. So, attempt an fsync |
| 2878 | ** and (for now) ignore the overhead of a superfluous fcntl call. |
| 2879 | ** It'd be better to detect fullfsync support once and avoid |
| 2880 | ** the fcntl call every time sync is called. |
| 2881 | */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2882 | if( rc ) rc = fsync(fd); |
| 2883 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 2884 | #elif defined(__APPLE__) |
| 2885 | /* fdatasync() on HFS+ doesn't yet flush the file size if it changed correctly |
| 2886 | ** so currently we default to the macro that redefines fdatasync to fsync |
| 2887 | */ |
| 2888 | rc = fsync(fd); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2889 | #else |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2890 | rc = fdatasync(fd); |
drh | c7288ee | 2009-01-15 04:30:02 +0000 | [diff] [blame] | 2891 | #if OS_VXWORKS |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2892 | if( rc==-1 && errno==ENOTSUP ){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2893 | rc = fsync(fd); |
| 2894 | } |
drh | 0b647ff | 2009-03-21 14:41:04 +0000 | [diff] [blame] | 2895 | #endif /* OS_VXWORKS */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2896 | #endif /* ifdef SQLITE_NO_SYNC elif HAVE_FULLFSYNC */ |
| 2897 | |
| 2898 | if( OS_VXWORKS && rc!= -1 ){ |
| 2899 | rc = 0; |
| 2900 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 2901 | return rc; |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 2902 | } |
| 2903 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2904 | /* |
| 2905 | ** Make sure all writes to a particular file are committed to disk. |
| 2906 | ** |
| 2907 | ** If dataOnly==0 then both the file itself and its metadata (file |
| 2908 | ** size, access time, etc) are synced. If dataOnly!=0 then only the |
| 2909 | ** file data is synced. |
| 2910 | ** |
| 2911 | ** Under Unix, also make sure that the directory entry for the file |
| 2912 | ** has been created by fsync-ing the directory that contains the file. |
| 2913 | ** If we do not do this and we encounter a power failure, the directory |
| 2914 | ** entry for the journal might not exist after we reboot. The next |
| 2915 | ** SQLite to access the file will not know that the journal exists (because |
| 2916 | ** the directory entry for the journal was never created) and the transaction |
| 2917 | ** will not roll back - possibly leading to database corruption. |
| 2918 | */ |
| 2919 | static int unixSync(sqlite3_file *id, int flags){ |
| 2920 | int rc; |
| 2921 | unixFile *pFile = (unixFile*)id; |
| 2922 | |
| 2923 | int isDataOnly = (flags&SQLITE_SYNC_DATAONLY); |
| 2924 | int isFullsync = (flags&0x0F)==SQLITE_SYNC_FULL; |
| 2925 | |
| 2926 | /* Check that one of SQLITE_SYNC_NORMAL or FULL was passed */ |
| 2927 | assert((flags&0x0F)==SQLITE_SYNC_NORMAL |
| 2928 | || (flags&0x0F)==SQLITE_SYNC_FULL |
| 2929 | ); |
| 2930 | |
| 2931 | /* Unix cannot, but some systems may return SQLITE_FULL from here. This |
| 2932 | ** line is to test that doing so does not cause any problems. |
| 2933 | */ |
| 2934 | SimulateDiskfullError( return SQLITE_FULL ); |
| 2935 | |
| 2936 | assert( pFile ); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2937 | OSTRACE(("SYNC %-3d\n", pFile->h)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2938 | rc = full_fsync(pFile->h, isFullsync, isDataOnly); |
| 2939 | SimulateIOError( rc=1 ); |
| 2940 | if( rc ){ |
| 2941 | pFile->lastErrno = errno; |
| 2942 | return SQLITE_IOERR_FSYNC; |
| 2943 | } |
| 2944 | if( pFile->dirfd>=0 ){ |
| 2945 | int err; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 2946 | OSTRACE(("DIRSYNC %-3d (have_fullfsync=%d fullsync=%d)\n", pFile->dirfd, |
| 2947 | HAVE_FULLFSYNC, isFullsync)); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2948 | #ifndef SQLITE_DISABLE_DIRSYNC |
| 2949 | /* The directory sync is only attempted if full_fsync is |
| 2950 | ** turned off or unavailable. If a full_fsync occurred above, |
| 2951 | ** then the directory sync is superfluous. |
| 2952 | */ |
| 2953 | if( (!HAVE_FULLFSYNC || !isFullsync) && full_fsync(pFile->dirfd,0,0) ){ |
| 2954 | /* |
| 2955 | ** We have received multiple reports of fsync() returning |
| 2956 | ** errors when applied to directories on certain file systems. |
| 2957 | ** A failed directory sync is not a big deal. So it seems |
| 2958 | ** better to ignore the error. Ticket #1657 |
| 2959 | */ |
| 2960 | /* pFile->lastErrno = errno; */ |
| 2961 | /* return SQLITE_IOERR; */ |
| 2962 | } |
| 2963 | #endif |
| 2964 | err = close(pFile->dirfd); /* Only need to sync once, so close the */ |
| 2965 | if( err==0 ){ /* directory when we are done */ |
| 2966 | pFile->dirfd = -1; |
| 2967 | }else{ |
| 2968 | pFile->lastErrno = errno; |
| 2969 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 2970 | } |
| 2971 | } |
| 2972 | return rc; |
| 2973 | } |
| 2974 | |
| 2975 | /* |
| 2976 | ** Truncate an open file to a specified size |
| 2977 | */ |
| 2978 | static int unixTruncate(sqlite3_file *id, i64 nByte){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2979 | unixFile *pFile = (unixFile *)id; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2980 | int rc; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2981 | assert( pFile ); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2982 | SimulateIOError( return SQLITE_IOERR_TRUNCATE ); |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2983 | |
| 2984 | /* If the user has configured a chunk-size for this file, truncate the |
| 2985 | ** file so that it consists of an integer number of chunks (i.e. the |
| 2986 | ** actual file size after the operation may be larger than the requested |
| 2987 | ** size). |
| 2988 | */ |
| 2989 | if( pFile->szChunk ){ |
| 2990 | nByte = ((nByte + pFile->szChunk - 1)/pFile->szChunk) * pFile->szChunk; |
| 2991 | } |
| 2992 | |
| 2993 | rc = ftruncate(pFile->h, (off_t)nByte); |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2994 | if( rc ){ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 2995 | pFile->lastErrno = errno; |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 2996 | return SQLITE_IOERR_TRUNCATE; |
| 2997 | }else{ |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 2998 | #ifndef NDEBUG |
| 2999 | /* If we are doing a normal write to a database file (as opposed to |
| 3000 | ** doing a hot-journal rollback or a write to some file other than a |
| 3001 | ** normal database file) and we truncate the file to zero length, |
| 3002 | ** that effectively updates the change counter. This might happen |
| 3003 | ** when restoring a database using the backup API from a zero-length |
| 3004 | ** source. |
| 3005 | */ |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3006 | if( pFile->inNormalWrite && nByte==0 ){ |
| 3007 | pFile->transCntrChng = 1; |
drh | 3313b14 | 2009-11-06 04:13:18 +0000 | [diff] [blame] | 3008 | } |
| 3009 | #endif |
| 3010 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3011 | return SQLITE_OK; |
| 3012 | } |
| 3013 | } |
| 3014 | |
| 3015 | /* |
| 3016 | ** Determine the current size of a file in bytes |
| 3017 | */ |
| 3018 | static int unixFileSize(sqlite3_file *id, i64 *pSize){ |
| 3019 | int rc; |
| 3020 | struct stat buf; |
| 3021 | assert( id ); |
| 3022 | rc = fstat(((unixFile*)id)->h, &buf); |
| 3023 | SimulateIOError( rc=1 ); |
| 3024 | if( rc!=0 ){ |
| 3025 | ((unixFile*)id)->lastErrno = errno; |
| 3026 | return SQLITE_IOERR_FSTAT; |
| 3027 | } |
| 3028 | *pSize = buf.st_size; |
| 3029 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 3030 | /* When opening a zero-size database, the findInodeInfo() procedure |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3031 | ** writes a single byte into that file in order to work around a bug |
| 3032 | ** in the OS-X msdos filesystem. In order to avoid problems with upper |
| 3033 | ** layers, we need to report this file size as zero even though it is |
| 3034 | ** really 1. Ticket #3260. |
| 3035 | */ |
| 3036 | if( *pSize==1 ) *pSize = 0; |
| 3037 | |
| 3038 | |
| 3039 | return SQLITE_OK; |
| 3040 | } |
| 3041 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3042 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3043 | /* |
| 3044 | ** Handler for proxy-locking file-control verbs. Defined below in the |
| 3045 | ** proxying locking division. |
| 3046 | */ |
| 3047 | static int proxyFileControl(sqlite3_file*,int,void*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 3048 | #endif |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3049 | |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3050 | /* |
| 3051 | ** This function is called to handle the SQLITE_FCNTL_SIZE_HINT |
| 3052 | ** file-control operation. |
| 3053 | ** |
| 3054 | ** If the user has configured a chunk-size for this file, it could be |
| 3055 | ** that the file needs to be extended at this point. Otherwise, the |
| 3056 | ** SQLITE_FCNTL_SIZE_HINT operation is a no-op for Unix. |
| 3057 | */ |
| 3058 | static int fcntlSizeHint(unixFile *pFile, i64 nByte){ |
| 3059 | if( pFile->szChunk ){ |
| 3060 | i64 nSize; /* Required file size */ |
| 3061 | struct stat buf; /* Used to hold return values of fstat() */ |
| 3062 | |
| 3063 | if( fstat(pFile->h, &buf) ) return SQLITE_IOERR_FSTAT; |
| 3064 | |
| 3065 | nSize = ((nByte+pFile->szChunk-1) / pFile->szChunk) * pFile->szChunk; |
| 3066 | if( nSize>(i64)buf.st_size ){ |
| 3067 | #if defined(HAVE_POSIX_FALLOCATE) && HAVE_POSIX_FALLOCATE |
| 3068 | if( posix_fallocate(pFile->h, buf.st_size, nSize-buf.st_size) ){ |
| 3069 | return SQLITE_IOERR_WRITE; |
| 3070 | } |
| 3071 | #else |
| 3072 | /* If the OS does not have posix_fallocate(), fake it. First use |
| 3073 | ** ftruncate() to set the file size, then write a single byte to |
| 3074 | ** the last byte in each block within the extended region. This |
| 3075 | ** is the same technique used by glibc to implement posix_fallocate() |
| 3076 | ** on systems that do not have a real fallocate() system call. |
| 3077 | */ |
| 3078 | int nBlk = buf.st_blksize; /* File-system block size */ |
| 3079 | i64 iWrite; /* Next offset to write to */ |
| 3080 | int nWrite; /* Return value from seekAndWrite() */ |
| 3081 | |
| 3082 | if( ftruncate(pFile->h, nSize) ){ |
| 3083 | pFile->lastErrno = errno; |
| 3084 | return SQLITE_IOERR_TRUNCATE; |
| 3085 | } |
| 3086 | iWrite = ((buf.st_size + 2*nBlk - 1)/nBlk)*nBlk-1; |
| 3087 | do { |
| 3088 | nWrite = seekAndWrite(pFile, iWrite, "", 1); |
| 3089 | iWrite += nBlk; |
| 3090 | } while( nWrite==1 && iWrite<nSize ); |
| 3091 | if( nWrite!=1 ) return SQLITE_IOERR_WRITE; |
| 3092 | #endif |
| 3093 | } |
| 3094 | } |
| 3095 | |
| 3096 | return SQLITE_OK; |
| 3097 | } |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 3098 | |
danielk1977 | e302663 | 2004-06-22 11:29:02 +0000 | [diff] [blame] | 3099 | /* |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3100 | ** Information and control of an open file handle. |
drh | 1883921 | 2005-11-26 03:43:23 +0000 | [diff] [blame] | 3101 | */ |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3102 | static int unixFileControl(sqlite3_file *id, int op, void *pArg){ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3103 | switch( op ){ |
| 3104 | case SQLITE_FCNTL_LOCKSTATE: { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 3105 | *(int*)pArg = ((unixFile*)id)->eFileLock; |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3106 | return SQLITE_OK; |
| 3107 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3108 | case SQLITE_LAST_ERRNO: { |
| 3109 | *(int*)pArg = ((unixFile*)id)->lastErrno; |
| 3110 | return SQLITE_OK; |
| 3111 | } |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3112 | case SQLITE_FCNTL_CHUNK_SIZE: { |
| 3113 | ((unixFile*)id)->szChunk = *(int *)pArg; |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3114 | return SQLITE_OK; |
dan | 6e09d69 | 2010-07-27 18:34:15 +0000 | [diff] [blame] | 3115 | } |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3116 | case SQLITE_FCNTL_SIZE_HINT: { |
dan | 502019c | 2010-07-28 14:26:17 +0000 | [diff] [blame] | 3117 | return fcntlSizeHint((unixFile *)id, *(i64 *)pArg); |
drh | 9ff27ec | 2010-05-19 19:26:05 +0000 | [diff] [blame] | 3118 | } |
drh | 8f941bc | 2009-01-14 23:03:40 +0000 | [diff] [blame] | 3119 | #ifndef NDEBUG |
| 3120 | /* The pager calls this method to signal that it has done |
| 3121 | ** a rollback and that the database is therefore unchanged and |
| 3122 | ** it hence it is OK for the transaction change counter to be |
| 3123 | ** unchanged. |
| 3124 | */ |
| 3125 | case SQLITE_FCNTL_DB_UNCHANGED: { |
| 3126 | ((unixFile*)id)->dbUpdate = 0; |
| 3127 | return SQLITE_OK; |
| 3128 | } |
| 3129 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3130 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3131 | case SQLITE_SET_LOCKPROXYFILE: |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3132 | case SQLITE_GET_LOCKPROXYFILE: { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3133 | return proxyFileControl(id,op,pArg); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3134 | } |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3135 | #endif /* SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) */ |
drh | 9e33c2c | 2007-08-31 18:34:59 +0000 | [diff] [blame] | 3136 | } |
drh | cc6bb3e | 2007-08-31 16:11:35 +0000 | [diff] [blame] | 3137 | return SQLITE_ERROR; |
drh | 9cbe635 | 2005-11-29 03:13:21 +0000 | [diff] [blame] | 3138 | } |
| 3139 | |
| 3140 | /* |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3141 | ** Return the sector size in bytes of the underlying block device for |
| 3142 | ** the specified file. This is almost always 512 bytes, but may be |
| 3143 | ** larger for some devices. |
| 3144 | ** |
| 3145 | ** SQLite code assumes this function cannot fail. It also assumes that |
| 3146 | ** if two files are created in the same file-system directory (i.e. |
drh | 85b623f | 2007-12-13 21:54:09 +0000 | [diff] [blame] | 3147 | ** a database and its journal file) that the sector size will be the |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3148 | ** same for both. |
| 3149 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3150 | static int unixSectorSize(sqlite3_file *NotUsed){ |
| 3151 | UNUSED_PARAMETER(NotUsed); |
drh | 3ceeb75 | 2007-03-29 18:19:52 +0000 | [diff] [blame] | 3152 | return SQLITE_DEFAULT_SECTOR_SIZE; |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 3153 | } |
| 3154 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3155 | /* |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3156 | ** Return the device characteristics for the file. This is always 0 for unix. |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 3157 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 3158 | static int unixDeviceCharacteristics(sqlite3_file *NotUsed){ |
| 3159 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 6207906 | 2007-08-15 17:08:46 +0000 | [diff] [blame] | 3160 | return 0; |
| 3161 | } |
| 3162 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3163 | #ifndef SQLITE_OMIT_WAL |
| 3164 | |
| 3165 | |
| 3166 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3167 | ** Object used to represent an shared memory buffer. |
| 3168 | ** |
| 3169 | ** When multiple threads all reference the same wal-index, each thread |
| 3170 | ** has its own unixShm object, but they all point to a single instance |
| 3171 | ** of this unixShmNode object. In other words, each wal-index is opened |
| 3172 | ** only once per process. |
| 3173 | ** |
| 3174 | ** Each unixShmNode object is connected to a single unixInodeInfo object. |
| 3175 | ** We could coalesce this object into unixInodeInfo, but that would mean |
| 3176 | ** every open file that does not use shared memory (in other words, most |
| 3177 | ** open files) would have to carry around this extra information. So |
| 3178 | ** the unixInodeInfo object contains a pointer to this unixShmNode object |
| 3179 | ** and the unixShmNode object is created only when needed. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3180 | ** |
| 3181 | ** unixMutexHeld() must be true when creating or destroying |
| 3182 | ** this object or while reading or writing the following fields: |
| 3183 | ** |
| 3184 | ** nRef |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3185 | ** |
| 3186 | ** The following fields are read-only after the object is created: |
| 3187 | ** |
| 3188 | ** fid |
| 3189 | ** zFilename |
| 3190 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3191 | ** Either unixShmNode.mutex must be held or unixShmNode.nRef==0 and |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3192 | ** unixMutexHeld() is true when reading or writing any other field |
| 3193 | ** in this structure. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3194 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3195 | struct unixShmNode { |
| 3196 | unixInodeInfo *pInode; /* unixInodeInfo that owns this SHM node */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3197 | sqlite3_mutex *mutex; /* Mutex to access this object */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3198 | char *zFilename; /* Name of the mmapped file */ |
| 3199 | int h; /* Open file descriptor */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3200 | int szRegion; /* Size of shared-memory regions */ |
| 3201 | int nRegion; /* Size of array apRegion */ |
| 3202 | char **apRegion; /* Array of mapped shared-memory regions */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3203 | int nRef; /* Number of unixShm objects pointing to this */ |
| 3204 | unixShm *pFirst; /* All unixShm objects pointing to this */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3205 | #ifdef SQLITE_DEBUG |
| 3206 | u8 exclMask; /* Mask of exclusive locks held */ |
| 3207 | u8 sharedMask; /* Mask of shared locks held */ |
| 3208 | u8 nextShmId; /* Next available unixShm.id value */ |
| 3209 | #endif |
| 3210 | }; |
| 3211 | |
| 3212 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3213 | ** Structure used internally by this VFS to record the state of an |
| 3214 | ** open shared memory connection. |
| 3215 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3216 | ** The following fields are initialized when this object is created and |
| 3217 | ** are read-only thereafter: |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3218 | ** |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3219 | ** unixShm.pFile |
| 3220 | ** unixShm.id |
| 3221 | ** |
| 3222 | ** All other fields are read/write. The unixShm.pFile->mutex must be held |
| 3223 | ** while accessing any read/write fields. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3224 | */ |
| 3225 | struct unixShm { |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3226 | unixShmNode *pShmNode; /* The underlying unixShmNode object */ |
| 3227 | unixShm *pNext; /* Next unixShm with the same unixShmNode */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3228 | u8 hasMutex; /* True if holding the unixShmNode mutex */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3229 | u16 sharedMask; /* Mask of shared locks held */ |
| 3230 | u16 exclMask; /* Mask of exclusive locks held */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3231 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3232 | u8 id; /* Id of this connection within its unixShmNode */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3233 | #endif |
| 3234 | }; |
| 3235 | |
| 3236 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3237 | ** Constants used for locking |
| 3238 | */ |
drh | bd9676c | 2010-06-23 17:58:38 +0000 | [diff] [blame] | 3239 | #define UNIX_SHM_BASE ((22+SQLITE_SHM_NLOCK)*4) /* first lock byte */ |
drh | 4222441 | 2010-05-31 14:28:25 +0000 | [diff] [blame] | 3240 | #define UNIX_SHM_DMS (UNIX_SHM_BASE+SQLITE_SHM_NLOCK) /* deadman switch */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3241 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3242 | /* |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3243 | ** Apply posix advisory locks for all bytes from ofst through ofst+n-1. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3244 | ** |
| 3245 | ** Locks block if the mask is exactly UNIX_SHM_C and are non-blocking |
| 3246 | ** otherwise. |
| 3247 | */ |
| 3248 | static int unixShmSystemLock( |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3249 | unixShmNode *pShmNode, /* Apply locks to this open shared-memory segment */ |
| 3250 | int lockType, /* F_UNLCK, F_RDLCK, or F_WRLCK */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3251 | int ofst, /* First byte of the locking range */ |
| 3252 | int n /* Number of bytes to lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3253 | ){ |
| 3254 | struct flock f; /* The posix advisory locking structure */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3255 | int rc = SQLITE_OK; /* Result code form fcntl() */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3256 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3257 | /* Access to the unixShmNode object is serialized by the caller */ |
| 3258 | assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3259 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3260 | /* Shared locks never span more than one byte */ |
| 3261 | assert( n==1 || lockType!=F_RDLCK ); |
| 3262 | |
| 3263 | /* Locks are within range */ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3264 | assert( n>=1 && n<SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3265 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3266 | /* Initialize the locking parameters */ |
| 3267 | memset(&f, 0, sizeof(f)); |
| 3268 | f.l_type = lockType; |
| 3269 | f.l_whence = SEEK_SET; |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3270 | f.l_start = ofst; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3271 | f.l_len = n; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3272 | |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3273 | rc = fcntl(pShmNode->h, F_SETLK, &f); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3274 | rc = (rc!=(-1)) ? SQLITE_OK : SQLITE_BUSY; |
| 3275 | |
| 3276 | /* Update the global lock state and do debug tracing */ |
| 3277 | #ifdef SQLITE_DEBUG |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3278 | { u16 mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3279 | OSTRACE(("SHM-LOCK ")); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3280 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3281 | if( rc==SQLITE_OK ){ |
| 3282 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3283 | OSTRACE(("unlock %d ok", ofst)); |
| 3284 | pShmNode->exclMask &= ~mask; |
| 3285 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3286 | }else if( lockType==F_RDLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3287 | OSTRACE(("read-lock %d ok", ofst)); |
| 3288 | pShmNode->exclMask &= ~mask; |
| 3289 | pShmNode->sharedMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3290 | }else{ |
| 3291 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3292 | OSTRACE(("write-lock %d ok", ofst)); |
| 3293 | pShmNode->exclMask |= mask; |
| 3294 | pShmNode->sharedMask &= ~mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3295 | } |
| 3296 | }else{ |
| 3297 | if( lockType==F_UNLCK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3298 | OSTRACE(("unlock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3299 | }else if( lockType==F_RDLCK ){ |
| 3300 | OSTRACE(("read-lock failed")); |
| 3301 | }else{ |
| 3302 | assert( lockType==F_WRLCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3303 | OSTRACE(("write-lock %d failed", ofst)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3304 | } |
| 3305 | } |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3306 | OSTRACE((" - afterwards %03x,%03x\n", |
| 3307 | pShmNode->sharedMask, pShmNode->exclMask)); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3308 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3309 | #endif |
| 3310 | |
| 3311 | return rc; |
| 3312 | } |
| 3313 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3314 | |
| 3315 | /* |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3316 | ** Purge the unixShmNodeList list of all entries with unixShmNode.nRef==0. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3317 | ** |
| 3318 | ** This is not a VFS shared-memory method; it is a utility function called |
| 3319 | ** by VFS shared-memory methods. |
| 3320 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3321 | static void unixShmPurge(unixFile *pFd){ |
| 3322 | unixShmNode *p = pFd->pInode->pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3323 | assert( unixMutexHeld() ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3324 | if( p && p->nRef==0 ){ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3325 | int i; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3326 | assert( p->pInode==pFd->pInode ); |
| 3327 | if( p->mutex ) sqlite3_mutex_free(p->mutex); |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3328 | for(i=0; i<p->nRegion; i++){ |
| 3329 | munmap(p->apRegion[i], p->szRegion); |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3330 | } |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3331 | sqlite3_free(p->apRegion); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3332 | if( p->h>=0 ) close(p->h); |
| 3333 | p->pInode->pShmNode = 0; |
| 3334 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3335 | } |
| 3336 | } |
| 3337 | |
| 3338 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3339 | ** Open a shared-memory area associated with open database file pDbFd. |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3340 | ** This particular implementation uses mmapped files. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3341 | ** |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3342 | ** The file used to implement shared-memory is in the same directory |
| 3343 | ** as the open database file and has the same name as the open database |
| 3344 | ** file with the "-shm" suffix added. For example, if the database file |
| 3345 | ** is "/home/user1/config.db" then the file that is created and mmapped |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3346 | ** for shared memory will be called "/home/user1/config.db-shm". |
| 3347 | ** |
| 3348 | ** Another approach to is to use files in /dev/shm or /dev/tmp or an |
| 3349 | ** some other tmpfs mount. But if a file in a different directory |
| 3350 | ** from the database file is used, then differing access permissions |
| 3351 | ** or a chroot() might cause two different processes on the same |
| 3352 | ** database to end up using different files for shared memory - |
| 3353 | ** meaning that their memory would not really be shared - resulting |
| 3354 | ** in database corruption. Nevertheless, this tmpfs file usage |
| 3355 | ** can be enabled at compile-time using -DSQLITE_SHM_DIRECTORY="/dev/shm" |
| 3356 | ** or the equivalent. The use of the SQLITE_SHM_DIRECTORY compile-time |
| 3357 | ** option results in an incompatible build of SQLite; builds of SQLite |
| 3358 | ** that with differing SQLITE_SHM_DIRECTORY settings attempt to use the |
| 3359 | ** same database file at the same time, database corruption will likely |
| 3360 | ** result. The SQLITE_SHM_DIRECTORY compile-time option is considered |
| 3361 | ** "unsupported" and may go away in a future SQLite release. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3362 | ** |
| 3363 | ** When opening a new shared-memory file, if no other instances of that |
| 3364 | ** file are currently open, in this process or in other processes, then |
| 3365 | ** the file must be truncated to zero length or have its header cleared. |
| 3366 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3367 | static int unixOpenSharedMemory(unixFile *pDbFd){ |
| 3368 | struct unixShm *p = 0; /* The connection to be opened */ |
| 3369 | struct unixShmNode *pShmNode; /* The underlying mmapped file */ |
| 3370 | int rc; /* Result code */ |
| 3371 | unixInodeInfo *pInode; /* The inode of fd */ |
| 3372 | char *zShmFilename; /* Name of the file used for SHM */ |
| 3373 | int nShmFilename; /* Size of the SHM filename in bytes */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3374 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3375 | /* Allocate space for the new unixShm object. */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3376 | p = sqlite3_malloc( sizeof(*p) ); |
| 3377 | if( p==0 ) return SQLITE_NOMEM; |
| 3378 | memset(p, 0, sizeof(*p)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3379 | assert( pDbFd->pShm==0 ); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3380 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3381 | /* Check to see if a unixShmNode object already exists. Reuse an existing |
| 3382 | ** one if present. Create a new one if necessary. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3383 | */ |
| 3384 | unixEnterMutex(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 3385 | pInode = pDbFd->pInode; |
| 3386 | pShmNode = pInode->pShmNode; |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3387 | if( pShmNode==0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3388 | struct stat sStat; /* fstat() info for database file */ |
| 3389 | |
| 3390 | /* Call fstat() to figure out the permissions on the database file. If |
| 3391 | ** a new *-shm file is created, an attempt will be made to create it |
| 3392 | ** with the same permissions. The actual permissions the file is created |
| 3393 | ** with are subject to the current umask setting. |
| 3394 | */ |
| 3395 | if( fstat(pDbFd->h, &sStat) ){ |
| 3396 | rc = SQLITE_IOERR_FSTAT; |
| 3397 | goto shm_open_err; |
| 3398 | } |
| 3399 | |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3400 | #ifdef SQLITE_SHM_DIRECTORY |
| 3401 | nShmFilename = sizeof(SQLITE_SHM_DIRECTORY) + 30; |
| 3402 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3403 | nShmFilename = 5 + (int)strlen(pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3404 | #endif |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3405 | pShmNode = sqlite3_malloc( sizeof(*pShmNode) + nShmFilename ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3406 | if( pShmNode==0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3407 | rc = SQLITE_NOMEM; |
| 3408 | goto shm_open_err; |
| 3409 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3410 | memset(pShmNode, 0, sizeof(*pShmNode)); |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3411 | zShmFilename = pShmNode->zFilename = (char*)&pShmNode[1]; |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3412 | #ifdef SQLITE_SHM_DIRECTORY |
| 3413 | sqlite3_snprintf(nShmFilename, zShmFilename, |
| 3414 | SQLITE_SHM_DIRECTORY "/sqlite-shm-%x-%x", |
| 3415 | (u32)sStat.st_ino, (u32)sStat.st_dev); |
| 3416 | #else |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 3417 | sqlite3_snprintf(nShmFilename, zShmFilename, "%s-shm", pDbFd->zPath); |
drh | a4ced19 | 2010-07-15 18:32:40 +0000 | [diff] [blame] | 3418 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3419 | pShmNode->h = -1; |
| 3420 | pDbFd->pInode->pShmNode = pShmNode; |
| 3421 | pShmNode->pInode = pDbFd->pInode; |
| 3422 | pShmNode->mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST); |
| 3423 | if( pShmNode->mutex==0 ){ |
| 3424 | rc = SQLITE_NOMEM; |
| 3425 | goto shm_open_err; |
| 3426 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3427 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 3428 | pShmNode->h = open(zShmFilename, O_RDWR|O_CREAT, (sStat.st_mode & 0777)); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3429 | if( pShmNode->h<0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3430 | rc = SQLITE_CANTOPEN_BKPT; |
| 3431 | goto shm_open_err; |
| 3432 | } |
| 3433 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3434 | /* Check to see if another process is holding the dead-man switch. |
| 3435 | ** If not, truncate the file to zero length. |
| 3436 | */ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3437 | rc = SQLITE_OK; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3438 | if( unixShmSystemLock(pShmNode, F_WRLCK, UNIX_SHM_DMS, 1)==SQLITE_OK ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3439 | if( ftruncate(pShmNode->h, 0) ){ |
drh | aab4c02 | 2010-06-02 14:45:51 +0000 | [diff] [blame] | 3440 | rc = SQLITE_IOERR_SHMOPEN; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3441 | } |
| 3442 | } |
| 3443 | if( rc==SQLITE_OK ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3444 | rc = unixShmSystemLock(pShmNode, F_RDLCK, UNIX_SHM_DMS, 1); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3445 | } |
| 3446 | if( rc ) goto shm_open_err; |
| 3447 | } |
| 3448 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3449 | /* Make the new connection a child of the unixShmNode */ |
| 3450 | p->pShmNode = pShmNode; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3451 | #ifdef SQLITE_DEBUG |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3452 | p->id = pShmNode->nextShmId++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3453 | #endif |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3454 | pShmNode->nRef++; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3455 | pDbFd->pShm = p; |
| 3456 | unixLeaveMutex(); |
dan | 0668f59 | 2010-07-20 18:59:00 +0000 | [diff] [blame] | 3457 | |
| 3458 | /* The reference count on pShmNode has already been incremented under |
| 3459 | ** the cover of the unixEnterMutex() mutex and the pointer from the |
| 3460 | ** new (struct unixShm) object to the pShmNode has been set. All that is |
| 3461 | ** left to do is to link the new object into the linked list starting |
| 3462 | ** at pShmNode->pFirst. This must be done while holding the pShmNode->mutex |
| 3463 | ** mutex. |
| 3464 | */ |
| 3465 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3466 | p->pNext = pShmNode->pFirst; |
| 3467 | pShmNode->pFirst = p; |
| 3468 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3469 | return SQLITE_OK; |
| 3470 | |
| 3471 | /* Jump here on any error */ |
| 3472 | shm_open_err: |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3473 | unixShmPurge(pDbFd); /* This call frees pShmNode if required */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3474 | sqlite3_free(p); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3475 | unixLeaveMutex(); |
| 3476 | return rc; |
| 3477 | } |
| 3478 | |
| 3479 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3480 | ** This function is called to obtain a pointer to region iRegion of the |
| 3481 | ** shared-memory associated with the database file fd. Shared-memory regions |
| 3482 | ** are numbered starting from zero. Each shared-memory region is szRegion |
| 3483 | ** bytes in size. |
| 3484 | ** |
| 3485 | ** If an error occurs, an error code is returned and *pp is set to NULL. |
| 3486 | ** |
| 3487 | ** Otherwise, if the bExtend parameter is 0 and the requested shared-memory |
| 3488 | ** region has not been allocated (by any client, including one running in a |
| 3489 | ** separate process), then *pp is set to NULL and SQLITE_OK returned. If |
| 3490 | ** bExtend is non-zero and the requested shared-memory region has not yet |
| 3491 | ** been allocated, it is allocated by this function. |
| 3492 | ** |
| 3493 | ** If the shared-memory region has already been allocated or is allocated by |
| 3494 | ** this call as described above, then it is mapped into this processes |
| 3495 | ** address space (if it is not already), *pp is set to point to the mapped |
| 3496 | ** memory and SQLITE_OK returned. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3497 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3498 | static int unixShmMap( |
| 3499 | sqlite3_file *fd, /* Handle open on database file */ |
| 3500 | int iRegion, /* Region to retrieve */ |
| 3501 | int szRegion, /* Size of regions */ |
| 3502 | int bExtend, /* True to extend file if necessary */ |
| 3503 | void volatile **pp /* OUT: Mapped memory */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3504 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3505 | unixFile *pDbFd = (unixFile*)fd; |
| 3506 | unixShm *p; |
| 3507 | unixShmNode *pShmNode; |
| 3508 | int rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3509 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3510 | /* If the shared-memory file has not yet been opened, open it now. */ |
| 3511 | if( pDbFd->pShm==0 ){ |
| 3512 | rc = unixOpenSharedMemory(pDbFd); |
| 3513 | if( rc!=SQLITE_OK ) return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3514 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3515 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3516 | p = pDbFd->pShm; |
| 3517 | pShmNode = p->pShmNode; |
| 3518 | sqlite3_mutex_enter(pShmNode->mutex); |
| 3519 | assert( szRegion==pShmNode->szRegion || pShmNode->nRegion==0 ); |
| 3520 | |
| 3521 | if( pShmNode->nRegion<=iRegion ){ |
| 3522 | char **apNew; /* New apRegion[] array */ |
| 3523 | int nByte = (iRegion+1)*szRegion; /* Minimum required file size */ |
| 3524 | struct stat sStat; /* Used by fstat() */ |
| 3525 | |
| 3526 | pShmNode->szRegion = szRegion; |
| 3527 | |
| 3528 | /* The requested region is not mapped into this processes address space. |
| 3529 | ** Check to see if it has been allocated (i.e. if the wal-index file is |
| 3530 | ** large enough to contain the requested region). |
| 3531 | */ |
| 3532 | if( fstat(pShmNode->h, &sStat) ){ |
| 3533 | rc = SQLITE_IOERR_SHMSIZE; |
| 3534 | goto shmpage_out; |
| 3535 | } |
| 3536 | |
| 3537 | if( sStat.st_size<nByte ){ |
| 3538 | /* The requested memory region does not exist. If bExtend is set to |
| 3539 | ** false, exit early. *pp will be set to NULL and SQLITE_OK returned. |
| 3540 | ** |
| 3541 | ** Alternatively, if bExtend is true, use ftruncate() to allocate |
| 3542 | ** the requested memory region. |
| 3543 | */ |
| 3544 | if( !bExtend ) goto shmpage_out; |
| 3545 | if( ftruncate(pShmNode->h, nByte) ){ |
| 3546 | rc = SQLITE_IOERR_SHMSIZE; |
| 3547 | goto shmpage_out; |
| 3548 | } |
| 3549 | } |
| 3550 | |
| 3551 | /* Map the requested memory region into this processes address space. */ |
| 3552 | apNew = (char **)sqlite3_realloc( |
| 3553 | pShmNode->apRegion, (iRegion+1)*sizeof(char *) |
| 3554 | ); |
| 3555 | if( !apNew ){ |
| 3556 | rc = SQLITE_IOERR_NOMEM; |
| 3557 | goto shmpage_out; |
| 3558 | } |
| 3559 | pShmNode->apRegion = apNew; |
| 3560 | while(pShmNode->nRegion<=iRegion){ |
| 3561 | void *pMem = mmap(0, szRegion, PROT_READ|PROT_WRITE, |
drh | 37e8b5b | 2010-09-02 14:00:19 +0000 | [diff] [blame] | 3562 | MAP_SHARED, pShmNode->h, pShmNode->nRegion*szRegion |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3563 | ); |
| 3564 | if( pMem==MAP_FAILED ){ |
| 3565 | rc = SQLITE_IOERR; |
| 3566 | goto shmpage_out; |
| 3567 | } |
| 3568 | pShmNode->apRegion[pShmNode->nRegion] = pMem; |
| 3569 | pShmNode->nRegion++; |
| 3570 | } |
| 3571 | } |
| 3572 | |
| 3573 | shmpage_out: |
| 3574 | if( pShmNode->nRegion>iRegion ){ |
| 3575 | *pp = pShmNode->apRegion[iRegion]; |
| 3576 | }else{ |
| 3577 | *pp = 0; |
| 3578 | } |
| 3579 | sqlite3_mutex_leave(pShmNode->mutex); |
| 3580 | return rc; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
| 3583 | /* |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3584 | ** Change the lock state for a shared-memory segment. |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3585 | ** |
| 3586 | ** Note that the relationship between SHAREd and EXCLUSIVE locks is a little |
| 3587 | ** different here than in posix. In xShmLock(), one can go from unlocked |
| 3588 | ** to shared and back or from unlocked to exclusive and back. But one may |
| 3589 | ** not go from shared to exclusive or from exclusive to shared. |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3590 | */ |
| 3591 | static int unixShmLock( |
| 3592 | sqlite3_file *fd, /* Database file holding the shared memory */ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3593 | int ofst, /* First lock to acquire or release */ |
| 3594 | int n, /* Number of locks to acquire or release */ |
| 3595 | int flags /* What to do with the lock */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3596 | ){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3597 | unixFile *pDbFd = (unixFile*)fd; /* Connection holding shared memory */ |
| 3598 | unixShm *p = pDbFd->pShm; /* The shared memory being locked */ |
| 3599 | unixShm *pX; /* For looping over all siblings */ |
| 3600 | unixShmNode *pShmNode = p->pShmNode; /* The underlying file iNode */ |
| 3601 | int rc = SQLITE_OK; /* Result code */ |
| 3602 | u16 mask; /* Mask of locks to take or release */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3603 | |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3604 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3605 | assert( pShmNode->pInode==pDbFd->pInode ); |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3606 | assert( ofst>=0 && ofst+n<=SQLITE_SHM_NLOCK ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3607 | assert( n>=1 ); |
| 3608 | assert( flags==(SQLITE_SHM_LOCK | SQLITE_SHM_SHARED) |
| 3609 | || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) |
| 3610 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) |
| 3611 | || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); |
| 3612 | assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3613 | |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3614 | mask = (1<<(ofst+n)) - (1<<ofst); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3615 | assert( n>1 || mask==(1<<ofst) ); |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3616 | sqlite3_mutex_enter(pShmNode->mutex); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3617 | if( flags & SQLITE_SHM_UNLOCK ){ |
| 3618 | u16 allMask = 0; /* Mask of locks held by siblings */ |
| 3619 | |
| 3620 | /* See if any siblings hold this same lock */ |
| 3621 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
| 3622 | if( pX==p ) continue; |
| 3623 | assert( (pX->exclMask & (p->exclMask|p->sharedMask))==0 ); |
| 3624 | allMask |= pX->sharedMask; |
| 3625 | } |
| 3626 | |
| 3627 | /* Unlock the system-level locks */ |
| 3628 | if( (mask & allMask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3629 | rc = unixShmSystemLock(pShmNode, F_UNLCK, ofst+UNIX_SHM_BASE, n); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3630 | }else{ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3631 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3632 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3633 | |
| 3634 | /* Undo the local locks */ |
| 3635 | if( rc==SQLITE_OK ){ |
| 3636 | p->exclMask &= ~mask; |
| 3637 | p->sharedMask &= ~mask; |
| 3638 | } |
| 3639 | }else if( flags & SQLITE_SHM_SHARED ){ |
| 3640 | u16 allShared = 0; /* Union of locks held by connections other than "p" */ |
| 3641 | |
| 3642 | /* Find out which shared locks are already held by sibling connections. |
| 3643 | ** If any sibling already holds an exclusive lock, go ahead and return |
| 3644 | ** SQLITE_BUSY. |
| 3645 | */ |
| 3646 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3647 | if( (pX->exclMask & mask)!=0 ){ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3648 | rc = SQLITE_BUSY; |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3649 | break; |
| 3650 | } |
| 3651 | allShared |= pX->sharedMask; |
| 3652 | } |
| 3653 | |
| 3654 | /* Get shared locks at the system level, if necessary */ |
| 3655 | if( rc==SQLITE_OK ){ |
| 3656 | if( (allShared & mask)==0 ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3657 | rc = unixShmSystemLock(pShmNode, F_RDLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3658 | }else{ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3659 | rc = SQLITE_OK; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3660 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3661 | } |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3662 | |
| 3663 | /* Get the local shared locks */ |
| 3664 | if( rc==SQLITE_OK ){ |
| 3665 | p->sharedMask |= mask; |
| 3666 | } |
| 3667 | }else{ |
| 3668 | /* Make sure no sibling connections hold locks that will block this |
| 3669 | ** lock. If any do, return SQLITE_BUSY right away. |
| 3670 | */ |
| 3671 | for(pX=pShmNode->pFirst; pX; pX=pX->pNext){ |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3672 | if( (pX->exclMask & mask)!=0 || (pX->sharedMask & mask)!=0 ){ |
| 3673 | rc = SQLITE_BUSY; |
| 3674 | break; |
| 3675 | } |
| 3676 | } |
| 3677 | |
| 3678 | /* Get the exclusive locks at the system level. Then if successful |
| 3679 | ** also mark the local connection as being locked. |
| 3680 | */ |
| 3681 | if( rc==SQLITE_OK ){ |
drh | c99597c | 2010-05-31 01:41:15 +0000 | [diff] [blame] | 3682 | rc = unixShmSystemLock(pShmNode, F_WRLCK, ofst+UNIX_SHM_BASE, n); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3683 | if( rc==SQLITE_OK ){ |
drh | 15d6809 | 2010-05-31 16:56:14 +0000 | [diff] [blame] | 3684 | assert( (p->sharedMask & mask)==0 ); |
drh | 73b64e4 | 2010-05-30 19:55:15 +0000 | [diff] [blame] | 3685 | p->exclMask |= mask; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3686 | } |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3687 | } |
| 3688 | } |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 3689 | sqlite3_mutex_leave(pShmNode->mutex); |
drh | 20e1f08 | 2010-05-31 16:10:12 +0000 | [diff] [blame] | 3690 | OSTRACE(("SHM-LOCK shmid-%d, pid-%d got %03x,%03x\n", |
| 3691 | p->id, getpid(), p->sharedMask, p->exclMask)); |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3692 | return rc; |
| 3693 | } |
| 3694 | |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3695 | /* |
| 3696 | ** Implement a memory barrier or memory fence on shared memory. |
| 3697 | ** |
| 3698 | ** All loads and stores begun before the barrier must complete before |
| 3699 | ** any load or store begun after the barrier. |
| 3700 | */ |
| 3701 | static void unixShmBarrier( |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3702 | sqlite3_file *fd /* Database file holding the shared memory */ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3703 | ){ |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 3704 | UNUSED_PARAMETER(fd); |
drh | b29ad85 | 2010-06-01 00:03:57 +0000 | [diff] [blame] | 3705 | unixEnterMutex(); |
| 3706 | unixLeaveMutex(); |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3707 | } |
| 3708 | |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3709 | /* |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3710 | ** Close a connection to shared-memory. Delete the underlying |
| 3711 | ** storage if deleteFlag is true. |
drh | e11fedc | 2010-07-14 00:14:30 +0000 | [diff] [blame] | 3712 | ** |
| 3713 | ** If there is no shared memory associated with the connection then this |
| 3714 | ** routine is a harmless no-op. |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3715 | */ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3716 | static int unixShmUnmap( |
| 3717 | sqlite3_file *fd, /* The underlying database file */ |
| 3718 | int deleteFlag /* Delete shared-memory if true */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3719 | ){ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3720 | unixShm *p; /* The connection to be closed */ |
| 3721 | unixShmNode *pShmNode; /* The underlying shared-memory file */ |
| 3722 | unixShm **pp; /* For looping over sibling connections */ |
| 3723 | unixFile *pDbFd; /* The underlying database file */ |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3724 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3725 | pDbFd = (unixFile*)fd; |
| 3726 | p = pDbFd->pShm; |
| 3727 | if( p==0 ) return SQLITE_OK; |
| 3728 | pShmNode = p->pShmNode; |
| 3729 | |
| 3730 | assert( pShmNode==pDbFd->pInode->pShmNode ); |
| 3731 | assert( pShmNode->pInode==pDbFd->pInode ); |
| 3732 | |
| 3733 | /* Remove connection p from the set of connections associated |
| 3734 | ** with pShmNode */ |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3735 | sqlite3_mutex_enter(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3736 | for(pp=&pShmNode->pFirst; (*pp)!=p; pp = &(*pp)->pNext){} |
| 3737 | *pp = p->pNext; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3738 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3739 | /* Free the connection p */ |
| 3740 | sqlite3_free(p); |
| 3741 | pDbFd->pShm = 0; |
dan | 1880191 | 2010-06-14 14:07:50 +0000 | [diff] [blame] | 3742 | sqlite3_mutex_leave(pShmNode->mutex); |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3743 | |
| 3744 | /* If pShmNode->nRef has reached 0, then close the underlying |
| 3745 | ** shared-memory file, too */ |
| 3746 | unixEnterMutex(); |
| 3747 | assert( pShmNode->nRef>0 ); |
| 3748 | pShmNode->nRef--; |
| 3749 | if( pShmNode->nRef==0 ){ |
| 3750 | if( deleteFlag ) unlink(pShmNode->zFilename); |
| 3751 | unixShmPurge(pDbFd); |
| 3752 | } |
| 3753 | unixLeaveMutex(); |
| 3754 | |
| 3755 | return SQLITE_OK; |
dan | 13a3cb8 | 2010-06-11 19:04:21 +0000 | [diff] [blame] | 3756 | } |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3757 | |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3758 | |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3759 | #else |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 3760 | # define unixShmMap 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3761 | # define unixShmLock 0 |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3762 | # define unixShmBarrier 0 |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3763 | # define unixShmUnmap 0 |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3764 | #endif /* #ifndef SQLITE_OMIT_WAL */ |
| 3765 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3766 | /* |
| 3767 | ** Here ends the implementation of all sqlite3_file methods. |
| 3768 | ** |
| 3769 | ********************** End sqlite3_file Methods ******************************* |
| 3770 | ******************************************************************************/ |
| 3771 | |
| 3772 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3773 | ** This division contains definitions of sqlite3_io_methods objects that |
| 3774 | ** implement various file locking strategies. It also contains definitions |
| 3775 | ** of "finder" functions. A finder-function is used to locate the appropriate |
| 3776 | ** sqlite3_io_methods object for a particular database file. The pAppData |
| 3777 | ** field of the sqlite3_vfs VFS objects are initialized to be pointers to |
| 3778 | ** the correct finder-function for that VFS. |
| 3779 | ** |
| 3780 | ** Most finder functions return a pointer to a fixed sqlite3_io_methods |
| 3781 | ** object. The only interesting finder-function is autolockIoFinder, which |
| 3782 | ** looks at the filesystem type and tries to guess the best locking |
| 3783 | ** strategy from that. |
| 3784 | ** |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3785 | ** For finder-funtion F, two objects are created: |
| 3786 | ** |
| 3787 | ** (1) The real finder-function named "FImpt()". |
| 3788 | ** |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 3789 | ** (2) A constant pointer to this function named just "F". |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3790 | ** |
| 3791 | ** |
| 3792 | ** A pointer to the F pointer is used as the pAppData value for VFS |
| 3793 | ** objects. We have to do this instead of letting pAppData point |
| 3794 | ** directly at the finder-function since C90 rules prevent a void* |
| 3795 | ** from be cast into a function pointer. |
| 3796 | ** |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3797 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3798 | ** Each instance of this macro generates two objects: |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3799 | ** |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3800 | ** * A constant sqlite3_io_methods object call METHOD that has locking |
| 3801 | ** methods CLOSE, LOCK, UNLOCK, CKRESLOCK. |
| 3802 | ** |
| 3803 | ** * An I/O method finder function called FINDER that returns a pointer |
| 3804 | ** to the METHOD object in the previous bullet. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3805 | */ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3806 | #define IOMETHODS(FINDER, METHOD, VERSION, CLOSE, LOCK, UNLOCK, CKLOCK) \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3807 | static const sqlite3_io_methods METHOD = { \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3808 | VERSION, /* iVersion */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3809 | CLOSE, /* xClose */ \ |
| 3810 | unixRead, /* xRead */ \ |
| 3811 | unixWrite, /* xWrite */ \ |
| 3812 | unixTruncate, /* xTruncate */ \ |
| 3813 | unixSync, /* xSync */ \ |
| 3814 | unixFileSize, /* xFileSize */ \ |
| 3815 | LOCK, /* xLock */ \ |
| 3816 | UNLOCK, /* xUnlock */ \ |
| 3817 | CKLOCK, /* xCheckReservedLock */ \ |
| 3818 | unixFileControl, /* xFileControl */ \ |
| 3819 | unixSectorSize, /* xSectorSize */ \ |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 3820 | unixDeviceCharacteristics, /* xDeviceCapabilities */ \ |
drh | 6b017cc | 2010-06-14 18:01:46 +0000 | [diff] [blame] | 3821 | unixShmMap, /* xShmMap */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3822 | unixShmLock, /* xShmLock */ \ |
drh | 286a288 | 2010-05-20 23:51:06 +0000 | [diff] [blame] | 3823 | unixShmBarrier, /* xShmBarrier */ \ |
dan | da9fe0c | 2010-07-13 18:44:03 +0000 | [diff] [blame] | 3824 | unixShmUnmap /* xShmUnmap */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3825 | }; \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3826 | static const sqlite3_io_methods *FINDER##Impl(const char *z, unixFile *p){ \ |
| 3827 | UNUSED_PARAMETER(z); UNUSED_PARAMETER(p); \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3828 | return &METHOD; \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3829 | } \ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3830 | static const sqlite3_io_methods *(*const FINDER)(const char*,unixFile *p) \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3831 | = FINDER##Impl; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3832 | |
| 3833 | /* |
| 3834 | ** Here are all of the sqlite3_io_methods objects for each of the |
| 3835 | ** locking strategies. Functions that return pointers to these methods |
| 3836 | ** are also created. |
| 3837 | */ |
| 3838 | IOMETHODS( |
| 3839 | posixIoFinder, /* Finder function name */ |
| 3840 | posixIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3841 | 2, /* shared memory is enabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3842 | unixClose, /* xClose method */ |
| 3843 | unixLock, /* xLock method */ |
| 3844 | unixUnlock, /* xUnlock method */ |
| 3845 | unixCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3846 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3847 | IOMETHODS( |
| 3848 | nolockIoFinder, /* Finder function name */ |
| 3849 | nolockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3850 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3851 | nolockClose, /* xClose method */ |
| 3852 | nolockLock, /* xLock method */ |
| 3853 | nolockUnlock, /* xUnlock method */ |
| 3854 | nolockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3855 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3856 | IOMETHODS( |
| 3857 | dotlockIoFinder, /* Finder function name */ |
| 3858 | dotlockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3859 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3860 | dotlockClose, /* xClose method */ |
| 3861 | dotlockLock, /* xLock method */ |
| 3862 | dotlockUnlock, /* xUnlock method */ |
| 3863 | dotlockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3864 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3865 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 3866 | #if SQLITE_ENABLE_LOCKING_STYLE && !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3867 | IOMETHODS( |
| 3868 | flockIoFinder, /* Finder function name */ |
| 3869 | flockIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3870 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3871 | flockClose, /* xClose method */ |
| 3872 | flockLock, /* xLock method */ |
| 3873 | flockUnlock, /* xUnlock method */ |
| 3874 | flockCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3875 | ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3876 | #endif |
| 3877 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 3878 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3879 | IOMETHODS( |
| 3880 | semIoFinder, /* Finder function name */ |
| 3881 | semIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3882 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3883 | semClose, /* xClose method */ |
| 3884 | semLock, /* xLock method */ |
| 3885 | semUnlock, /* xUnlock method */ |
| 3886 | semCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3887 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3888 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3889 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3890 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3891 | IOMETHODS( |
| 3892 | afpIoFinder, /* Finder function name */ |
| 3893 | afpIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3894 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3895 | afpClose, /* xClose method */ |
| 3896 | afpLock, /* xLock method */ |
| 3897 | afpUnlock, /* xUnlock method */ |
| 3898 | afpCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3899 | ) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3900 | #endif |
| 3901 | |
| 3902 | /* |
| 3903 | ** The proxy locking method is a "super-method" in the sense that it |
| 3904 | ** opens secondary file descriptors for the conch and lock files and |
| 3905 | ** it uses proxy, dot-file, AFP, and flock() locking methods on those |
| 3906 | ** secondary files. For this reason, the division that implements |
| 3907 | ** proxy locking is located much further down in the file. But we need |
| 3908 | ** to go ahead and define the sqlite3_io_methods and finder function |
| 3909 | ** for proxy locking here. So we forward declare the I/O methods. |
| 3910 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3911 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 3912 | static int proxyClose(sqlite3_file*); |
| 3913 | static int proxyLock(sqlite3_file*, int); |
| 3914 | static int proxyUnlock(sqlite3_file*, int); |
| 3915 | static int proxyCheckReservedLock(sqlite3_file*, int*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3916 | IOMETHODS( |
| 3917 | proxyIoFinder, /* Finder function name */ |
| 3918 | proxyIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3919 | 1, /* shared memory is disabled */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3920 | proxyClose, /* xClose method */ |
| 3921 | proxyLock, /* xLock method */ |
| 3922 | proxyUnlock, /* xUnlock method */ |
| 3923 | proxyCheckReservedLock /* xCheckReservedLock method */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3924 | ) |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 3925 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3926 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3927 | /* nfs lockd on OSX 10.3+ doesn't clear write locks when a read lock is set */ |
| 3928 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 3929 | IOMETHODS( |
| 3930 | nfsIoFinder, /* Finder function name */ |
| 3931 | nfsIoMethods, /* sqlite3_io_methods object name */ |
drh | 6e1f482 | 2010-07-13 23:41:40 +0000 | [diff] [blame] | 3932 | 1, /* shared memory is disabled */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3933 | unixClose, /* xClose method */ |
| 3934 | unixLock, /* xLock method */ |
| 3935 | nfsUnlock, /* xUnlock method */ |
| 3936 | unixCheckReservedLock /* xCheckReservedLock method */ |
| 3937 | ) |
| 3938 | #endif |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3939 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 3940 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3941 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3942 | ** This "finder" function attempts to determine the best locking strategy |
| 3943 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3944 | ** object that implements that strategy. |
| 3945 | ** |
| 3946 | ** This is for MacOSX only. |
| 3947 | */ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 3948 | static const sqlite3_io_methods *autolockIoFinderImpl( |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3949 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3950 | unixFile *pNew /* open file object for the database file */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3951 | ){ |
| 3952 | static const struct Mapping { |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3953 | const char *zFilesystem; /* Filesystem type name */ |
| 3954 | const sqlite3_io_methods *pMethods; /* Appropriate locking method */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3955 | } aMap[] = { |
| 3956 | { "hfs", &posixIoMethods }, |
| 3957 | { "ufs", &posixIoMethods }, |
| 3958 | { "afpfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3959 | { "smbfs", &afpIoMethods }, |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3960 | { "webdav", &nolockIoMethods }, |
| 3961 | { 0, 0 } |
| 3962 | }; |
| 3963 | int i; |
| 3964 | struct statfs fsInfo; |
| 3965 | struct flock lockInfo; |
| 3966 | |
| 3967 | if( !filePath ){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 3968 | /* If filePath==NULL that means we are dealing with a transient file |
| 3969 | ** that does not need to be locked. */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3970 | return &nolockIoMethods; |
| 3971 | } |
| 3972 | if( statfs(filePath, &fsInfo) != -1 ){ |
| 3973 | if( fsInfo.f_flags & MNT_RDONLY ){ |
| 3974 | return &nolockIoMethods; |
| 3975 | } |
| 3976 | for(i=0; aMap[i].zFilesystem; i++){ |
| 3977 | if( strcmp(fsInfo.f_fstypename, aMap[i].zFilesystem)==0 ){ |
| 3978 | return aMap[i].pMethods; |
| 3979 | } |
| 3980 | } |
| 3981 | } |
| 3982 | |
| 3983 | /* Default case. Handles, amongst others, "nfs". |
| 3984 | ** Test byte-range lock using fcntl(). If the call succeeds, |
| 3985 | ** assume that the file-system supports POSIX style locks. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 3986 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3987 | lockInfo.l_len = 1; |
| 3988 | lockInfo.l_start = 0; |
| 3989 | lockInfo.l_whence = SEEK_SET; |
| 3990 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 3991 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 3992 | if( strcmp(fsInfo.f_fstypename, "nfs")==0 ){ |
| 3993 | return &nfsIoMethods; |
| 3994 | } else { |
| 3995 | return &posixIoMethods; |
| 3996 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 3997 | }else{ |
| 3998 | return &dotlockIoMethods; |
| 3999 | } |
| 4000 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4001 | static const sqlite3_io_methods |
| 4002 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4003 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4004 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4005 | |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4006 | #if OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE |
| 4007 | /* |
| 4008 | ** This "finder" function attempts to determine the best locking strategy |
| 4009 | ** for the database file "filePath". It then returns the sqlite3_io_methods |
| 4010 | ** object that implements that strategy. |
| 4011 | ** |
| 4012 | ** This is for VXWorks only. |
| 4013 | */ |
| 4014 | static const sqlite3_io_methods *autolockIoFinderImpl( |
| 4015 | const char *filePath, /* name of the database file */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4016 | unixFile *pNew /* the open file object */ |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4017 | ){ |
| 4018 | struct flock lockInfo; |
| 4019 | |
| 4020 | if( !filePath ){ |
| 4021 | /* If filePath==NULL that means we are dealing with a transient file |
| 4022 | ** that does not need to be locked. */ |
| 4023 | return &nolockIoMethods; |
| 4024 | } |
| 4025 | |
| 4026 | /* Test if fcntl() is supported and use POSIX style locks. |
| 4027 | ** Otherwise fall back to the named semaphore method. |
| 4028 | */ |
| 4029 | lockInfo.l_len = 1; |
| 4030 | lockInfo.l_start = 0; |
| 4031 | lockInfo.l_whence = SEEK_SET; |
| 4032 | lockInfo.l_type = F_RDLCK; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4033 | if( fcntl(pNew->h, F_GETLK, &lockInfo)!=-1 ) { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4034 | return &posixIoMethods; |
| 4035 | }else{ |
| 4036 | return &semIoMethods; |
| 4037 | } |
| 4038 | } |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4039 | static const sqlite3_io_methods |
| 4040 | *(*const autolockIoFinder)(const char*,unixFile*) = autolockIoFinderImpl; |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 4041 | |
| 4042 | #endif /* OS_VXWORKS && SQLITE_ENABLE_LOCKING_STYLE */ |
| 4043 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4044 | /* |
| 4045 | ** An abstract type for a pointer to a IO method finder function: |
| 4046 | */ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4047 | typedef const sqlite3_io_methods *(*finder_type)(const char*,unixFile*); |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4048 | |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4049 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4050 | /**************************************************************************** |
| 4051 | **************************** sqlite3_vfs methods **************************** |
| 4052 | ** |
| 4053 | ** This division contains the implementation of methods on the |
| 4054 | ** sqlite3_vfs object. |
| 4055 | */ |
| 4056 | |
danielk1977 | a3d4c88 | 2007-03-23 10:08:38 +0000 | [diff] [blame] | 4057 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4058 | ** Initialize the contents of the unixFile structure pointed to by pId. |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4059 | */ |
| 4060 | static int fillInUnixFile( |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4061 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4062 | int h, /* Open file descriptor of file being opened */ |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4063 | int dirfd, /* Directory file descriptor */ |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4064 | sqlite3_file *pId, /* Write to the unixFile structure here */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4065 | const char *zFilename, /* Name of the file being opened */ |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4066 | int noLock, /* Omit locking if true */ |
| 4067 | int isDelete /* Delete on close if true */ |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4068 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4069 | const sqlite3_io_methods *pLockingStyle; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4070 | unixFile *pNew = (unixFile *)pId; |
| 4071 | int rc = SQLITE_OK; |
| 4072 | |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4073 | assert( pNew->pInode==NULL ); |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4074 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4075 | /* Parameter isDelete is only used on vxworks. Express this explicitly |
| 4076 | ** here to prevent compiler warnings about unused parameters. |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4077 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4078 | UNUSED_PARAMETER(isDelete); |
danielk1977 | a03396a | 2008-11-19 14:35:46 +0000 | [diff] [blame] | 4079 | |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4080 | /* Usually the path zFilename should not be a relative pathname. The |
| 4081 | ** exception is when opening the proxy "conch" file in builds that |
| 4082 | ** include the special Apple locking styles. |
| 4083 | */ |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4084 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | f7f55ed | 2010-10-05 18:22:47 +0000 | [diff] [blame^] | 4085 | assert( zFilename==0 || zFilename[0]=='/' |
| 4086 | || pVfs->pAppData==(void*)&autolockIoFinder ); |
| 4087 | #else |
| 4088 | assert( zFilename==0 || zFilename[0]=='/' ); |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4089 | #endif |
dan | 0015739 | 2010-10-05 11:33:15 +0000 | [diff] [blame] | 4090 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4091 | OSTRACE(("OPEN %-3d %s\n", h, zFilename)); |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4092 | pNew->h = h; |
drh | 218c508 | 2008-03-07 00:27:10 +0000 | [diff] [blame] | 4093 | pNew->dirfd = dirfd; |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4094 | pNew->fileFlags = 0; |
drh | d9e5c4f | 2010-05-12 18:01:39 +0000 | [diff] [blame] | 4095 | pNew->zPath = zFilename; |
drh | 339eb0b | 2008-03-07 15:34:11 +0000 | [diff] [blame] | 4096 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4097 | #if OS_VXWORKS |
drh | 107886a | 2008-11-21 22:21:50 +0000 | [diff] [blame] | 4098 | pNew->pId = vxworksFindFileId(zFilename); |
| 4099 | if( pNew->pId==0 ){ |
| 4100 | noLock = 1; |
| 4101 | rc = SQLITE_NOMEM; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4102 | } |
| 4103 | #endif |
| 4104 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4105 | if( noLock ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4106 | pLockingStyle = &nolockIoMethods; |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4107 | }else{ |
drh | 0c2694b | 2009-09-03 16:23:44 +0000 | [diff] [blame] | 4108 | pLockingStyle = (**(finder_type*)pVfs->pAppData)(zFilename, pNew); |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4109 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4110 | /* Cache zFilename in the locking context (AFP and dotlock override) for |
| 4111 | ** proxyLock activation is possible (remote proxy is based on db name) |
| 4112 | ** zFilename remains valid until file is closed, to support */ |
| 4113 | pNew->lockingContext = (void*)zFilename; |
| 4114 | #endif |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4115 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4116 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4117 | if( pLockingStyle == &posixIoMethods |
| 4118 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
| 4119 | || pLockingStyle == &nfsIoMethods |
| 4120 | #endif |
| 4121 | ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4122 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4123 | rc = findInodeInfo(pNew, &pNew->pInode); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4124 | if( rc!=SQLITE_OK ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4125 | /* If an error occured in findInodeInfo(), close the file descriptor |
| 4126 | ** immediately, before releasing the mutex. findInodeInfo() may fail |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4127 | ** in two scenarios: |
| 4128 | ** |
| 4129 | ** (a) A call to fstat() failed. |
| 4130 | ** (b) A malloc failed. |
| 4131 | ** |
| 4132 | ** Scenario (b) may only occur if the process is holding no other |
| 4133 | ** file descriptors open on the same file. If there were other file |
| 4134 | ** descriptors on this file, then no malloc would be required by |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4135 | ** findInodeInfo(). If this is the case, it is quite safe to close |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4136 | ** handle h - as it is guaranteed that no posix locks will be released |
| 4137 | ** by doing so. |
| 4138 | ** |
| 4139 | ** If scenario (a) caused the error then things are not so safe. The |
| 4140 | ** implicit assumption here is that if fstat() fails, things are in |
| 4141 | ** such bad shape that dropping a lock or two doesn't matter much. |
| 4142 | */ |
| 4143 | close(h); |
| 4144 | h = -1; |
| 4145 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4146 | unixLeaveMutex(); |
| 4147 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4148 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4149 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
aswift | f0551ee | 2008-12-03 21:26:19 +0000 | [diff] [blame] | 4150 | else if( pLockingStyle == &afpIoMethods ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4151 | /* AFP locking uses the file path so it needs to be included in |
| 4152 | ** the afpLockingContext. |
| 4153 | */ |
| 4154 | afpLockingContext *pCtx; |
| 4155 | pNew->lockingContext = pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 4156 | if( pCtx==0 ){ |
| 4157 | rc = SQLITE_NOMEM; |
| 4158 | }else{ |
| 4159 | /* NB: zFilename exists and remains valid until the file is closed |
| 4160 | ** according to requirement F11141. So we do not need to make a |
| 4161 | ** copy of the filename. */ |
| 4162 | pCtx->dbPath = zFilename; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4163 | pCtx->reserved = 0; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4164 | srandomdev(); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4165 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4166 | rc = findInodeInfo(pNew, &pNew->pInode); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4167 | if( rc!=SQLITE_OK ){ |
| 4168 | sqlite3_free(pNew->lockingContext); |
| 4169 | close(h); |
| 4170 | h = -1; |
| 4171 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4172 | unixLeaveMutex(); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4173 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4174 | } |
| 4175 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4176 | |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4177 | else if( pLockingStyle == &dotlockIoMethods ){ |
| 4178 | /* Dotfile locking uses the file path so it needs to be included in |
| 4179 | ** the dotlockLockingContext |
| 4180 | */ |
| 4181 | char *zLockFile; |
| 4182 | int nFilename; |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4183 | nFilename = (int)strlen(zFilename) + 6; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4184 | zLockFile = (char *)sqlite3_malloc(nFilename); |
| 4185 | if( zLockFile==0 ){ |
| 4186 | rc = SQLITE_NOMEM; |
| 4187 | }else{ |
| 4188 | sqlite3_snprintf(nFilename, zLockFile, "%s" DOTLOCK_SUFFIX, zFilename); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4189 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4190 | pNew->lockingContext = zLockFile; |
| 4191 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4192 | |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4193 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4194 | else if( pLockingStyle == &semIoMethods ){ |
| 4195 | /* Named semaphore locking uses the file path so it needs to be |
| 4196 | ** included in the semLockingContext |
| 4197 | */ |
| 4198 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4199 | rc = findInodeInfo(pNew, &pNew->pInode); |
| 4200 | if( (rc==SQLITE_OK) && (pNew->pInode->pSem==NULL) ){ |
| 4201 | char *zSemName = pNew->pInode->aSemName; |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4202 | int n; |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4203 | sqlite3_snprintf(MAX_PATHNAME, zSemName, "/%s.sem", |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4204 | pNew->pId->zCanonicalName); |
drh | 2238dcc | 2009-08-27 17:56:20 +0000 | [diff] [blame] | 4205 | for( n=1; zSemName[n]; n++ ) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4206 | if( zSemName[n]=='/' ) zSemName[n] = '_'; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4207 | pNew->pInode->pSem = sem_open(zSemName, O_CREAT, 0666, 1); |
| 4208 | if( pNew->pInode->pSem == SEM_FAILED ){ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4209 | rc = SQLITE_NOMEM; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4210 | pNew->pInode->aSemName[0] = '\0'; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4211 | } |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4212 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4213 | unixLeaveMutex(); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4214 | } |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4215 | #endif |
aswift | 5b1a256 | 2008-08-22 00:22:35 +0000 | [diff] [blame] | 4216 | |
| 4217 | pNew->lastErrno = 0; |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4218 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4219 | if( rc!=SQLITE_OK ){ |
drh | 309e655 | 2010-02-05 18:00:26 +0000 | [diff] [blame] | 4220 | if( h>=0 ) close(h); |
| 4221 | h = -1; |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4222 | unlink(zFilename); |
| 4223 | isDelete = 0; |
| 4224 | } |
| 4225 | pNew->isDelete = isDelete; |
| 4226 | #endif |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4227 | if( rc!=SQLITE_OK ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4228 | if( dirfd>=0 ) close(dirfd); /* silent leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4229 | if( h>=0 ) close(h); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4230 | }else{ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 4231 | pNew->pMethod = pLockingStyle; |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4232 | OpenCounter(+1); |
drh | bfe6631 | 2006-10-03 17:40:40 +0000 | [diff] [blame] | 4233 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4234 | return rc; |
drh | 054889e | 2005-11-30 03:20:31 +0000 | [diff] [blame] | 4235 | } |
drh | 9c06c95 | 2005-11-26 00:25:00 +0000 | [diff] [blame] | 4236 | |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4237 | /* |
| 4238 | ** Open a file descriptor to the directory containing file zFilename. |
| 4239 | ** If successful, *pFd is set to the opened file descriptor and |
| 4240 | ** SQLITE_OK is returned. If an error occurs, either SQLITE_NOMEM |
| 4241 | ** or SQLITE_CANTOPEN is returned and *pFd is set to an undefined |
| 4242 | ** value. |
| 4243 | ** |
| 4244 | ** If SQLITE_OK is returned, the caller is responsible for closing |
| 4245 | ** the file descriptor *pFd using close(). |
| 4246 | */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4247 | static int openDirectory(const char *zFilename, int *pFd){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4248 | int ii; |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4249 | int fd = -1; |
drh | f3a65f7 | 2007-08-22 20:18:21 +0000 | [diff] [blame] | 4250 | char zDirname[MAX_PATHNAME+1]; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4251 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4252 | sqlite3_snprintf(MAX_PATHNAME, zDirname, "%s", zFilename); |
drh | 617634e | 2009-01-08 14:36:20 +0000 | [diff] [blame] | 4253 | for(ii=(int)strlen(zDirname); ii>1 && zDirname[ii]!='/'; ii--); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4254 | if( ii>0 ){ |
| 4255 | zDirname[ii] = '\0'; |
| 4256 | fd = open(zDirname, O_RDONLY|O_BINARY, 0); |
drh | 777b17a | 2007-09-20 10:02:54 +0000 | [diff] [blame] | 4257 | if( fd>=0 ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4258 | #ifdef FD_CLOEXEC |
| 4259 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4260 | #endif |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4261 | OSTRACE(("OPENDIR %-3d %s\n", fd, zDirname)); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4262 | } |
| 4263 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4264 | *pFd = fd; |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4265 | return (fd>=0?SQLITE_OK:SQLITE_CANTOPEN_BKPT); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4266 | } |
| 4267 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4268 | /* |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4269 | ** Return the name of a directory in which to put temporary files. |
| 4270 | ** If no suitable temporary file directory can be found, return NULL. |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4271 | */ |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4272 | static const char *unixTempFileDir(void){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4273 | static const char *azDirs[] = { |
| 4274 | 0, |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4275 | 0, |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4276 | "/var/tmp", |
| 4277 | "/usr/tmp", |
| 4278 | "/tmp", |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4279 | 0 /* List terminator */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4280 | }; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4281 | unsigned int i; |
| 4282 | struct stat buf; |
| 4283 | const char *zDir = 0; |
| 4284 | |
| 4285 | azDirs[0] = sqlite3_temp_directory; |
| 4286 | if( !azDirs[1] ) azDirs[1] = getenv("TMPDIR"); |
drh | 19515c8 | 2010-06-19 23:53:11 +0000 | [diff] [blame] | 4287 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); zDir=azDirs[i++]){ |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4288 | if( zDir==0 ) continue; |
| 4289 | if( stat(zDir, &buf) ) continue; |
| 4290 | if( !S_ISDIR(buf.st_mode) ) continue; |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4291 | if( access(zDir, 07) ) continue; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4292 | break; |
| 4293 | } |
| 4294 | return zDir; |
| 4295 | } |
| 4296 | |
| 4297 | /* |
| 4298 | ** Create a temporary file name in zBuf. zBuf must be allocated |
| 4299 | ** by the calling process and must be big enough to hold at least |
| 4300 | ** pVfs->mxPathname bytes. |
| 4301 | */ |
| 4302 | static int unixGetTempname(int nBuf, char *zBuf){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4303 | static const unsigned char zChars[] = |
| 4304 | "abcdefghijklmnopqrstuvwxyz" |
| 4305 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 4306 | "0123456789"; |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4307 | unsigned int i, j; |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4308 | const char *zDir; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4309 | |
| 4310 | /* It's odd to simulate an io-error here, but really this is just |
| 4311 | ** using the io-error infrastructure to test that SQLite handles this |
| 4312 | ** function failing. |
| 4313 | */ |
| 4314 | SimulateIOError( return SQLITE_IOERR ); |
| 4315 | |
drh | 7234c6d | 2010-06-19 15:10:09 +0000 | [diff] [blame] | 4316 | zDir = unixTempFileDir(); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4317 | if( zDir==0 ) zDir = "."; |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4318 | |
| 4319 | /* Check that the output buffer is large enough for the temporary file |
| 4320 | ** name. If it is not, return SQLITE_ERROR. |
| 4321 | */ |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4322 | if( (strlen(zDir) + strlen(SQLITE_TEMP_FILE_PREFIX) + 17) >= (size_t)nBuf ){ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4323 | return SQLITE_ERROR; |
| 4324 | } |
| 4325 | |
| 4326 | do{ |
| 4327 | sqlite3_snprintf(nBuf-17, zBuf, "%s/"SQLITE_TEMP_FILE_PREFIX, zDir); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4328 | j = (int)strlen(zBuf); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4329 | sqlite3_randomness(15, &zBuf[j]); |
| 4330 | for(i=0; i<15; i++, j++){ |
| 4331 | zBuf[j] = (char)zChars[ ((unsigned char)zBuf[j])%(sizeof(zChars)-1) ]; |
| 4332 | } |
| 4333 | zBuf[j] = 0; |
| 4334 | }while( access(zBuf,0)==0 ); |
| 4335 | return SQLITE_OK; |
| 4336 | } |
| 4337 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 4338 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4339 | /* |
| 4340 | ** Routine to transform a unixFile into a proxy-locking unixFile. |
| 4341 | ** Implementation in the proxy-lock division, but used by unixOpen() |
| 4342 | ** if SQLITE_PREFER_PROXY_LOCKING is defined. |
| 4343 | */ |
| 4344 | static int proxyTransformUnixFile(unixFile*, const char*); |
drh | 947bd80 | 2008-12-04 12:34:15 +0000 | [diff] [blame] | 4345 | #endif |
drh | c66d5b6 | 2008-12-03 22:48:32 +0000 | [diff] [blame] | 4346 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4347 | /* |
| 4348 | ** Search for an unused file descriptor that was opened on the database |
| 4349 | ** file (not a journal or master-journal file) identified by pathname |
| 4350 | ** zPath with SQLITE_OPEN_XXX flags matching those passed as the second |
| 4351 | ** argument to this function. |
| 4352 | ** |
| 4353 | ** Such a file descriptor may exist if a database connection was closed |
| 4354 | ** but the associated file descriptor could not be closed because some |
| 4355 | ** other file descriptor open on the same file is holding a file-lock. |
| 4356 | ** Refer to comments in the unixClose() function and the lengthy comment |
| 4357 | ** describing "Posix Advisory Locking" at the start of this file for |
| 4358 | ** further details. Also, ticket #4018. |
| 4359 | ** |
| 4360 | ** If a suitable file descriptor is found, then it is returned. If no |
| 4361 | ** such file descriptor is located, -1 is returned. |
| 4362 | */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4363 | static UnixUnusedFd *findReusableFd(const char *zPath, int flags){ |
| 4364 | UnixUnusedFd *pUnused = 0; |
| 4365 | |
| 4366 | /* Do not search for an unused file descriptor on vxworks. Not because |
| 4367 | ** vxworks would not benefit from the change (it might, we're not sure), |
| 4368 | ** but because no way to test it is currently available. It is better |
| 4369 | ** not to risk breaking vxworks support for the sake of such an obscure |
| 4370 | ** feature. */ |
| 4371 | #if !OS_VXWORKS |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4372 | struct stat sStat; /* Results of stat() call */ |
| 4373 | |
| 4374 | /* A stat() call may fail for various reasons. If this happens, it is |
| 4375 | ** almost certain that an open() call on the same path will also fail. |
| 4376 | ** For this reason, if an error occurs in the stat() call here, it is |
| 4377 | ** ignored and -1 is returned. The caller will try to open a new file |
| 4378 | ** descriptor on the same path, fail, and return an error to SQLite. |
| 4379 | ** |
| 4380 | ** Even if a subsequent open() call does succeed, the consequences of |
| 4381 | ** not searching for a resusable file descriptor are not dire. */ |
| 4382 | if( 0==stat(zPath, &sStat) ){ |
drh | d91c68f | 2010-05-14 14:52:25 +0000 | [diff] [blame] | 4383 | unixInodeInfo *pInode; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4384 | |
| 4385 | unixEnterMutex(); |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4386 | pInode = inodeList; |
| 4387 | while( pInode && (pInode->fileId.dev!=sStat.st_dev |
| 4388 | || pInode->fileId.ino!=sStat.st_ino) ){ |
| 4389 | pInode = pInode->pNext; |
drh | 9061ad1 | 2010-01-05 00:14:49 +0000 | [diff] [blame] | 4390 | } |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4391 | if( pInode ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4392 | UnixUnusedFd **pp; |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 4393 | for(pp=&pInode->pUnused; *pp && (*pp)->flags!=flags; pp=&((*pp)->pNext)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4394 | pUnused = *pp; |
| 4395 | if( pUnused ){ |
| 4396 | *pp = pUnused->pNext; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4397 | } |
| 4398 | } |
| 4399 | unixLeaveMutex(); |
| 4400 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4401 | #endif /* if !OS_VXWORKS */ |
| 4402 | return pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4403 | } |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4404 | |
| 4405 | /* |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4406 | ** This function is called by unixOpen() to determine the unix permissions |
drh | f65bc91 | 2010-07-14 20:51:34 +0000 | [diff] [blame] | 4407 | ** to create new files with. If no error occurs, then SQLITE_OK is returned |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4408 | ** and a value suitable for passing as the third argument to open(2) is |
| 4409 | ** written to *pMode. If an IO error occurs, an SQLite error code is |
| 4410 | ** returned and the value of *pMode is not modified. |
| 4411 | ** |
| 4412 | ** If the file being opened is a temporary file, it is always created with |
| 4413 | ** the octal permissions 0600 (read/writable by owner only). If the file |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4414 | ** is a database or master journal file, it is created with the permissions |
| 4415 | ** mask SQLITE_DEFAULT_FILE_PERMISSIONS. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4416 | ** |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4417 | ** Finally, if the file being opened is a WAL or regular journal file, then |
| 4418 | ** this function queries the file-system for the permissions on the |
| 4419 | ** corresponding database file and sets *pMode to this value. Whenever |
| 4420 | ** possible, WAL and journal files are created using the same permissions |
| 4421 | ** as the associated database file. |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4422 | */ |
| 4423 | static int findCreateFileMode( |
| 4424 | const char *zPath, /* Path of file (possibly) being created */ |
| 4425 | int flags, /* Flags passed as 4th argument to xOpen() */ |
| 4426 | mode_t *pMode /* OUT: Permissions to open file with */ |
| 4427 | ){ |
| 4428 | int rc = SQLITE_OK; /* Return Code */ |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4429 | if( flags & (SQLITE_OPEN_WAL|SQLITE_OPEN_MAIN_JOURNAL) ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4430 | char zDb[MAX_PATHNAME+1]; /* Database file path */ |
| 4431 | int nDb; /* Number of valid bytes in zDb */ |
| 4432 | struct stat sStat; /* Output of stat() on database file */ |
| 4433 | |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4434 | nDb = sqlite3Strlen30(zPath) - ((flags & SQLITE_OPEN_WAL) ? 4 : 8); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4435 | memcpy(zDb, zPath, nDb); |
| 4436 | zDb[nDb] = '\0'; |
| 4437 | if( 0==stat(zDb, &sStat) ){ |
| 4438 | *pMode = sStat.st_mode & 0777; |
| 4439 | }else{ |
| 4440 | rc = SQLITE_IOERR_FSTAT; |
| 4441 | } |
| 4442 | }else if( flags & SQLITE_OPEN_DELETEONCLOSE ){ |
| 4443 | *pMode = 0600; |
| 4444 | }else{ |
| 4445 | *pMode = SQLITE_DEFAULT_FILE_PERMISSIONS; |
| 4446 | } |
| 4447 | return rc; |
| 4448 | } |
| 4449 | |
| 4450 | /* |
danielk1977 | ad94b58 | 2007-08-20 06:44:22 +0000 | [diff] [blame] | 4451 | ** Open the file zPath. |
| 4452 | ** |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4453 | ** Previously, the SQLite OS layer used three functions in place of this |
| 4454 | ** one: |
| 4455 | ** |
| 4456 | ** sqlite3OsOpenReadWrite(); |
| 4457 | ** sqlite3OsOpenReadOnly(); |
| 4458 | ** sqlite3OsOpenExclusive(); |
| 4459 | ** |
| 4460 | ** These calls correspond to the following combinations of flags: |
| 4461 | ** |
| 4462 | ** ReadWrite() -> (READWRITE | CREATE) |
| 4463 | ** ReadOnly() -> (READONLY) |
| 4464 | ** OpenExclusive() -> (READWRITE | CREATE | EXCLUSIVE) |
| 4465 | ** |
| 4466 | ** The old OpenExclusive() accepted a boolean argument - "delFlag". If |
| 4467 | ** true, the file was configured to be automatically deleted when the |
| 4468 | ** file handle closed. To achieve the same effect using this new |
| 4469 | ** interface, add the DELETEONCLOSE flag to those specified above for |
| 4470 | ** OpenExclusive(). |
| 4471 | */ |
| 4472 | static int unixOpen( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4473 | sqlite3_vfs *pVfs, /* The VFS for which this is the xOpen method */ |
| 4474 | const char *zPath, /* Pathname of file to be opened */ |
| 4475 | sqlite3_file *pFile, /* The file descriptor to be filled in */ |
| 4476 | int flags, /* Input flags to control the opening */ |
| 4477 | int *pOutFlags /* Output flags returned to SQLite core */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4478 | ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4479 | unixFile *p = (unixFile *)pFile; |
| 4480 | int fd = -1; /* File descriptor returned by open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4481 | int dirfd = -1; /* Directory file descriptor */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4482 | int openFlags = 0; /* Flags to pass to open() */ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4483 | int eType = flags&0xFFFFFF00; /* Type of file to open */ |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4484 | int noLock; /* True to omit locking primitives */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4485 | int rc = SQLITE_OK; /* Function Return Code */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4486 | |
| 4487 | int isExclusive = (flags & SQLITE_OPEN_EXCLUSIVE); |
| 4488 | int isDelete = (flags & SQLITE_OPEN_DELETEONCLOSE); |
| 4489 | int isCreate = (flags & SQLITE_OPEN_CREATE); |
| 4490 | int isReadonly = (flags & SQLITE_OPEN_READONLY); |
| 4491 | int isReadWrite = (flags & SQLITE_OPEN_READWRITE); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4492 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4493 | int isAutoProxy = (flags & SQLITE_OPEN_AUTOPROXY); |
| 4494 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4495 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4496 | /* If creating a master or main-file journal, this function will open |
| 4497 | ** a file-descriptor on the directory too. The first time unixSync() |
| 4498 | ** is called the directory file descriptor will be fsync()ed and close()d. |
| 4499 | */ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4500 | int isOpenDirectory = (isCreate && ( |
| 4501 | eType==SQLITE_OPEN_MASTER_JOURNAL |
| 4502 | || eType==SQLITE_OPEN_MAIN_JOURNAL |
| 4503 | || eType==SQLITE_OPEN_WAL |
| 4504 | )); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4505 | |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4506 | /* If argument zPath is a NULL pointer, this function is required to open |
| 4507 | ** a temporary file. Use this buffer to store the file name in. |
| 4508 | */ |
| 4509 | char zTmpname[MAX_PATHNAME+1]; |
| 4510 | const char *zName = zPath; |
| 4511 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4512 | /* Check the following statements are true: |
| 4513 | ** |
| 4514 | ** (a) Exactly one of the READWRITE and READONLY flags must be set, and |
| 4515 | ** (b) if CREATE is set, then READWRITE must also be set, and |
| 4516 | ** (c) if EXCLUSIVE is set, then CREATE must also be set. |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4517 | ** (d) if DELETEONCLOSE is set, then CREATE must also be set. |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4518 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4519 | assert((isReadonly==0 || isReadWrite==0) && (isReadWrite || isReadonly)); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4520 | assert(isCreate==0 || isReadWrite); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4521 | assert(isExclusive==0 || isCreate); |
drh | 33f4e02 | 2007-09-03 15:19:34 +0000 | [diff] [blame] | 4522 | assert(isDelete==0 || isCreate); |
| 4523 | |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4524 | /* The main DB, main journal, WAL file and master journal are never |
| 4525 | ** automatically deleted. Nor are they ever temporary files. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4526 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_DB ); |
| 4527 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MAIN_JOURNAL ); |
| 4528 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_MASTER_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4529 | assert( (!isDelete && zName) || eType!=SQLITE_OPEN_WAL ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4530 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4531 | /* Assert that the upper layer has set one of the "file-type" flags. */ |
| 4532 | assert( eType==SQLITE_OPEN_MAIN_DB || eType==SQLITE_OPEN_TEMP_DB |
| 4533 | || eType==SQLITE_OPEN_MAIN_JOURNAL || eType==SQLITE_OPEN_TEMP_JOURNAL |
| 4534 | || eType==SQLITE_OPEN_SUBJOURNAL || eType==SQLITE_OPEN_MASTER_JOURNAL |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4535 | || eType==SQLITE_OPEN_TRANSIENT_DB || eType==SQLITE_OPEN_WAL |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4536 | ); |
| 4537 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4538 | memset(p, 0, sizeof(unixFile)); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4539 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4540 | if( eType==SQLITE_OPEN_MAIN_DB ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4541 | UnixUnusedFd *pUnused; |
| 4542 | pUnused = findReusableFd(zName, flags); |
| 4543 | if( pUnused ){ |
| 4544 | fd = pUnused->fd; |
| 4545 | }else{ |
dan | 6aa657f | 2009-08-24 18:57:58 +0000 | [diff] [blame] | 4546 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4547 | if( !pUnused ){ |
| 4548 | return SQLITE_NOMEM; |
| 4549 | } |
| 4550 | } |
| 4551 | p->pUnused = pUnused; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4552 | }else if( !zName ){ |
| 4553 | /* If zName is NULL, the upper layer is requesting a temp file. */ |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4554 | assert(isDelete && !isOpenDirectory); |
drh | 8b3cf82 | 2010-06-01 21:02:51 +0000 | [diff] [blame] | 4555 | rc = unixGetTempname(MAX_PATHNAME+1, zTmpname); |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4556 | if( rc!=SQLITE_OK ){ |
| 4557 | return rc; |
| 4558 | } |
| 4559 | zName = zTmpname; |
| 4560 | } |
| 4561 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4562 | /* Determine the value of the flags parameter passed to POSIX function |
| 4563 | ** open(). These must be calculated even if open() is not called, as |
| 4564 | ** they may be stored as part of the file handle and used by the |
| 4565 | ** 'conch file' locking functions later on. */ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 4566 | if( isReadonly ) openFlags |= O_RDONLY; |
| 4567 | if( isReadWrite ) openFlags |= O_RDWR; |
| 4568 | if( isCreate ) openFlags |= O_CREAT; |
| 4569 | if( isExclusive ) openFlags |= (O_EXCL|O_NOFOLLOW); |
| 4570 | openFlags |= (O_LARGEFILE|O_BINARY); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4571 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4572 | if( fd<0 ){ |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4573 | mode_t openMode; /* Permissions to create file with */ |
| 4574 | rc = findCreateFileMode(zName, flags, &openMode); |
| 4575 | if( rc!=SQLITE_OK ){ |
| 4576 | assert( !p->pUnused ); |
drh | 8ab5866 | 2010-07-15 18:38:39 +0000 | [diff] [blame] | 4577 | assert( eType==SQLITE_OPEN_WAL || eType==SQLITE_OPEN_MAIN_JOURNAL ); |
dan | ddb0ac4 | 2010-07-14 14:48:58 +0000 | [diff] [blame] | 4578 | return rc; |
| 4579 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4580 | fd = open(zName, openFlags, openMode); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 4581 | OSTRACE(("OPENX %-3d %s 0%o\n", fd, zName, openFlags)); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4582 | if( fd<0 && errno!=EISDIR && isReadWrite && !isExclusive ){ |
| 4583 | /* Failed to open the file for read/write access. Try read-only. */ |
| 4584 | flags &= ~(SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE); |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4585 | openFlags &= ~(O_RDWR|O_CREAT); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4586 | flags |= SQLITE_OPEN_READONLY; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4587 | openFlags |= O_RDONLY; |
| 4588 | fd = open(zName, openFlags, openMode); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4589 | } |
| 4590 | if( fd<0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4591 | rc = SQLITE_CANTOPEN_BKPT; |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4592 | goto open_finished; |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4593 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4594 | } |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4595 | assert( fd>=0 ); |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4596 | if( pOutFlags ){ |
| 4597 | *pOutFlags = flags; |
| 4598 | } |
| 4599 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4600 | if( p->pUnused ){ |
| 4601 | p->pUnused->fd = fd; |
| 4602 | p->pUnused->flags = flags; |
| 4603 | } |
| 4604 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4605 | if( isDelete ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4606 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4607 | zPath = zName; |
| 4608 | #else |
danielk1977 | 17b90b5 | 2008-06-06 11:11:25 +0000 | [diff] [blame] | 4609 | unlink(zName); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4610 | #endif |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4611 | } |
drh | 4102264 | 2008-11-21 00:24:42 +0000 | [diff] [blame] | 4612 | #if SQLITE_ENABLE_LOCKING_STYLE |
| 4613 | else{ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4614 | p->openFlags = openFlags; |
drh | 08c6d44 | 2009-02-09 17:34:07 +0000 | [diff] [blame] | 4615 | } |
| 4616 | #endif |
| 4617 | |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4618 | if( isOpenDirectory ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4619 | rc = openDirectory(zPath, &dirfd); |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4620 | if( rc!=SQLITE_OK ){ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4621 | /* It is safe to close fd at this point, because it is guaranteed not |
| 4622 | ** to be open on a database file. If it were open on a database file, |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4623 | ** it would not be safe to close as this would release any locks held |
| 4624 | ** on the file by this process. */ |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4625 | assert( eType!=SQLITE_OPEN_MAIN_DB ); |
| 4626 | close(fd); /* silently leak if fail, already in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4627 | goto open_finished; |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4628 | } |
| 4629 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 4630 | |
| 4631 | #ifdef FD_CLOEXEC |
| 4632 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD, 0) | FD_CLOEXEC); |
| 4633 | #endif |
| 4634 | |
drh | da0e768 | 2008-07-30 15:27:54 +0000 | [diff] [blame] | 4635 | noLock = eType!=SQLITE_OPEN_MAIN_DB; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4636 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4637 | |
| 4638 | #if defined(__APPLE__) || SQLITE_ENABLE_LOCKING_STYLE |
| 4639 | struct statfs fsInfo; |
| 4640 | if( fstatfs(fd, &fsInfo) == -1 ){ |
| 4641 | ((unixFile*)pFile)->lastErrno = errno; |
| 4642 | if( dirfd>=0 ) close(dirfd); /* silently leak if fail, in error */ |
| 4643 | close(fd); /* silently leak if fail, in error */ |
| 4644 | return SQLITE_IOERR_ACCESS; |
| 4645 | } |
| 4646 | if (0 == strncmp("msdos", fsInfo.f_fstypename, 5)) { |
| 4647 | ((unixFile*)pFile)->fsFlags |= SQLITE_FSFLAGS_IS_MSDOS; |
| 4648 | } |
| 4649 | #endif |
| 4650 | |
| 4651 | #if SQLITE_ENABLE_LOCKING_STYLE |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4652 | #if SQLITE_PREFER_PROXY_LOCKING |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4653 | isAutoProxy = 1; |
| 4654 | #endif |
| 4655 | if( isAutoProxy && (zPath!=NULL) && (!noLock) && pVfs->xOpen ){ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4656 | char *envforce = getenv("SQLITE_FORCE_PROXY_LOCKING"); |
| 4657 | int useProxy = 0; |
| 4658 | |
dan | 08da86a | 2009-08-21 17:18:03 +0000 | [diff] [blame] | 4659 | /* SQLITE_FORCE_PROXY_LOCKING==1 means force always use proxy, 0 means |
| 4660 | ** never use proxy, NULL means use proxy for non-local files only. */ |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4661 | if( envforce!=NULL ){ |
| 4662 | useProxy = atoi(envforce)>0; |
| 4663 | }else{ |
| 4664 | struct statfs fsInfo; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4665 | if( statfs(zPath, &fsInfo) == -1 ){ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4666 | /* In theory, the close(fd) call is sub-optimal. If the file opened |
| 4667 | ** with fd is a database file, and there are other connections open |
| 4668 | ** on that file that are currently holding advisory locks on it, |
| 4669 | ** then the call to close() will cancel those locks. In practice, |
| 4670 | ** we're assuming that statfs() doesn't fail very often. At least |
| 4671 | ** not while other file descriptors opened by the same process on |
| 4672 | ** the same file are working. */ |
| 4673 | p->lastErrno = errno; |
| 4674 | if( dirfd>=0 ){ |
| 4675 | close(dirfd); /* silently leak if fail, in error */ |
| 4676 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4677 | close(fd); /* silently leak if fail, in error */ |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4678 | rc = SQLITE_IOERR_ACCESS; |
| 4679 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4680 | } |
| 4681 | useProxy = !(fsInfo.f_flags&MNT_LOCAL); |
| 4682 | } |
| 4683 | if( useProxy ){ |
| 4684 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4685 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 4686 | rc = proxyTransformUnixFile((unixFile*)pFile, ":auto:"); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 4687 | if( rc!=SQLITE_OK ){ |
| 4688 | /* Use unixClose to clean up the resources added in fillInUnixFile |
| 4689 | ** and clear all the structure's references. Specifically, |
| 4690 | ** pFile->pMethods will be NULL so sqlite3OsClose will be a no-op |
| 4691 | */ |
| 4692 | unixClose(pFile); |
| 4693 | return rc; |
| 4694 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4695 | } |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4696 | goto open_finished; |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4697 | } |
| 4698 | } |
| 4699 | #endif |
| 4700 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4701 | rc = fillInUnixFile(pVfs, fd, dirfd, pFile, zPath, noLock, isDelete); |
| 4702 | open_finished: |
| 4703 | if( rc!=SQLITE_OK ){ |
| 4704 | sqlite3_free(p->pUnused); |
| 4705 | } |
| 4706 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4707 | } |
| 4708 | |
dan | e946c39 | 2009-08-22 11:39:46 +0000 | [diff] [blame] | 4709 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4710 | /* |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4711 | ** Delete the file at zPath. If the dirSync argument is true, fsync() |
| 4712 | ** the directory after deleting the file. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4713 | */ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4714 | static int unixDelete( |
| 4715 | sqlite3_vfs *NotUsed, /* VFS containing this as the xDelete method */ |
| 4716 | const char *zPath, /* Name of file to be deleted */ |
| 4717 | int dirSync /* If true, fsync() directory after deleting file */ |
| 4718 | ){ |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4719 | int rc = SQLITE_OK; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4720 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4721 | SimulateIOError(return SQLITE_IOERR_DELETE); |
drh | 5d4feff | 2010-07-14 01:45:22 +0000 | [diff] [blame] | 4722 | if( unlink(zPath)==(-1) && errno!=ENOENT ){ |
| 4723 | return SQLITE_IOERR_DELETE; |
| 4724 | } |
danielk1977 | d39fa70 | 2008-10-16 13:27:40 +0000 | [diff] [blame] | 4725 | #ifndef SQLITE_DISABLE_DIRSYNC |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4726 | if( dirSync ){ |
| 4727 | int fd; |
| 4728 | rc = openDirectory(zPath, &fd); |
| 4729 | if( rc==SQLITE_OK ){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4730 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4731 | if( fsync(fd)==-1 ) |
| 4732 | #else |
| 4733 | if( fsync(fd) ) |
| 4734 | #endif |
| 4735 | { |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4736 | rc = SQLITE_IOERR_DIR_FSYNC; |
| 4737 | } |
aswift | aebf413 | 2008-11-21 00:10:35 +0000 | [diff] [blame] | 4738 | if( close(fd)&&!rc ){ |
| 4739 | rc = SQLITE_IOERR_DIR_CLOSE; |
| 4740 | } |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4741 | } |
| 4742 | } |
danielk1977 | d138dd8 | 2008-10-15 16:02:48 +0000 | [diff] [blame] | 4743 | #endif |
danielk1977 | fee2d25 | 2007-08-18 10:59:19 +0000 | [diff] [blame] | 4744 | return rc; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4745 | } |
| 4746 | |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4747 | /* |
| 4748 | ** Test the existance of or access permissions of file zPath. The |
| 4749 | ** test performed depends on the value of flags: |
| 4750 | ** |
| 4751 | ** SQLITE_ACCESS_EXISTS: Return 1 if the file exists |
| 4752 | ** SQLITE_ACCESS_READWRITE: Return 1 if the file is read and writable. |
| 4753 | ** SQLITE_ACCESS_READONLY: Return 1 if the file is readable. |
| 4754 | ** |
| 4755 | ** Otherwise return 0. |
| 4756 | */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4757 | static int unixAccess( |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4758 | sqlite3_vfs *NotUsed, /* The VFS containing this xAccess method */ |
| 4759 | const char *zPath, /* Path of the file to examine */ |
| 4760 | int flags, /* What do we want to learn about the zPath file? */ |
| 4761 | int *pResOut /* Write result boolean here */ |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4762 | ){ |
rse | 25c0d1a | 2007-09-20 08:38:14 +0000 | [diff] [blame] | 4763 | int amode = 0; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4764 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4765 | SimulateIOError( return SQLITE_IOERR_ACCESS; ); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4766 | switch( flags ){ |
| 4767 | case SQLITE_ACCESS_EXISTS: |
| 4768 | amode = F_OK; |
| 4769 | break; |
| 4770 | case SQLITE_ACCESS_READWRITE: |
| 4771 | amode = W_OK|R_OK; |
| 4772 | break; |
drh | 50d3f90 | 2007-08-27 21:10:36 +0000 | [diff] [blame] | 4773 | case SQLITE_ACCESS_READ: |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4774 | amode = R_OK; |
| 4775 | break; |
| 4776 | |
| 4777 | default: |
| 4778 | assert(!"Invalid flags argument"); |
| 4779 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4780 | *pResOut = (access(zPath, amode)==0); |
dan | 83acd42 | 2010-06-18 11:10:06 +0000 | [diff] [blame] | 4781 | if( flags==SQLITE_ACCESS_EXISTS && *pResOut ){ |
| 4782 | struct stat buf; |
| 4783 | if( 0==stat(zPath, &buf) && buf.st_size==0 ){ |
| 4784 | *pResOut = 0; |
| 4785 | } |
| 4786 | } |
danielk1977 | 861f745 | 2008-06-05 11:39:11 +0000 | [diff] [blame] | 4787 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4788 | } |
| 4789 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4790 | |
| 4791 | /* |
| 4792 | ** Turn a relative pathname into a full pathname. The relative path |
| 4793 | ** is stored as a nul-terminated string in the buffer pointed to by |
| 4794 | ** zPath. |
| 4795 | ** |
| 4796 | ** zOut points to a buffer of at least sqlite3_vfs.mxPathname bytes |
| 4797 | ** (in this case, MAX_PATHNAME bytes). The full-path is written to |
| 4798 | ** this buffer before returning. |
| 4799 | */ |
danielk1977 | adfb9b0 | 2007-09-17 07:02:56 +0000 | [diff] [blame] | 4800 | static int unixFullPathname( |
| 4801 | sqlite3_vfs *pVfs, /* Pointer to vfs object */ |
| 4802 | const char *zPath, /* Possibly relative input path */ |
| 4803 | int nOut, /* Size of output buffer in bytes */ |
| 4804 | char *zOut /* Output buffer */ |
| 4805 | ){ |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4806 | |
| 4807 | /* It's odd to simulate an io-error here, but really this is just |
| 4808 | ** using the io-error infrastructure to test that SQLite handles this |
| 4809 | ** function failing. This function could fail if, for example, the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4810 | ** current working directory has been unlinked. |
danielk1977 | 843e65f | 2007-09-01 16:16:15 +0000 | [diff] [blame] | 4811 | */ |
| 4812 | SimulateIOError( return SQLITE_ERROR ); |
| 4813 | |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4814 | assert( pVfs->mxPathname==MAX_PATHNAME ); |
danielk1977 | f3d3c27 | 2008-11-19 16:52:44 +0000 | [diff] [blame] | 4815 | UNUSED_PARAMETER(pVfs); |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4816 | |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4817 | zOut[nOut-1] = '\0'; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4818 | if( zPath[0]=='/' ){ |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4819 | sqlite3_snprintf(nOut, zOut, "%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4820 | }else{ |
| 4821 | int nCwd; |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4822 | if( getcwd(zOut, nOut-1)==0 ){ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 4823 | return SQLITE_CANTOPEN_BKPT; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4824 | } |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 4825 | nCwd = (int)strlen(zOut); |
drh | 3c7f2dc | 2007-12-06 13:26:20 +0000 | [diff] [blame] | 4826 | sqlite3_snprintf(nOut-nCwd, &zOut[nCwd], "/%s", zPath); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4827 | } |
| 4828 | return SQLITE_OK; |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
drh | 0ccebe7 | 2005-06-07 22:22:50 +0000 | [diff] [blame] | 4831 | |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4832 | #ifndef SQLITE_OMIT_LOAD_EXTENSION |
| 4833 | /* |
| 4834 | ** Interfaces for opening a shared library, finding entry points |
| 4835 | ** within the shared library, and closing the shared library. |
| 4836 | */ |
| 4837 | #include <dlfcn.h> |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4838 | static void *unixDlOpen(sqlite3_vfs *NotUsed, const char *zFilename){ |
| 4839 | UNUSED_PARAMETER(NotUsed); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4840 | return dlopen(zFilename, RTLD_NOW | RTLD_GLOBAL); |
| 4841 | } |
danielk1977 | 95c8a54 | 2007-09-01 06:51:27 +0000 | [diff] [blame] | 4842 | |
| 4843 | /* |
| 4844 | ** SQLite calls this function immediately after a call to unixDlSym() or |
| 4845 | ** unixDlOpen() fails (returns a null pointer). If a more detailed error |
| 4846 | ** message is available, it is written to zBufOut. If no error message |
| 4847 | ** is available, zBufOut is left unmodified and SQLite uses a default |
| 4848 | ** error message. |
| 4849 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4850 | static void unixDlError(sqlite3_vfs *NotUsed, int nBuf, char *zBufOut){ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4851 | char *zErr; |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4852 | UNUSED_PARAMETER(NotUsed); |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4853 | unixEnterMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4854 | zErr = dlerror(); |
| 4855 | if( zErr ){ |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 4856 | sqlite3_snprintf(nBuf, zBufOut, "%s", zErr); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4857 | } |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4858 | unixLeaveMutex(); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4859 | } |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4860 | static void (*unixDlSym(sqlite3_vfs *NotUsed, void *p, const char*zSym))(void){ |
| 4861 | /* |
| 4862 | ** GCC with -pedantic-errors says that C90 does not allow a void* to be |
| 4863 | ** cast into a pointer to a function. And yet the library dlsym() routine |
| 4864 | ** returns a void* which is really a pointer to a function. So how do we |
| 4865 | ** use dlsym() with -pedantic-errors? |
| 4866 | ** |
| 4867 | ** Variable x below is defined to be a pointer to a function taking |
| 4868 | ** parameters void* and const char* and returning a pointer to a function. |
| 4869 | ** We initialize x by assigning it a pointer to the dlsym() function. |
| 4870 | ** (That assignment requires a cast.) Then we call the function that |
| 4871 | ** x points to. |
| 4872 | ** |
| 4873 | ** This work-around is unlikely to work correctly on any system where |
| 4874 | ** you really cannot cast a function pointer into void*. But then, on the |
| 4875 | ** other hand, dlsym() will not work on such a system either, so we have |
| 4876 | ** not really lost anything. |
| 4877 | */ |
| 4878 | void (*(*x)(void*,const char*))(void); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4879 | UNUSED_PARAMETER(NotUsed); |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 4880 | x = (void(*(*)(void*,const char*))(void))dlsym; |
| 4881 | return (*x)(p, zSym); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4882 | } |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4883 | static void unixDlClose(sqlite3_vfs *NotUsed, void *pHandle){ |
| 4884 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4885 | dlclose(pHandle); |
drh | 761df87 | 2006-12-21 01:29:22 +0000 | [diff] [blame] | 4886 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4887 | #else /* if SQLITE_OMIT_LOAD_EXTENSION is defined: */ |
| 4888 | #define unixDlOpen 0 |
| 4889 | #define unixDlError 0 |
| 4890 | #define unixDlSym 0 |
| 4891 | #define unixDlClose 0 |
| 4892 | #endif |
| 4893 | |
| 4894 | /* |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4895 | ** Write nBuf bytes of random data to the supplied buffer zBuf. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4896 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4897 | static int unixRandomness(sqlite3_vfs *NotUsed, int nBuf, char *zBuf){ |
| 4898 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4899 | assert((size_t)nBuf>=(sizeof(time_t)+sizeof(int))); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4900 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4901 | /* We have to initialize zBuf to prevent valgrind from reporting |
| 4902 | ** errors. The reports issued by valgrind are incorrect - we would |
| 4903 | ** prefer that the randomness be increased by making use of the |
| 4904 | ** uninitialized space in zBuf - but valgrind errors tend to worry |
| 4905 | ** some users. Rather than argue, it seems easier just to initialize |
| 4906 | ** the whole array and silence valgrind, even if that means less randomness |
| 4907 | ** in the random seed. |
| 4908 | ** |
| 4909 | ** When testing, initializing zBuf[] to zero is all we do. That means |
drh | f1a221e | 2006-01-15 17:27:17 +0000 | [diff] [blame] | 4910 | ** that we always use the same random number sequence. This makes the |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4911 | ** tests repeatable. |
| 4912 | */ |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4913 | memset(zBuf, 0, nBuf); |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4914 | #if !defined(SQLITE_TEST) |
| 4915 | { |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4916 | int pid, fd; |
| 4917 | fd = open("/dev/urandom", O_RDONLY); |
| 4918 | if( fd<0 ){ |
drh | 0739723 | 2006-01-06 14:46:46 +0000 | [diff] [blame] | 4919 | time_t t; |
| 4920 | time(&t); |
danielk1977 | 90949c2 | 2007-08-17 16:50:38 +0000 | [diff] [blame] | 4921 | memcpy(zBuf, &t, sizeof(t)); |
| 4922 | pid = getpid(); |
| 4923 | memcpy(&zBuf[sizeof(t)], &pid, sizeof(pid)); |
danielk1977 | 00e1361 | 2008-11-17 19:18:54 +0000 | [diff] [blame] | 4924 | assert( sizeof(t)+sizeof(pid)<=(size_t)nBuf ); |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4925 | nBuf = sizeof(t) + sizeof(pid); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4926 | }else{ |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4927 | nBuf = read(fd, zBuf, nBuf); |
drh | 842b864 | 2005-01-21 17:53:17 +0000 | [diff] [blame] | 4928 | close(fd); |
| 4929 | } |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4930 | } |
| 4931 | #endif |
drh | 72cbd07 | 2008-10-14 17:58:38 +0000 | [diff] [blame] | 4932 | return nBuf; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4933 | } |
| 4934 | |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4935 | |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4936 | /* |
| 4937 | ** Sleep for a little while. Return the amount of time slept. |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4938 | ** The argument is the number of microseconds we want to sleep. |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4939 | ** The return value is the number of microseconds of sleep actually |
| 4940 | ** requested from the underlying operating system, a number which |
| 4941 | ** might be greater than or equal to the argument, but not less |
| 4942 | ** than the argument. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4943 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4944 | static int unixSleep(sqlite3_vfs *NotUsed, int microseconds){ |
drh | 6c7d5c5 | 2008-11-21 20:32:33 +0000 | [diff] [blame] | 4945 | #if OS_VXWORKS |
chw | 9718548 | 2008-11-17 08:05:31 +0000 | [diff] [blame] | 4946 | struct timespec sp; |
| 4947 | |
| 4948 | sp.tv_sec = microseconds / 1000000; |
| 4949 | sp.tv_nsec = (microseconds % 1000000) * 1000; |
| 4950 | nanosleep(&sp, NULL); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4951 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 4952 | return microseconds; |
| 4953 | #elif defined(HAVE_USLEEP) && HAVE_USLEEP |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4954 | usleep(microseconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4955 | UNUSED_PARAMETER(NotUsed); |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4956 | return microseconds; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4957 | #else |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 4958 | int seconds = (microseconds+999999)/1000000; |
| 4959 | sleep(seconds); |
drh | d43fe20 | 2009-03-01 22:29:20 +0000 | [diff] [blame] | 4960 | UNUSED_PARAMETER(NotUsed); |
drh | 4a50aac | 2007-08-23 02:47:53 +0000 | [diff] [blame] | 4961 | return seconds*1000000; |
drh | a3fad6f | 2006-01-18 14:06:37 +0000 | [diff] [blame] | 4962 | #endif |
drh | 88f474a | 2006-01-02 20:00:12 +0000 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4966 | ** The following variable, if set to a non-zero value, is interpreted as |
| 4967 | ** the number of seconds since 1970 and is used to set the result of |
| 4968 | ** sqlite3OsCurrentTime() during testing. |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4969 | */ |
| 4970 | #ifdef SQLITE_TEST |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 4971 | int sqlite3_current_time = 0; /* Fake system time in seconds since 1970. */ |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 4972 | #endif |
| 4973 | |
| 4974 | /* |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 4975 | ** Find the current time (in Universal Coordinated Time). Write into *piNow |
| 4976 | ** the current time and date as a Julian Day number times 86_400_000. In |
| 4977 | ** other words, write into *piNow the number of milliseconds since the Julian |
| 4978 | ** epoch of noon in Greenwich on November 24, 4714 B.C according to the |
| 4979 | ** proleptic Gregorian calendar. |
| 4980 | ** |
| 4981 | ** On success, return 0. Return 1 if the time and date cannot be found. |
| 4982 | */ |
| 4983 | static int unixCurrentTimeInt64(sqlite3_vfs *NotUsed, sqlite3_int64 *piNow){ |
| 4984 | static const sqlite3_int64 unixEpoch = 24405875*(sqlite3_int64)8640000; |
| 4985 | #if defined(NO_GETTOD) |
| 4986 | time_t t; |
| 4987 | time(&t); |
| 4988 | *piNow = ((sqlite3_int64)i)*1000 + unixEpoch; |
| 4989 | #elif OS_VXWORKS |
| 4990 | struct timespec sNow; |
| 4991 | clock_gettime(CLOCK_REALTIME, &sNow); |
| 4992 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_nsec/1000000; |
| 4993 | #else |
| 4994 | struct timeval sNow; |
| 4995 | gettimeofday(&sNow, 0); |
| 4996 | *piNow = unixEpoch + 1000*(sqlite3_int64)sNow.tv_sec + sNow.tv_usec/1000; |
| 4997 | #endif |
| 4998 | |
| 4999 | #ifdef SQLITE_TEST |
| 5000 | if( sqlite3_current_time ){ |
| 5001 | *piNow = 1000*(sqlite3_int64)sqlite3_current_time + unixEpoch; |
| 5002 | } |
| 5003 | #endif |
| 5004 | UNUSED_PARAMETER(NotUsed); |
| 5005 | return 0; |
| 5006 | } |
| 5007 | |
| 5008 | /* |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5009 | ** Find the current time (in Universal Coordinated Time). Write the |
| 5010 | ** current time and date as a Julian Day number into *prNow and |
| 5011 | ** return 0. Return 1 if the time and date cannot be found. |
| 5012 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5013 | static int unixCurrentTime(sqlite3_vfs *NotUsed, double *prNow){ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5014 | sqlite3_int64 i; |
drh | ff82894 | 2010-06-26 21:34:06 +0000 | [diff] [blame] | 5015 | UNUSED_PARAMETER(NotUsed); |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 5016 | unixCurrentTimeInt64(0, &i); |
drh | 0dcb0a7 | 2010-05-03 18:22:52 +0000 | [diff] [blame] | 5017 | *prNow = i/86400000.0; |
drh | bbd42a6 | 2004-05-22 17:41:58 +0000 | [diff] [blame] | 5018 | return 0; |
| 5019 | } |
danielk1977 | b4b4741 | 2007-08-17 15:53:36 +0000 | [diff] [blame] | 5020 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 5021 | /* |
| 5022 | ** We added the xGetLastError() method with the intention of providing |
| 5023 | ** better low-level error messages when operating-system problems come up |
| 5024 | ** during SQLite operation. But so far, none of that has been implemented |
| 5025 | ** in the core. So this routine is never called. For now, it is merely |
| 5026 | ** a place-holder. |
| 5027 | */ |
danielk1977 | 397d65f | 2008-11-19 11:35:39 +0000 | [diff] [blame] | 5028 | static int unixGetLastError(sqlite3_vfs *NotUsed, int NotUsed2, char *NotUsed3){ |
| 5029 | UNUSED_PARAMETER(NotUsed); |
| 5030 | UNUSED_PARAMETER(NotUsed2); |
| 5031 | UNUSED_PARAMETER(NotUsed3); |
danielk1977 | bcb97fe | 2008-06-06 15:49:29 +0000 | [diff] [blame] | 5032 | return 0; |
| 5033 | } |
| 5034 | |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 5035 | |
| 5036 | /* |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 5037 | ************************ End of sqlite3_vfs methods *************************** |
| 5038 | ******************************************************************************/ |
| 5039 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5040 | /****************************************************************************** |
| 5041 | ************************** Begin Proxy Locking ******************************** |
| 5042 | ** |
| 5043 | ** Proxy locking is a "uber-locking-method" in this sense: It uses the |
| 5044 | ** other locking methods on secondary lock files. Proxy locking is a |
| 5045 | ** meta-layer over top of the primitive locking implemented above. For |
| 5046 | ** this reason, the division that implements of proxy locking is deferred |
| 5047 | ** until late in the file (here) after all of the other I/O methods have |
| 5048 | ** been defined - so that the primitive locking methods are available |
| 5049 | ** as services to help with the implementation of proxy locking. |
| 5050 | ** |
| 5051 | **** |
| 5052 | ** |
| 5053 | ** The default locking schemes in SQLite use byte-range locks on the |
| 5054 | ** database file to coordinate safe, concurrent access by multiple readers |
| 5055 | ** and writers [http://sqlite.org/lockingv3.html]. The five file locking |
| 5056 | ** states (UNLOCKED, PENDING, SHARED, RESERVED, EXCLUSIVE) are implemented |
| 5057 | ** as POSIX read & write locks over fixed set of locations (via fsctl), |
| 5058 | ** on AFP and SMB only exclusive byte-range locks are available via fsctl |
| 5059 | ** with _IOWR('z', 23, struct ByteRangeLockPB2) to track the same 5 states. |
| 5060 | ** To simulate a F_RDLCK on the shared range, on AFP a randomly selected |
| 5061 | ** address in the shared range is taken for a SHARED lock, the entire |
| 5062 | ** shared range is taken for an EXCLUSIVE lock): |
| 5063 | ** |
| 5064 | ** PENDING_BYTE 0x40000000 |
| 5065 | ** RESERVED_BYTE 0x40000001 |
| 5066 | ** SHARED_RANGE 0x40000002 -> 0x40000200 |
| 5067 | ** |
| 5068 | ** This works well on the local file system, but shows a nearly 100x |
| 5069 | ** slowdown in read performance on AFP because the AFP client disables |
| 5070 | ** the read cache when byte-range locks are present. Enabling the read |
| 5071 | ** cache exposes a cache coherency problem that is present on all OS X |
| 5072 | ** supported network file systems. NFS and AFP both observe the |
| 5073 | ** close-to-open semantics for ensuring cache coherency |
| 5074 | ** [http://nfs.sourceforge.net/#faq_a8], which does not effectively |
| 5075 | ** address the requirements for concurrent database access by multiple |
| 5076 | ** readers and writers |
| 5077 | ** [http://www.nabble.com/SQLite-on-NFS-cache-coherency-td15655701.html]. |
| 5078 | ** |
| 5079 | ** To address the performance and cache coherency issues, proxy file locking |
| 5080 | ** changes the way database access is controlled by limiting access to a |
| 5081 | ** single host at a time and moving file locks off of the database file |
| 5082 | ** and onto a proxy file on the local file system. |
| 5083 | ** |
| 5084 | ** |
| 5085 | ** Using proxy locks |
| 5086 | ** ----------------- |
| 5087 | ** |
| 5088 | ** C APIs |
| 5089 | ** |
| 5090 | ** sqlite3_file_control(db, dbname, SQLITE_SET_LOCKPROXYFILE, |
| 5091 | ** <proxy_path> | ":auto:"); |
| 5092 | ** sqlite3_file_control(db, dbname, SQLITE_GET_LOCKPROXYFILE, &<proxy_path>); |
| 5093 | ** |
| 5094 | ** |
| 5095 | ** SQL pragmas |
| 5096 | ** |
| 5097 | ** PRAGMA [database.]lock_proxy_file=<proxy_path> | :auto: |
| 5098 | ** PRAGMA [database.]lock_proxy_file |
| 5099 | ** |
| 5100 | ** Specifying ":auto:" means that if there is a conch file with a matching |
| 5101 | ** host ID in it, the proxy path in the conch file will be used, otherwise |
| 5102 | ** a proxy path based on the user's temp dir |
| 5103 | ** (via confstr(_CS_DARWIN_USER_TEMP_DIR,...)) will be used and the |
| 5104 | ** actual proxy file name is generated from the name and path of the |
| 5105 | ** database file. For example: |
| 5106 | ** |
| 5107 | ** For database path "/Users/me/foo.db" |
| 5108 | ** The lock path will be "<tmpdir>/sqliteplocks/_Users_me_foo.db:auto:") |
| 5109 | ** |
| 5110 | ** Once a lock proxy is configured for a database connection, it can not |
| 5111 | ** be removed, however it may be switched to a different proxy path via |
| 5112 | ** the above APIs (assuming the conch file is not being held by another |
| 5113 | ** connection or process). |
| 5114 | ** |
| 5115 | ** |
| 5116 | ** How proxy locking works |
| 5117 | ** ----------------------- |
| 5118 | ** |
| 5119 | ** Proxy file locking relies primarily on two new supporting files: |
| 5120 | ** |
| 5121 | ** * conch file to limit access to the database file to a single host |
| 5122 | ** at a time |
| 5123 | ** |
| 5124 | ** * proxy file to act as a proxy for the advisory locks normally |
| 5125 | ** taken on the database |
| 5126 | ** |
| 5127 | ** The conch file - to use a proxy file, sqlite must first "hold the conch" |
| 5128 | ** by taking an sqlite-style shared lock on the conch file, reading the |
| 5129 | ** contents and comparing the host's unique host ID (see below) and lock |
| 5130 | ** proxy path against the values stored in the conch. The conch file is |
| 5131 | ** stored in the same directory as the database file and the file name |
| 5132 | ** is patterned after the database file name as ".<databasename>-conch". |
| 5133 | ** If the conch file does not exist, or it's contents do not match the |
| 5134 | ** host ID and/or proxy path, then the lock is escalated to an exclusive |
| 5135 | ** lock and the conch file contents is updated with the host ID and proxy |
| 5136 | ** path and the lock is downgraded to a shared lock again. If the conch |
| 5137 | ** is held by another process (with a shared lock), the exclusive lock |
| 5138 | ** will fail and SQLITE_BUSY is returned. |
| 5139 | ** |
| 5140 | ** The proxy file - a single-byte file used for all advisory file locks |
| 5141 | ** normally taken on the database file. This allows for safe sharing |
| 5142 | ** of the database file for multiple readers and writers on the same |
| 5143 | ** host (the conch ensures that they all use the same local lock file). |
| 5144 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5145 | ** Requesting the lock proxy does not immediately take the conch, it is |
| 5146 | ** only taken when the first request to lock database file is made. |
| 5147 | ** This matches the semantics of the traditional locking behavior, where |
| 5148 | ** opening a connection to a database file does not take a lock on it. |
| 5149 | ** The shared lock and an open file descriptor are maintained until |
| 5150 | ** the connection to the database is closed. |
| 5151 | ** |
| 5152 | ** The proxy file and the lock file are never deleted so they only need |
| 5153 | ** to be created the first time they are used. |
| 5154 | ** |
| 5155 | ** Configuration options |
| 5156 | ** --------------------- |
| 5157 | ** |
| 5158 | ** SQLITE_PREFER_PROXY_LOCKING |
| 5159 | ** |
| 5160 | ** Database files accessed on non-local file systems are |
| 5161 | ** automatically configured for proxy locking, lock files are |
| 5162 | ** named automatically using the same logic as |
| 5163 | ** PRAGMA lock_proxy_file=":auto:" |
| 5164 | ** |
| 5165 | ** SQLITE_PROXY_DEBUG |
| 5166 | ** |
| 5167 | ** Enables the logging of error messages during host id file |
| 5168 | ** retrieval and creation |
| 5169 | ** |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5170 | ** LOCKPROXYDIR |
| 5171 | ** |
| 5172 | ** Overrides the default directory used for lock proxy files that |
| 5173 | ** are named automatically via the ":auto:" setting |
| 5174 | ** |
| 5175 | ** SQLITE_DEFAULT_PROXYDIR_PERMISSIONS |
| 5176 | ** |
| 5177 | ** Permissions to use when creating a directory for storing the |
| 5178 | ** lock proxy files, only used when LOCKPROXYDIR is not set. |
| 5179 | ** |
| 5180 | ** |
| 5181 | ** As mentioned above, when compiled with SQLITE_PREFER_PROXY_LOCKING, |
| 5182 | ** setting the environment variable SQLITE_FORCE_PROXY_LOCKING to 1 will |
| 5183 | ** force proxy locking to be used for every database file opened, and 0 |
| 5184 | ** will force automatic proxy locking to be disabled for all database |
| 5185 | ** files (explicity calling the SQLITE_SET_LOCKPROXYFILE pragma or |
| 5186 | ** sqlite_file_control API is not affected by SQLITE_FORCE_PROXY_LOCKING). |
| 5187 | */ |
| 5188 | |
| 5189 | /* |
| 5190 | ** Proxy locking is only available on MacOSX |
| 5191 | */ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5192 | #if defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5193 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5194 | /* |
| 5195 | ** The proxyLockingContext has the path and file structures for the remote |
| 5196 | ** and local proxy files in it |
| 5197 | */ |
| 5198 | typedef struct proxyLockingContext proxyLockingContext; |
| 5199 | struct proxyLockingContext { |
| 5200 | unixFile *conchFile; /* Open conch file */ |
| 5201 | char *conchFilePath; /* Name of the conch file */ |
| 5202 | unixFile *lockProxy; /* Open proxy lock file */ |
| 5203 | char *lockProxyPath; /* Name of the proxy lock file */ |
| 5204 | char *dbPath; /* Name of the open file */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5205 | int conchHeld; /* 1 if the conch is held, -1 if lockless */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5206 | void *oldLockingContext; /* Original lockingcontext to restore on close */ |
| 5207 | sqlite3_io_methods const *pOldMethod; /* Original I/O methods for close */ |
| 5208 | }; |
| 5209 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5210 | /* |
| 5211 | ** The proxy lock file path for the database at dbPath is written into lPath, |
| 5212 | ** which must point to valid, writable memory large enough for a maxLen length |
| 5213 | ** file path. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5214 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5215 | static int proxyGetLockPath(const char *dbPath, char *lPath, size_t maxLen){ |
| 5216 | int len; |
| 5217 | int dbLen; |
| 5218 | int i; |
| 5219 | |
| 5220 | #ifdef LOCKPROXYDIR |
| 5221 | len = strlcpy(lPath, LOCKPROXYDIR, maxLen); |
| 5222 | #else |
| 5223 | # ifdef _CS_DARWIN_USER_TEMP_DIR |
| 5224 | { |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5225 | if( !confstr(_CS_DARWIN_USER_TEMP_DIR, lPath, maxLen) ){ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5226 | OSTRACE(("GETLOCKPATH failed %s errno=%d pid=%d\n", |
| 5227 | lPath, errno, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5228 | return SQLITE_IOERR_LOCK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5229 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5230 | len = strlcat(lPath, "sqliteplocks", maxLen); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5231 | } |
| 5232 | # else |
| 5233 | len = strlcpy(lPath, "/tmp/", maxLen); |
| 5234 | # endif |
| 5235 | #endif |
| 5236 | |
| 5237 | if( lPath[len-1]!='/' ){ |
| 5238 | len = strlcat(lPath, "/", maxLen); |
| 5239 | } |
| 5240 | |
| 5241 | /* transform the db path to a unique cache name */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5242 | dbLen = (int)strlen(dbPath); |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5243 | for( i=0; i<dbLen && (i+len+7)<(int)maxLen; i++){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5244 | char c = dbPath[i]; |
| 5245 | lPath[i+len] = (c=='/')?'_':c; |
| 5246 | } |
| 5247 | lPath[i+len]='\0'; |
| 5248 | strlcat(lPath, ":auto:", maxLen); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5249 | OSTRACE(("GETLOCKPATH proxy lock path=%s pid=%d\n", lPath, getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5250 | return SQLITE_OK; |
| 5251 | } |
| 5252 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5253 | /* |
| 5254 | ** Creates the lock file and any missing directories in lockPath |
| 5255 | */ |
| 5256 | static int proxyCreateLockPath(const char *lockPath){ |
| 5257 | int i, len; |
| 5258 | char buf[MAXPATHLEN]; |
| 5259 | int start = 0; |
| 5260 | |
| 5261 | assert(lockPath!=NULL); |
| 5262 | /* try to create all the intermediate directories */ |
| 5263 | len = (int)strlen(lockPath); |
| 5264 | buf[0] = lockPath[0]; |
| 5265 | for( i=1; i<len; i++ ){ |
| 5266 | if( lockPath[i] == '/' && (i - start > 0) ){ |
| 5267 | /* only mkdir if leaf dir != "." or "/" or ".." */ |
| 5268 | if( i-start>2 || (i-start==1 && buf[start] != '.' && buf[start] != '/') |
| 5269 | || (i-start==2 && buf[start] != '.' && buf[start+1] != '.') ){ |
| 5270 | buf[i]='\0'; |
| 5271 | if( mkdir(buf, SQLITE_DEFAULT_PROXYDIR_PERMISSIONS) ){ |
| 5272 | int err=errno; |
| 5273 | if( err!=EEXIST ) { |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5274 | OSTRACE(("CREATELOCKPATH FAILED creating %s, " |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5275 | "'%s' proxy lock path=%s pid=%d\n", |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5276 | buf, strerror(err), lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5277 | return err; |
| 5278 | } |
| 5279 | } |
| 5280 | } |
| 5281 | start=i+1; |
| 5282 | } |
| 5283 | buf[i] = lockPath[i]; |
| 5284 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5285 | OSTRACE(("CREATELOCKPATH proxy lock path=%s pid=%d\n", lockPath, getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5286 | return 0; |
| 5287 | } |
| 5288 | |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5289 | /* |
| 5290 | ** Create a new VFS file descriptor (stored in memory obtained from |
| 5291 | ** sqlite3_malloc) and open the file named "path" in the file descriptor. |
| 5292 | ** |
| 5293 | ** The caller is responsible not only for closing the file descriptor |
| 5294 | ** but also for freeing the memory associated with the file descriptor. |
| 5295 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5296 | static int proxyCreateUnixFile( |
| 5297 | const char *path, /* path for the new unixFile */ |
| 5298 | unixFile **ppFile, /* unixFile created and returned by ref */ |
| 5299 | int islockfile /* if non zero missing dirs will be created */ |
| 5300 | ) { |
| 5301 | int fd = -1; |
| 5302 | int dirfd = -1; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5303 | unixFile *pNew; |
| 5304 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5305 | int openFlags = O_RDWR | O_CREAT; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5306 | sqlite3_vfs dummyVfs; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5307 | int terrno = 0; |
| 5308 | UnixUnusedFd *pUnused = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5309 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5310 | /* 1. first try to open/create the file |
| 5311 | ** 2. if that fails, and this is a lock file (not-conch), try creating |
| 5312 | ** the parent directories and then try again. |
| 5313 | ** 3. if that fails, try to open the file read-only |
| 5314 | ** otherwise return BUSY (if lock file) or CANTOPEN for the conch file |
| 5315 | */ |
| 5316 | pUnused = findReusableFd(path, openFlags); |
| 5317 | if( pUnused ){ |
| 5318 | fd = pUnused->fd; |
| 5319 | }else{ |
| 5320 | pUnused = sqlite3_malloc(sizeof(*pUnused)); |
| 5321 | if( !pUnused ){ |
| 5322 | return SQLITE_NOMEM; |
| 5323 | } |
| 5324 | } |
| 5325 | if( fd<0 ){ |
| 5326 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5327 | terrno = errno; |
| 5328 | if( fd<0 && errno==ENOENT && islockfile ){ |
| 5329 | if( proxyCreateLockPath(path) == SQLITE_OK ){ |
| 5330 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5331 | } |
| 5332 | } |
| 5333 | } |
| 5334 | if( fd<0 ){ |
| 5335 | openFlags = O_RDONLY; |
| 5336 | fd = open(path, openFlags, SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5337 | terrno = errno; |
| 5338 | } |
| 5339 | if( fd<0 ){ |
| 5340 | if( islockfile ){ |
| 5341 | return SQLITE_BUSY; |
| 5342 | } |
| 5343 | switch (terrno) { |
| 5344 | case EACCES: |
| 5345 | return SQLITE_PERM; |
| 5346 | case EIO: |
| 5347 | return SQLITE_IOERR_LOCK; /* even though it is the conch */ |
| 5348 | default: |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5349 | return SQLITE_CANTOPEN_BKPT; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5350 | } |
| 5351 | } |
| 5352 | |
| 5353 | pNew = (unixFile *)sqlite3_malloc(sizeof(*pNew)); |
| 5354 | if( pNew==NULL ){ |
| 5355 | rc = SQLITE_NOMEM; |
| 5356 | goto end_create_proxy; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5357 | } |
| 5358 | memset(pNew, 0, sizeof(unixFile)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5359 | pNew->openFlags = openFlags; |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 5360 | dummyVfs.pAppData = (void*)&autolockIoFinder; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5361 | pUnused->fd = fd; |
| 5362 | pUnused->flags = openFlags; |
| 5363 | pNew->pUnused = pUnused; |
| 5364 | |
| 5365 | rc = fillInUnixFile(&dummyVfs, fd, dirfd, (sqlite3_file*)pNew, path, 0, 0); |
| 5366 | if( rc==SQLITE_OK ){ |
| 5367 | *ppFile = pNew; |
| 5368 | return SQLITE_OK; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5369 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5370 | end_create_proxy: |
| 5371 | close(fd); /* silently leak fd if error, we're already in error */ |
| 5372 | sqlite3_free(pNew); |
| 5373 | sqlite3_free(pUnused); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5374 | return rc; |
| 5375 | } |
| 5376 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5377 | #ifdef SQLITE_TEST |
| 5378 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5379 | int sqlite3_hostid_num = 0; |
| 5380 | #endif |
| 5381 | |
| 5382 | #define PROXY_HOSTIDLEN 16 /* conch file host id length */ |
| 5383 | |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5384 | /* Not always defined in the headers as it ought to be */ |
| 5385 | extern int gethostuuid(uuid_t id, const struct timespec *wait); |
| 5386 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5387 | /* get the host ID via gethostuuid(), pHostID must point to PROXY_HOSTIDLEN |
| 5388 | ** bytes of writable memory. |
| 5389 | */ |
| 5390 | static int proxyGetHostID(unsigned char *pHostID, int *pError){ |
| 5391 | struct timespec timeout = {1, 0}; /* 1 sec timeout */ |
| 5392 | |
| 5393 | assert(PROXY_HOSTIDLEN == sizeof(uuid_t)); |
| 5394 | memset(pHostID, 0, PROXY_HOSTIDLEN); |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5395 | #if defined(__MAX_OS_X_VERSION_MIN_REQUIRED)\ |
| 5396 | && __MAC_OS_X_VERSION_MIN_REQUIRED<1050 |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5397 | if( gethostuuid(pHostID, &timeout) ){ |
| 5398 | int err = errno; |
| 5399 | if( pError ){ |
| 5400 | *pError = err; |
| 5401 | } |
| 5402 | return SQLITE_IOERR; |
| 5403 | } |
drh | e8b0c9b | 2010-09-25 14:13:17 +0000 | [diff] [blame] | 5404 | #endif |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5405 | #ifdef SQLITE_TEST |
| 5406 | /* simulate multiple hosts by creating unique hostid file paths */ |
| 5407 | if( sqlite3_hostid_num != 0){ |
| 5408 | pHostID[0] = (char)(pHostID[0] + (char)(sqlite3_hostid_num & 0xFF)); |
| 5409 | } |
| 5410 | #endif |
| 5411 | |
| 5412 | return SQLITE_OK; |
| 5413 | } |
| 5414 | |
| 5415 | /* The conch file contains the header, host id and lock file path |
| 5416 | */ |
| 5417 | #define PROXY_CONCHVERSION 2 /* 1-byte header, 16-byte host id, path */ |
| 5418 | #define PROXY_HEADERLEN 1 /* conch file header length */ |
| 5419 | #define PROXY_PATHINDEX (PROXY_HEADERLEN+PROXY_HOSTIDLEN) |
| 5420 | #define PROXY_MAXCONCHLEN (PROXY_HEADERLEN+PROXY_HOSTIDLEN+MAXPATHLEN) |
| 5421 | |
| 5422 | /* |
| 5423 | ** Takes an open conch file, copies the contents to a new path and then moves |
| 5424 | ** it back. The newly created file's file descriptor is assigned to the |
| 5425 | ** conch file structure and finally the original conch file descriptor is |
| 5426 | ** closed. Returns zero if successful. |
| 5427 | */ |
| 5428 | static int proxyBreakConchLock(unixFile *pFile, uuid_t myHostID){ |
| 5429 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5430 | unixFile *conchFile = pCtx->conchFile; |
| 5431 | char tPath[MAXPATHLEN]; |
| 5432 | char buf[PROXY_MAXCONCHLEN]; |
| 5433 | char *cPath = pCtx->conchFilePath; |
| 5434 | size_t readLen = 0; |
| 5435 | size_t pathLen = 0; |
| 5436 | char errmsg[64] = ""; |
| 5437 | int fd = -1; |
| 5438 | int rc = -1; |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5439 | UNUSED_PARAMETER(myHostID); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5440 | |
| 5441 | /* create a new path by replace the trailing '-conch' with '-break' */ |
| 5442 | pathLen = strlcpy(tPath, cPath, MAXPATHLEN); |
| 5443 | if( pathLen>MAXPATHLEN || pathLen<6 || |
| 5444 | (strlcpy(&tPath[pathLen-5], "break", 6) != 5) ){ |
| 5445 | sprintf(errmsg, "path error (len %d)", (int)pathLen); |
| 5446 | goto end_breaklock; |
| 5447 | } |
| 5448 | /* read the conch content */ |
| 5449 | readLen = pread(conchFile->h, buf, PROXY_MAXCONCHLEN, 0); |
| 5450 | if( readLen<PROXY_PATHINDEX ){ |
| 5451 | sprintf(errmsg, "read error (len %d)", (int)readLen); |
| 5452 | goto end_breaklock; |
| 5453 | } |
| 5454 | /* write it out to the temporary break file */ |
| 5455 | fd = open(tPath, (O_RDWR|O_CREAT|O_EXCL), SQLITE_DEFAULT_FILE_PERMISSIONS); |
| 5456 | if( fd<0 ){ |
| 5457 | sprintf(errmsg, "create failed (%d)", errno); |
| 5458 | goto end_breaklock; |
| 5459 | } |
drh | 0ab216a | 2010-07-02 17:10:40 +0000 | [diff] [blame] | 5460 | if( pwrite(fd, buf, readLen, 0) != (ssize_t)readLen ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5461 | sprintf(errmsg, "write failed (%d)", errno); |
| 5462 | goto end_breaklock; |
| 5463 | } |
| 5464 | if( rename(tPath, cPath) ){ |
| 5465 | sprintf(errmsg, "rename failed (%d)", errno); |
| 5466 | goto end_breaklock; |
| 5467 | } |
| 5468 | rc = 0; |
| 5469 | fprintf(stderr, "broke stale lock on %s\n", cPath); |
| 5470 | close(conchFile->h); |
| 5471 | conchFile->h = fd; |
| 5472 | conchFile->openFlags = O_RDWR | O_CREAT; |
| 5473 | |
| 5474 | end_breaklock: |
| 5475 | if( rc ){ |
| 5476 | if( fd>=0 ){ |
| 5477 | unlink(tPath); |
| 5478 | close(fd); |
| 5479 | } |
| 5480 | fprintf(stderr, "failed to break stale lock on %s, %s\n", cPath, errmsg); |
| 5481 | } |
| 5482 | return rc; |
| 5483 | } |
| 5484 | |
| 5485 | /* Take the requested lock on the conch file and break a stale lock if the |
| 5486 | ** host id matches. |
| 5487 | */ |
| 5488 | static int proxyConchLock(unixFile *pFile, uuid_t myHostID, int lockType){ |
| 5489 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5490 | unixFile *conchFile = pCtx->conchFile; |
| 5491 | int rc = SQLITE_OK; |
| 5492 | int nTries = 0; |
| 5493 | struct timespec conchModTime; |
| 5494 | |
| 5495 | do { |
| 5496 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5497 | nTries ++; |
| 5498 | if( rc==SQLITE_BUSY ){ |
| 5499 | /* If the lock failed (busy): |
| 5500 | * 1st try: get the mod time of the conch, wait 0.5s and try again. |
| 5501 | * 2nd try: fail if the mod time changed or host id is different, wait |
| 5502 | * 10 sec and try again |
| 5503 | * 3rd try: break the lock unless the mod time has changed. |
| 5504 | */ |
| 5505 | struct stat buf; |
| 5506 | if( fstat(conchFile->h, &buf) ){ |
| 5507 | pFile->lastErrno = errno; |
| 5508 | return SQLITE_IOERR_LOCK; |
| 5509 | } |
| 5510 | |
| 5511 | if( nTries==1 ){ |
| 5512 | conchModTime = buf.st_mtimespec; |
| 5513 | usleep(500000); /* wait 0.5 sec and try the lock again*/ |
| 5514 | continue; |
| 5515 | } |
| 5516 | |
| 5517 | assert( nTries>1 ); |
| 5518 | if( conchModTime.tv_sec != buf.st_mtimespec.tv_sec || |
| 5519 | conchModTime.tv_nsec != buf.st_mtimespec.tv_nsec ){ |
| 5520 | return SQLITE_BUSY; |
| 5521 | } |
| 5522 | |
| 5523 | if( nTries==2 ){ |
| 5524 | char tBuf[PROXY_MAXCONCHLEN]; |
| 5525 | int len = pread(conchFile->h, tBuf, PROXY_MAXCONCHLEN, 0); |
| 5526 | if( len<0 ){ |
| 5527 | pFile->lastErrno = errno; |
| 5528 | return SQLITE_IOERR_LOCK; |
| 5529 | } |
| 5530 | if( len>PROXY_PATHINDEX && tBuf[0]==(char)PROXY_CONCHVERSION){ |
| 5531 | /* don't break the lock if the host id doesn't match */ |
| 5532 | if( 0!=memcmp(&tBuf[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN) ){ |
| 5533 | return SQLITE_BUSY; |
| 5534 | } |
| 5535 | }else{ |
| 5536 | /* don't break the lock on short read or a version mismatch */ |
| 5537 | return SQLITE_BUSY; |
| 5538 | } |
| 5539 | usleep(10000000); /* wait 10 sec and try the lock again */ |
| 5540 | continue; |
| 5541 | } |
| 5542 | |
| 5543 | assert( nTries==3 ); |
| 5544 | if( 0==proxyBreakConchLock(pFile, myHostID) ){ |
| 5545 | rc = SQLITE_OK; |
| 5546 | if( lockType==EXCLUSIVE_LOCK ){ |
| 5547 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5548 | } |
| 5549 | if( !rc ){ |
| 5550 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, lockType); |
| 5551 | } |
| 5552 | } |
| 5553 | } |
| 5554 | } while( rc==SQLITE_BUSY && nTries<3 ); |
| 5555 | |
| 5556 | return rc; |
| 5557 | } |
| 5558 | |
| 5559 | /* Takes the conch by taking a shared lock and read the contents conch, if |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5560 | ** lockPath is non-NULL, the host ID and lock file path must match. A NULL |
| 5561 | ** lockPath means that the lockPath in the conch file will be used if the |
| 5562 | ** host IDs match, or a new lock path will be generated automatically |
| 5563 | ** and written to the conch file. |
| 5564 | */ |
| 5565 | static int proxyTakeConch(unixFile *pFile){ |
| 5566 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5567 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5568 | if( pCtx->conchHeld!=0 ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5569 | return SQLITE_OK; |
| 5570 | }else{ |
| 5571 | unixFile *conchFile = pCtx->conchFile; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5572 | uuid_t myHostID; |
| 5573 | int pError = 0; |
| 5574 | char readBuf[PROXY_MAXCONCHLEN]; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5575 | char lockPath[MAXPATHLEN]; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5576 | char *tempLockPath = NULL; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5577 | int rc = SQLITE_OK; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5578 | int createConch = 0; |
| 5579 | int hostIdMatch = 0; |
| 5580 | int readLen = 0; |
| 5581 | int tryOldLockPath = 0; |
| 5582 | int forceNewLockPath = 0; |
| 5583 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5584 | OSTRACE(("TAKECONCH %d for %s pid=%d\n", conchFile->h, |
| 5585 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5586 | |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5587 | rc = proxyGetHostID(myHostID, &pError); |
| 5588 | if( (rc&0xff)==SQLITE_IOERR ){ |
| 5589 | pFile->lastErrno = pError; |
| 5590 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5591 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5592 | rc = proxyConchLock(pFile, myHostID, SHARED_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5593 | if( rc!=SQLITE_OK ){ |
| 5594 | goto end_takeconch; |
| 5595 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5596 | /* read the existing conch file */ |
| 5597 | readLen = seekAndRead((unixFile*)conchFile, 0, readBuf, PROXY_MAXCONCHLEN); |
| 5598 | if( readLen<0 ){ |
| 5599 | /* I/O error: lastErrno set by seekAndRead */ |
| 5600 | pFile->lastErrno = conchFile->lastErrno; |
| 5601 | rc = SQLITE_IOERR_READ; |
| 5602 | goto end_takeconch; |
| 5603 | }else if( readLen<=(PROXY_HEADERLEN+PROXY_HOSTIDLEN) || |
| 5604 | readBuf[0]!=(char)PROXY_CONCHVERSION ){ |
| 5605 | /* a short read or version format mismatch means we need to create a new |
| 5606 | ** conch file. |
| 5607 | */ |
| 5608 | createConch = 1; |
| 5609 | } |
| 5610 | /* if the host id matches and the lock path already exists in the conch |
| 5611 | ** we'll try to use the path there, if we can't open that path, we'll |
| 5612 | ** retry with a new auto-generated path |
| 5613 | */ |
| 5614 | do { /* in case we need to try again for an :auto: named lock file */ |
| 5615 | |
| 5616 | if( !createConch && !forceNewLockPath ){ |
| 5617 | hostIdMatch = !memcmp(&readBuf[PROXY_HEADERLEN], myHostID, |
| 5618 | PROXY_HOSTIDLEN); |
| 5619 | /* if the conch has data compare the contents */ |
| 5620 | if( !pCtx->lockProxyPath ){ |
| 5621 | /* for auto-named local lock file, just check the host ID and we'll |
| 5622 | ** use the local lock file path that's already in there |
| 5623 | */ |
| 5624 | if( hostIdMatch ){ |
| 5625 | size_t pathLen = (readLen - PROXY_PATHINDEX); |
| 5626 | |
| 5627 | if( pathLen>=MAXPATHLEN ){ |
| 5628 | pathLen=MAXPATHLEN-1; |
| 5629 | } |
| 5630 | memcpy(lockPath, &readBuf[PROXY_PATHINDEX], pathLen); |
| 5631 | lockPath[pathLen] = 0; |
| 5632 | tempLockPath = lockPath; |
| 5633 | tryOldLockPath = 1; |
| 5634 | /* create a copy of the lock path if the conch is taken */ |
| 5635 | goto end_takeconch; |
| 5636 | } |
| 5637 | }else if( hostIdMatch |
| 5638 | && !strncmp(pCtx->lockProxyPath, &readBuf[PROXY_PATHINDEX], |
| 5639 | readLen-PROXY_PATHINDEX) |
| 5640 | ){ |
| 5641 | /* conch host and lock path match */ |
| 5642 | goto end_takeconch; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5643 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5644 | } |
| 5645 | |
| 5646 | /* if the conch isn't writable and doesn't match, we can't take it */ |
| 5647 | if( (conchFile->openFlags&O_RDWR) == 0 ){ |
| 5648 | rc = SQLITE_BUSY; |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5649 | goto end_takeconch; |
| 5650 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5651 | |
| 5652 | /* either the conch didn't match or we need to create a new one */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5653 | if( !pCtx->lockProxyPath ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5654 | proxyGetLockPath(pCtx->dbPath, lockPath, MAXPATHLEN); |
| 5655 | tempLockPath = lockPath; |
| 5656 | /* create a copy of the lock path _only_ if the conch is taken */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5657 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5658 | |
| 5659 | /* update conch with host and path (this will fail if other process |
| 5660 | ** has a shared lock already), if the host id matches, use the big |
| 5661 | ** stick. |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5662 | */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5663 | futimes(conchFile->h, NULL); |
| 5664 | if( hostIdMatch && !createConch ){ |
drh | 8af6c22 | 2010-05-14 12:43:01 +0000 | [diff] [blame] | 5665 | if( conchFile->pInode && conchFile->pInode->nShared>1 ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5666 | /* We are trying for an exclusive lock but another thread in this |
| 5667 | ** same process is still holding a shared lock. */ |
| 5668 | rc = SQLITE_BUSY; |
| 5669 | } else { |
| 5670 | rc = proxyConchLock(pFile, myHostID, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5671 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5672 | }else{ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5673 | rc = conchFile->pMethod->xLock((sqlite3_file*)conchFile, EXCLUSIVE_LOCK); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5674 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5675 | if( rc==SQLITE_OK ){ |
| 5676 | char writeBuffer[PROXY_MAXCONCHLEN]; |
| 5677 | int writeSize = 0; |
| 5678 | |
| 5679 | writeBuffer[0] = (char)PROXY_CONCHVERSION; |
| 5680 | memcpy(&writeBuffer[PROXY_HEADERLEN], myHostID, PROXY_HOSTIDLEN); |
| 5681 | if( pCtx->lockProxyPath!=NULL ){ |
| 5682 | strlcpy(&writeBuffer[PROXY_PATHINDEX], pCtx->lockProxyPath, MAXPATHLEN); |
| 5683 | }else{ |
| 5684 | strlcpy(&writeBuffer[PROXY_PATHINDEX], tempLockPath, MAXPATHLEN); |
| 5685 | } |
| 5686 | writeSize = PROXY_PATHINDEX + strlen(&writeBuffer[PROXY_PATHINDEX]); |
| 5687 | ftruncate(conchFile->h, writeSize); |
| 5688 | rc = unixWrite((sqlite3_file *)conchFile, writeBuffer, writeSize, 0); |
| 5689 | fsync(conchFile->h); |
| 5690 | /* If we created a new conch file (not just updated the contents of a |
| 5691 | ** valid conch file), try to match the permissions of the database |
| 5692 | */ |
| 5693 | if( rc==SQLITE_OK && createConch ){ |
| 5694 | struct stat buf; |
| 5695 | int err = fstat(pFile->h, &buf); |
| 5696 | if( err==0 ){ |
| 5697 | mode_t cmode = buf.st_mode&(S_IRUSR|S_IWUSR | S_IRGRP|S_IWGRP | |
| 5698 | S_IROTH|S_IWOTH); |
| 5699 | /* try to match the database file R/W permissions, ignore failure */ |
| 5700 | #ifndef SQLITE_PROXY_DEBUG |
| 5701 | fchmod(conchFile->h, cmode); |
| 5702 | #else |
| 5703 | if( fchmod(conchFile->h, cmode)!=0 ){ |
| 5704 | int code = errno; |
| 5705 | fprintf(stderr, "fchmod %o FAILED with %d %s\n", |
| 5706 | cmode, code, strerror(code)); |
| 5707 | } else { |
| 5708 | fprintf(stderr, "fchmod %o SUCCEDED\n",cmode); |
| 5709 | } |
| 5710 | }else{ |
| 5711 | int code = errno; |
| 5712 | fprintf(stderr, "STAT FAILED[%d] with %d %s\n", |
| 5713 | err, code, strerror(code)); |
| 5714 | #endif |
| 5715 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5716 | } |
| 5717 | } |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5718 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, SHARED_LOCK); |
| 5719 | |
| 5720 | end_takeconch: |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5721 | OSTRACE(("TRANSPROXY: CLOSE %d\n", pFile->h)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5722 | if( rc==SQLITE_OK && pFile->openFlags ){ |
| 5723 | if( pFile->h>=0 ){ |
| 5724 | #ifdef STRICT_CLOSE_ERROR |
| 5725 | if( close(pFile->h) ){ |
| 5726 | pFile->lastErrno = errno; |
| 5727 | return SQLITE_IOERR_CLOSE; |
| 5728 | } |
| 5729 | #else |
| 5730 | close(pFile->h); /* silently leak fd if fail */ |
| 5731 | #endif |
| 5732 | } |
| 5733 | pFile->h = -1; |
| 5734 | int fd = open(pCtx->dbPath, pFile->openFlags, |
| 5735 | SQLITE_DEFAULT_FILE_PERMISSIONS); |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5736 | OSTRACE(("TRANSPROXY: OPEN %d\n", fd)); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5737 | if( fd>=0 ){ |
| 5738 | pFile->h = fd; |
| 5739 | }else{ |
drh | 9978c97 | 2010-02-23 17:36:32 +0000 | [diff] [blame] | 5740 | rc=SQLITE_CANTOPEN_BKPT; /* SQLITE_BUSY? proxyTakeConch called |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5741 | during locking */ |
| 5742 | } |
| 5743 | } |
| 5744 | if( rc==SQLITE_OK && !pCtx->lockProxy ){ |
| 5745 | char *path = tempLockPath ? tempLockPath : pCtx->lockProxyPath; |
| 5746 | rc = proxyCreateUnixFile(path, &pCtx->lockProxy, 1); |
| 5747 | if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && tryOldLockPath ){ |
| 5748 | /* we couldn't create the proxy lock file with the old lock file path |
| 5749 | ** so try again via auto-naming |
| 5750 | */ |
| 5751 | forceNewLockPath = 1; |
| 5752 | tryOldLockPath = 0; |
dan | 2b0ef47 | 2010-02-16 12:18:47 +0000 | [diff] [blame] | 5753 | continue; /* go back to the do {} while start point, try again */ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5754 | } |
| 5755 | } |
| 5756 | if( rc==SQLITE_OK ){ |
| 5757 | /* Need to make a copy of path if we extracted the value |
| 5758 | ** from the conch file or the path was allocated on the stack |
| 5759 | */ |
| 5760 | if( tempLockPath ){ |
| 5761 | pCtx->lockProxyPath = sqlite3DbStrDup(0, tempLockPath); |
| 5762 | if( !pCtx->lockProxyPath ){ |
| 5763 | rc = SQLITE_NOMEM; |
| 5764 | } |
| 5765 | } |
| 5766 | } |
| 5767 | if( rc==SQLITE_OK ){ |
| 5768 | pCtx->conchHeld = 1; |
| 5769 | |
| 5770 | if( pCtx->lockProxy->pMethod == &afpIoMethods ){ |
| 5771 | afpLockingContext *afpCtx; |
| 5772 | afpCtx = (afpLockingContext *)pCtx->lockProxy->lockingContext; |
| 5773 | afpCtx->dbPath = pCtx->lockProxyPath; |
| 5774 | } |
| 5775 | } else { |
| 5776 | conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5777 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5778 | OSTRACE(("TAKECONCH %d %s\n", conchFile->h, |
| 5779 | rc==SQLITE_OK?"ok":"failed")); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5780 | return rc; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5781 | } while (1); /* in case we need to retry the :auto: lock file - |
| 5782 | ** we should never get here except via the 'continue' call. */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5783 | } |
| 5784 | } |
| 5785 | |
| 5786 | /* |
| 5787 | ** If pFile holds a lock on a conch file, then release that lock. |
| 5788 | */ |
| 5789 | static int proxyReleaseConch(unixFile *pFile){ |
drh | 1c5bb4d | 2010-05-10 17:29:28 +0000 | [diff] [blame] | 5790 | int rc = SQLITE_OK; /* Subroutine return code */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5791 | proxyLockingContext *pCtx; /* The locking context for the proxy lock */ |
| 5792 | unixFile *conchFile; /* Name of the conch file */ |
| 5793 | |
| 5794 | pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 5795 | conchFile = pCtx->conchFile; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5796 | OSTRACE(("RELEASECONCH %d for %s pid=%d\n", conchFile->h, |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5797 | (pCtx->lockProxyPath ? pCtx->lockProxyPath : ":auto:"), |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5798 | getpid())); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5799 | if( pCtx->conchHeld>0 ){ |
| 5800 | rc = conchFile->pMethod->xUnlock((sqlite3_file*)conchFile, NO_LOCK); |
| 5801 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5802 | pCtx->conchHeld = 0; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5803 | OSTRACE(("RELEASECONCH %d %s\n", conchFile->h, |
| 5804 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5805 | return rc; |
| 5806 | } |
| 5807 | |
| 5808 | /* |
| 5809 | ** Given the name of a database file, compute the name of its conch file. |
| 5810 | ** Store the conch filename in memory obtained from sqlite3_malloc(). |
| 5811 | ** Make *pConchPath point to the new name. Return SQLITE_OK on success |
| 5812 | ** or SQLITE_NOMEM if unable to obtain memory. |
| 5813 | ** |
| 5814 | ** The caller is responsible for ensuring that the allocated memory |
| 5815 | ** space is eventually freed. |
| 5816 | ** |
| 5817 | ** *pConchPath is set to NULL if a memory allocation error occurs. |
| 5818 | */ |
| 5819 | static int proxyCreateConchPathname(char *dbPath, char **pConchPath){ |
| 5820 | int i; /* Loop counter */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5821 | int len = (int)strlen(dbPath); /* Length of database filename - dbPath */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5822 | char *conchPath; /* buffer in which to construct conch name */ |
| 5823 | |
| 5824 | /* Allocate space for the conch filename and initialize the name to |
| 5825 | ** the name of the original database file. */ |
| 5826 | *pConchPath = conchPath = (char *)sqlite3_malloc(len + 8); |
| 5827 | if( conchPath==0 ){ |
| 5828 | return SQLITE_NOMEM; |
| 5829 | } |
| 5830 | memcpy(conchPath, dbPath, len+1); |
| 5831 | |
| 5832 | /* now insert a "." before the last / character */ |
| 5833 | for( i=(len-1); i>=0; i-- ){ |
| 5834 | if( conchPath[i]=='/' ){ |
| 5835 | i++; |
| 5836 | break; |
| 5837 | } |
| 5838 | } |
| 5839 | conchPath[i]='.'; |
| 5840 | while ( i<len ){ |
| 5841 | conchPath[i+1]=dbPath[i]; |
| 5842 | i++; |
| 5843 | } |
| 5844 | |
| 5845 | /* append the "-conch" suffix to the file */ |
| 5846 | memcpy(&conchPath[i+1], "-conch", 7); |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5847 | assert( (int)strlen(conchPath) == len+7 ); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5848 | |
| 5849 | return SQLITE_OK; |
| 5850 | } |
| 5851 | |
| 5852 | |
| 5853 | /* Takes a fully configured proxy locking-style unix file and switches |
| 5854 | ** the local lock file path |
| 5855 | */ |
| 5856 | static int switchLockProxyPath(unixFile *pFile, const char *path) { |
| 5857 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 5858 | char *oldPath = pCtx->lockProxyPath; |
| 5859 | int rc = SQLITE_OK; |
| 5860 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5861 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5862 | return SQLITE_BUSY; |
| 5863 | } |
| 5864 | |
| 5865 | /* nothing to do if the path is NULL, :auto: or matches the existing path */ |
| 5866 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") || |
| 5867 | (oldPath && !strncmp(oldPath, path, MAXPATHLEN)) ){ |
| 5868 | return SQLITE_OK; |
| 5869 | }else{ |
| 5870 | unixFile *lockProxy = pCtx->lockProxy; |
| 5871 | pCtx->lockProxy=NULL; |
| 5872 | pCtx->conchHeld = 0; |
| 5873 | if( lockProxy!=NULL ){ |
| 5874 | rc=lockProxy->pMethod->xClose((sqlite3_file *)lockProxy); |
| 5875 | if( rc ) return rc; |
| 5876 | sqlite3_free(lockProxy); |
| 5877 | } |
| 5878 | sqlite3_free(oldPath); |
| 5879 | pCtx->lockProxyPath = sqlite3DbStrDup(0, path); |
| 5880 | } |
| 5881 | |
| 5882 | return rc; |
| 5883 | } |
| 5884 | |
| 5885 | /* |
| 5886 | ** pFile is a file that has been opened by a prior xOpen call. dbPath |
| 5887 | ** is a string buffer at least MAXPATHLEN+1 characters in size. |
| 5888 | ** |
| 5889 | ** This routine find the filename associated with pFile and writes it |
| 5890 | ** int dbPath. |
| 5891 | */ |
| 5892 | static int proxyGetDbPathForUnixFile(unixFile *pFile, char *dbPath){ |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 5893 | #if defined(__APPLE__) |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5894 | if( pFile->pMethod == &afpIoMethods ){ |
| 5895 | /* afp style keeps a reference to the db path in the filePath field |
| 5896 | ** of the struct */ |
drh | ea67883 | 2008-12-10 19:26:22 +0000 | [diff] [blame] | 5897 | assert( (int)strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5898 | strlcpy(dbPath, ((afpLockingContext *)pFile->lockingContext)->dbPath, MAXPATHLEN); |
| 5899 | } else |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5900 | #endif |
| 5901 | if( pFile->pMethod == &dotlockIoMethods ){ |
| 5902 | /* dot lock style uses the locking context to store the dot lock |
| 5903 | ** file path */ |
| 5904 | int len = strlen((char *)pFile->lockingContext) - strlen(DOTLOCK_SUFFIX); |
| 5905 | memcpy(dbPath, (char *)pFile->lockingContext, len + 1); |
| 5906 | }else{ |
| 5907 | /* all other styles use the locking context to store the db file path */ |
| 5908 | assert( strlen((char*)pFile->lockingContext)<=MAXPATHLEN ); |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5909 | strlcpy(dbPath, (char *)pFile->lockingContext, MAXPATHLEN); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5910 | } |
| 5911 | return SQLITE_OK; |
| 5912 | } |
| 5913 | |
| 5914 | /* |
| 5915 | ** Takes an already filled in unix file and alters it so all file locking |
| 5916 | ** will be performed on the local proxy lock file. The following fields |
| 5917 | ** are preserved in the locking context so that they can be restored and |
| 5918 | ** the unix structure properly cleaned up at close time: |
| 5919 | ** ->lockingContext |
| 5920 | ** ->pMethod |
| 5921 | */ |
| 5922 | static int proxyTransformUnixFile(unixFile *pFile, const char *path) { |
| 5923 | proxyLockingContext *pCtx; |
| 5924 | char dbPath[MAXPATHLEN+1]; /* Name of the database file */ |
| 5925 | char *lockPath=NULL; |
| 5926 | int rc = SQLITE_OK; |
| 5927 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5928 | if( pFile->eFileLock!=NO_LOCK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5929 | return SQLITE_BUSY; |
| 5930 | } |
| 5931 | proxyGetDbPathForUnixFile(pFile, dbPath); |
| 5932 | if( !path || path[0]=='\0' || !strcmp(path, ":auto:") ){ |
| 5933 | lockPath=NULL; |
| 5934 | }else{ |
| 5935 | lockPath=(char *)path; |
| 5936 | } |
| 5937 | |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5938 | OSTRACE(("TRANSPROXY %d for %s pid=%d\n", pFile->h, |
| 5939 | (lockPath ? lockPath : ":auto:"), getpid())); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5940 | |
| 5941 | pCtx = sqlite3_malloc( sizeof(*pCtx) ); |
| 5942 | if( pCtx==0 ){ |
| 5943 | return SQLITE_NOMEM; |
| 5944 | } |
| 5945 | memset(pCtx, 0, sizeof(*pCtx)); |
| 5946 | |
| 5947 | rc = proxyCreateConchPathname(dbPath, &pCtx->conchFilePath); |
| 5948 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5949 | rc = proxyCreateUnixFile(pCtx->conchFilePath, &pCtx->conchFile, 0); |
| 5950 | if( rc==SQLITE_CANTOPEN && ((pFile->openFlags&O_RDWR) == 0) ){ |
| 5951 | /* if (a) the open flags are not O_RDWR, (b) the conch isn't there, and |
| 5952 | ** (c) the file system is read-only, then enable no-locking access. |
| 5953 | ** Ugh, since O_RDONLY==0x0000 we test for !O_RDWR since unixOpen asserts |
| 5954 | ** that openFlags will have only one of O_RDONLY or O_RDWR. |
| 5955 | */ |
| 5956 | struct statfs fsInfo; |
| 5957 | struct stat conchInfo; |
| 5958 | int goLockless = 0; |
| 5959 | |
| 5960 | if( stat(pCtx->conchFilePath, &conchInfo) == -1 ) { |
| 5961 | int err = errno; |
| 5962 | if( (err==ENOENT) && (statfs(dbPath, &fsInfo) != -1) ){ |
| 5963 | goLockless = (fsInfo.f_flags&MNT_RDONLY) == MNT_RDONLY; |
| 5964 | } |
| 5965 | } |
| 5966 | if( goLockless ){ |
| 5967 | pCtx->conchHeld = -1; /* read only FS/ lockless */ |
| 5968 | rc = SQLITE_OK; |
| 5969 | } |
| 5970 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5971 | } |
| 5972 | if( rc==SQLITE_OK && lockPath ){ |
| 5973 | pCtx->lockProxyPath = sqlite3DbStrDup(0, lockPath); |
| 5974 | } |
| 5975 | |
| 5976 | if( rc==SQLITE_OK ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5977 | pCtx->dbPath = sqlite3DbStrDup(0, dbPath); |
| 5978 | if( pCtx->dbPath==NULL ){ |
| 5979 | rc = SQLITE_NOMEM; |
| 5980 | } |
| 5981 | } |
| 5982 | if( rc==SQLITE_OK ){ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5983 | /* all memory is allocated, proxys are created and assigned, |
| 5984 | ** switch the locking context and pMethod then return. |
| 5985 | */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5986 | pCtx->oldLockingContext = pFile->lockingContext; |
| 5987 | pFile->lockingContext = pCtx; |
| 5988 | pCtx->pOldMethod = pFile->pMethod; |
| 5989 | pFile->pMethod = &proxyIoMethods; |
| 5990 | }else{ |
| 5991 | if( pCtx->conchFile ){ |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 5992 | pCtx->conchFile->pMethod->xClose((sqlite3_file *)pCtx->conchFile); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5993 | sqlite3_free(pCtx->conchFile); |
| 5994 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 5995 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 5996 | sqlite3_free(pCtx->conchFilePath); |
| 5997 | sqlite3_free(pCtx); |
| 5998 | } |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 5999 | OSTRACE(("TRANSPROXY %d %s\n", pFile->h, |
| 6000 | (rc==SQLITE_OK ? "ok" : "failed"))); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6001 | return rc; |
| 6002 | } |
| 6003 | |
| 6004 | |
| 6005 | /* |
| 6006 | ** This routine handles sqlite3_file_control() calls that are specific |
| 6007 | ** to proxy locking. |
| 6008 | */ |
| 6009 | static int proxyFileControl(sqlite3_file *id, int op, void *pArg){ |
| 6010 | switch( op ){ |
| 6011 | case SQLITE_GET_LOCKPROXYFILE: { |
| 6012 | unixFile *pFile = (unixFile*)id; |
| 6013 | if( pFile->pMethod == &proxyIoMethods ){ |
| 6014 | proxyLockingContext *pCtx = (proxyLockingContext*)pFile->lockingContext; |
| 6015 | proxyTakeConch(pFile); |
| 6016 | if( pCtx->lockProxyPath ){ |
| 6017 | *(const char **)pArg = pCtx->lockProxyPath; |
| 6018 | }else{ |
| 6019 | *(const char **)pArg = ":auto: (not held)"; |
| 6020 | } |
| 6021 | } else { |
| 6022 | *(const char **)pArg = NULL; |
| 6023 | } |
| 6024 | return SQLITE_OK; |
| 6025 | } |
| 6026 | case SQLITE_SET_LOCKPROXYFILE: { |
| 6027 | unixFile *pFile = (unixFile*)id; |
| 6028 | int rc = SQLITE_OK; |
| 6029 | int isProxyStyle = (pFile->pMethod == &proxyIoMethods); |
| 6030 | if( pArg==NULL || (const char *)pArg==0 ){ |
| 6031 | if( isProxyStyle ){ |
| 6032 | /* turn off proxy locking - not supported */ |
| 6033 | rc = SQLITE_ERROR /*SQLITE_PROTOCOL? SQLITE_MISUSE?*/; |
| 6034 | }else{ |
| 6035 | /* turn off proxy locking - already off - NOOP */ |
| 6036 | rc = SQLITE_OK; |
| 6037 | } |
| 6038 | }else{ |
| 6039 | const char *proxyPath = (const char *)pArg; |
| 6040 | if( isProxyStyle ){ |
| 6041 | proxyLockingContext *pCtx = |
| 6042 | (proxyLockingContext*)pFile->lockingContext; |
| 6043 | if( !strcmp(pArg, ":auto:") |
| 6044 | || (pCtx->lockProxyPath && |
| 6045 | !strncmp(pCtx->lockProxyPath, proxyPath, MAXPATHLEN)) |
| 6046 | ){ |
| 6047 | rc = SQLITE_OK; |
| 6048 | }else{ |
| 6049 | rc = switchLockProxyPath(pFile, proxyPath); |
| 6050 | } |
| 6051 | }else{ |
| 6052 | /* turn on proxy file locking */ |
| 6053 | rc = proxyTransformUnixFile(pFile, proxyPath); |
| 6054 | } |
| 6055 | } |
| 6056 | return rc; |
| 6057 | } |
| 6058 | default: { |
| 6059 | assert( 0 ); /* The call assures that only valid opcodes are sent */ |
| 6060 | } |
| 6061 | } |
| 6062 | /*NOTREACHED*/ |
| 6063 | return SQLITE_ERROR; |
| 6064 | } |
| 6065 | |
| 6066 | /* |
| 6067 | ** Within this division (the proxying locking implementation) the procedures |
| 6068 | ** above this point are all utilities. The lock-related methods of the |
| 6069 | ** proxy-locking sqlite3_io_method object follow. |
| 6070 | */ |
| 6071 | |
| 6072 | |
| 6073 | /* |
| 6074 | ** This routine checks if there is a RESERVED lock held on the specified |
| 6075 | ** file by this or any other process. If such a lock is held, set *pResOut |
| 6076 | ** to a non-zero value otherwise *pResOut is set to zero. The return value |
| 6077 | ** is set to SQLITE_OK unless an I/O error occurs during lock checking. |
| 6078 | */ |
| 6079 | static int proxyCheckReservedLock(sqlite3_file *id, int *pResOut) { |
| 6080 | unixFile *pFile = (unixFile*)id; |
| 6081 | int rc = proxyTakeConch(pFile); |
| 6082 | if( rc==SQLITE_OK ){ |
| 6083 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6084 | if( pCtx->conchHeld>0 ){ |
| 6085 | unixFile *proxy = pCtx->lockProxy; |
| 6086 | return proxy->pMethod->xCheckReservedLock((sqlite3_file*)proxy, pResOut); |
| 6087 | }else{ /* conchHeld < 0 is lockless */ |
| 6088 | pResOut=0; |
| 6089 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6090 | } |
| 6091 | return rc; |
| 6092 | } |
| 6093 | |
| 6094 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6095 | ** Lock the file with the lock specified by parameter eFileLock - one |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6096 | ** of the following: |
| 6097 | ** |
| 6098 | ** (1) SHARED_LOCK |
| 6099 | ** (2) RESERVED_LOCK |
| 6100 | ** (3) PENDING_LOCK |
| 6101 | ** (4) EXCLUSIVE_LOCK |
| 6102 | ** |
| 6103 | ** Sometimes when requesting one lock state, additional lock states |
| 6104 | ** are inserted in between. The locking might fail on one of the later |
| 6105 | ** transitions leaving the lock state different from what it started but |
| 6106 | ** still short of its goal. The following chart shows the allowed |
| 6107 | ** transitions and the inserted intermediate states: |
| 6108 | ** |
| 6109 | ** UNLOCKED -> SHARED |
| 6110 | ** SHARED -> RESERVED |
| 6111 | ** SHARED -> (PENDING) -> EXCLUSIVE |
| 6112 | ** RESERVED -> (PENDING) -> EXCLUSIVE |
| 6113 | ** PENDING -> EXCLUSIVE |
| 6114 | ** |
| 6115 | ** This routine will only increase a lock. Use the sqlite3OsUnlock() |
| 6116 | ** routine to lower a locking level. |
| 6117 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6118 | static int proxyLock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6119 | unixFile *pFile = (unixFile*)id; |
| 6120 | int rc = proxyTakeConch(pFile); |
| 6121 | if( rc==SQLITE_OK ){ |
| 6122 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6123 | if( pCtx->conchHeld>0 ){ |
| 6124 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6125 | rc = proxy->pMethod->xLock((sqlite3_file*)proxy, eFileLock); |
| 6126 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6127 | }else{ |
| 6128 | /* conchHeld < 0 is lockless */ |
| 6129 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6130 | } |
| 6131 | return rc; |
| 6132 | } |
| 6133 | |
| 6134 | |
| 6135 | /* |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6136 | ** Lower the locking level on file descriptor pFile to eFileLock. eFileLock |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6137 | ** must be either NO_LOCK or SHARED_LOCK. |
| 6138 | ** |
| 6139 | ** If the locking level of the file descriptor is already at or below |
| 6140 | ** the requested locking level, this routine is a no-op. |
| 6141 | */ |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6142 | static int proxyUnlock(sqlite3_file *id, int eFileLock) { |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6143 | unixFile *pFile = (unixFile*)id; |
| 6144 | int rc = proxyTakeConch(pFile); |
| 6145 | if( rc==SQLITE_OK ){ |
| 6146 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6147 | if( pCtx->conchHeld>0 ){ |
| 6148 | unixFile *proxy = pCtx->lockProxy; |
drh | 308c2a5 | 2010-05-14 11:30:18 +0000 | [diff] [blame] | 6149 | rc = proxy->pMethod->xUnlock((sqlite3_file*)proxy, eFileLock); |
| 6150 | pFile->eFileLock = proxy->eFileLock; |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6151 | }else{ |
| 6152 | /* conchHeld < 0 is lockless */ |
| 6153 | } |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6154 | } |
| 6155 | return rc; |
| 6156 | } |
| 6157 | |
| 6158 | /* |
| 6159 | ** Close a file that uses proxy locks. |
| 6160 | */ |
| 6161 | static int proxyClose(sqlite3_file *id) { |
| 6162 | if( id ){ |
| 6163 | unixFile *pFile = (unixFile*)id; |
| 6164 | proxyLockingContext *pCtx = (proxyLockingContext *)pFile->lockingContext; |
| 6165 | unixFile *lockProxy = pCtx->lockProxy; |
| 6166 | unixFile *conchFile = pCtx->conchFile; |
| 6167 | int rc = SQLITE_OK; |
| 6168 | |
| 6169 | if( lockProxy ){ |
| 6170 | rc = lockProxy->pMethod->xUnlock((sqlite3_file*)lockProxy, NO_LOCK); |
| 6171 | if( rc ) return rc; |
| 6172 | rc = lockProxy->pMethod->xClose((sqlite3_file*)lockProxy); |
| 6173 | if( rc ) return rc; |
| 6174 | sqlite3_free(lockProxy); |
| 6175 | pCtx->lockProxy = 0; |
| 6176 | } |
| 6177 | if( conchFile ){ |
| 6178 | if( pCtx->conchHeld ){ |
| 6179 | rc = proxyReleaseConch(pFile); |
| 6180 | if( rc ) return rc; |
| 6181 | } |
| 6182 | rc = conchFile->pMethod->xClose((sqlite3_file*)conchFile); |
| 6183 | if( rc ) return rc; |
| 6184 | sqlite3_free(conchFile); |
| 6185 | } |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6186 | sqlite3DbFree(0, pCtx->lockProxyPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6187 | sqlite3_free(pCtx->conchFilePath); |
drh | d56b121 | 2010-08-11 06:14:15 +0000 | [diff] [blame] | 6188 | sqlite3DbFree(0, pCtx->dbPath); |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6189 | /* restore the original locking context and pMethod then close it */ |
| 6190 | pFile->lockingContext = pCtx->oldLockingContext; |
| 6191 | pFile->pMethod = pCtx->pOldMethod; |
| 6192 | sqlite3_free(pCtx); |
| 6193 | return pFile->pMethod->xClose(id); |
| 6194 | } |
| 6195 | return SQLITE_OK; |
| 6196 | } |
| 6197 | |
| 6198 | |
| 6199 | |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6200 | #endif /* defined(__APPLE__) && SQLITE_ENABLE_LOCKING_STYLE */ |
drh | 715ff30 | 2008-12-03 22:32:44 +0000 | [diff] [blame] | 6201 | /* |
| 6202 | ** The proxy locking style is intended for use with AFP filesystems. |
| 6203 | ** And since AFP is only supported on MacOSX, the proxy locking is also |
| 6204 | ** restricted to MacOSX. |
| 6205 | ** |
| 6206 | ** |
| 6207 | ******************* End of the proxy lock implementation ********************** |
| 6208 | ******************************************************************************/ |
| 6209 | |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6210 | /* |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6211 | ** Initialize the operating system interface. |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6212 | ** |
| 6213 | ** This routine registers all VFS implementations for unix-like operating |
| 6214 | ** systems. This routine, and the sqlite3_os_end() routine that follows, |
| 6215 | ** should be the only routines in this file that are visible from other |
| 6216 | ** files. |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6217 | ** |
| 6218 | ** This routine is called once during SQLite initialization and by a |
| 6219 | ** single thread. The memory allocation and mutex subsystems have not |
| 6220 | ** necessarily been initialized when this routine is called, and so they |
| 6221 | ** should not be used. |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6222 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6223 | int sqlite3_os_init(void){ |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6224 | /* |
| 6225 | ** The following macro defines an initializer for an sqlite3_vfs object. |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6226 | ** The name of the VFS is NAME. The pAppData is a pointer to a pointer |
| 6227 | ** to the "finder" function. (pAppData is a pointer to a pointer because |
| 6228 | ** silly C90 rules prohibit a void* from being cast to a function pointer |
| 6229 | ** and so we have to go through the intermediate pointer to avoid problems |
| 6230 | ** when compiling with -pedantic-errors on GCC.) |
| 6231 | ** |
| 6232 | ** The FINDER parameter to this macro is the name of the pointer to the |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6233 | ** finder-function. The finder-function returns a pointer to the |
| 6234 | ** sqlite_io_methods object that implements the desired locking |
| 6235 | ** behaviors. See the division above that contains the IOMETHODS |
| 6236 | ** macro for addition information on finder-functions. |
| 6237 | ** |
| 6238 | ** Most finders simply return a pointer to a fixed sqlite3_io_methods |
| 6239 | ** object. But the "autolockIoFinder" available on MacOSX does a little |
| 6240 | ** more than that; it looks at the filesystem type that hosts the |
| 6241 | ** database file and tries to choose an locking method appropriate for |
| 6242 | ** that filesystem time. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6243 | */ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6244 | #define UNIXVFS(VFSNAME, FINDER) { \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6245 | 2, /* iVersion */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6246 | sizeof(unixFile), /* szOsFile */ \ |
| 6247 | MAX_PATHNAME, /* mxPathname */ \ |
| 6248 | 0, /* pNext */ \ |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6249 | VFSNAME, /* zName */ \ |
drh | 1875f7a | 2008-12-08 18:19:17 +0000 | [diff] [blame] | 6250 | (void*)&FINDER, /* pAppData */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6251 | unixOpen, /* xOpen */ \ |
| 6252 | unixDelete, /* xDelete */ \ |
| 6253 | unixAccess, /* xAccess */ \ |
| 6254 | unixFullPathname, /* xFullPathname */ \ |
| 6255 | unixDlOpen, /* xDlOpen */ \ |
| 6256 | unixDlError, /* xDlError */ \ |
| 6257 | unixDlSym, /* xDlSym */ \ |
| 6258 | unixDlClose, /* xDlClose */ \ |
| 6259 | unixRandomness, /* xRandomness */ \ |
| 6260 | unixSleep, /* xSleep */ \ |
| 6261 | unixCurrentTime, /* xCurrentTime */ \ |
drh | f2424c5 | 2010-04-26 00:04:55 +0000 | [diff] [blame] | 6262 | unixGetLastError, /* xGetLastError */ \ |
drh | b7e8ea2 | 2010-05-03 14:32:30 +0000 | [diff] [blame] | 6263 | unixCurrentTimeInt64, /* xCurrentTimeInt64 */ \ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6264 | } |
| 6265 | |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6266 | /* |
| 6267 | ** All default VFSes for unix are contained in the following array. |
| 6268 | ** |
| 6269 | ** Note that the sqlite3_vfs.pNext field of the VFS object is modified |
| 6270 | ** by the SQLite core when the VFS is registered. So the following |
| 6271 | ** array cannot be const. |
| 6272 | */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6273 | static sqlite3_vfs aVfs[] = { |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6274 | #if SQLITE_ENABLE_LOCKING_STYLE && (OS_VXWORKS || defined(__APPLE__)) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6275 | UNIXVFS("unix", autolockIoFinder ), |
| 6276 | #else |
| 6277 | UNIXVFS("unix", posixIoFinder ), |
| 6278 | #endif |
| 6279 | UNIXVFS("unix-none", nolockIoFinder ), |
| 6280 | UNIXVFS("unix-dotfile", dotlockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6281 | #if OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6282 | UNIXVFS("unix-namedsem", semIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6283 | #endif |
| 6284 | #if SQLITE_ENABLE_LOCKING_STYLE |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6285 | UNIXVFS("unix-posix", posixIoFinder ), |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6286 | #if !OS_VXWORKS |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6287 | UNIXVFS("unix-flock", flockIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6288 | #endif |
chw | 78a1318 | 2009-04-07 05:35:03 +0000 | [diff] [blame] | 6289 | #endif |
drh | d2cb50b | 2009-01-09 21:41:17 +0000 | [diff] [blame] | 6290 | #if SQLITE_ENABLE_LOCKING_STYLE && defined(__APPLE__) |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6291 | UNIXVFS("unix-afp", afpIoFinder ), |
drh | 7ed97b9 | 2010-01-20 13:07:21 +0000 | [diff] [blame] | 6292 | UNIXVFS("unix-nfs", nfsIoFinder ), |
drh | 7708e97 | 2008-11-29 00:56:52 +0000 | [diff] [blame] | 6293 | UNIXVFS("unix-proxy", proxyIoFinder ), |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6294 | #endif |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6295 | }; |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6296 | unsigned int i; /* Loop counter */ |
| 6297 | |
| 6298 | /* Register all VFSes defined in the aVfs[] array */ |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6299 | for(i=0; i<(sizeof(aVfs)/sizeof(sqlite3_vfs)); i++){ |
drh | 734c986 | 2008-11-28 15:37:20 +0000 | [diff] [blame] | 6300 | sqlite3_vfs_register(&aVfs[i], i==0); |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6301 | } |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6302 | return SQLITE_OK; |
drh | 153c62c | 2007-08-24 03:51:33 +0000 | [diff] [blame] | 6303 | } |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6304 | |
| 6305 | /* |
drh | 6b9d6dd | 2008-12-03 19:34:47 +0000 | [diff] [blame] | 6306 | ** Shutdown the operating system interface. |
| 6307 | ** |
| 6308 | ** Some operating systems might need to do some cleanup in this routine, |
| 6309 | ** to release dynamically allocated objects. But not on unix. |
| 6310 | ** This routine is a no-op for unix. |
danielk1977 | e339d65 | 2008-06-28 11:23:00 +0000 | [diff] [blame] | 6311 | */ |
danielk1977 | c0fa4c5 | 2008-06-25 17:19:00 +0000 | [diff] [blame] | 6312 | int sqlite3_os_end(void){ |
| 6313 | return SQLITE_OK; |
| 6314 | } |
drh | dce8bdb | 2007-08-16 13:01:44 +0000 | [diff] [blame] | 6315 | |
danielk1977 | 29bafea | 2008-06-26 10:41:19 +0000 | [diff] [blame] | 6316 | #endif /* SQLITE_OS_UNIX */ |