2 Unix SMB/CIFS implementation.
3 Database interface wrapper
5 Copyright (C) Volker Lendecke 2005-2007
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program. If not, see <http://www.gnu.org/licenses/>.
22 #include "dbwrap/dbwrap.h"
23 #include "dbwrap/dbwrap_private.h"
24 #include "dbwrap/dbwrap_open.h"
25 #include "dbwrap/dbwrap_tdb.h"
26 #include "dbwrap/dbwrap_ctdb.h"
27 #include "lib/param/param.h"
28 #include "lib/cluster_support.h"
29 #include "lib/messages_ctdb.h"
31 #include "ctdbd_conn.h"
32 #include "global_contexts.h"
34 bool db_is_local(const char *name)
36 const char *sockname = lp_ctdbd_socket();
38 if (lp_clustering() && socket_exist(sockname)) {
40 /* ctdb only wants the file part of the name */
41 partname = strrchr(name, '/');
47 /* allow ctdb for individual databases to be disabled */
48 if (lp_parm_bool(-1, "ctdb", partname, True)) {
59 struct db_context *db_open(TALLOC_CTX *mem_ctx,
61 int hash_size, int tdb_flags,
62 int open_flags, mode_t mode,
63 enum dbwrap_lock_order lock_order,
64 uint64_t dbwrap_flags)
66 struct db_context *result = NULL;
68 struct loadparm_context *lp_ctx = NULL;
70 if ((lock_order != DBWRAP_LOCK_ORDER_NONE) &&
71 !DBWRAP_LOCK_ORDER_VALID(lock_order)) {
76 base = strrchr_m(name, '/');
83 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
84 bool try_readonly = false;
86 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
90 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
91 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
94 dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
96 dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
100 if (tdb_flags & TDB_CLEAR_IF_FIRST) {
101 bool try_mutex = true;
102 bool require_mutex = false;
104 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex);
105 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex);
107 if (!lp_use_mmap()) {
109 * Mutexes require mmap. "use mmap = no" can
110 * be a debugging tool, so let it override the
116 if (try_mutex && tdb_runtime_check_for_robust_mutexes()) {
117 tdb_flags |= TDB_MUTEX_LOCKING;
120 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
122 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
123 base, require_mutex);
126 tdb_flags |= TDB_MUTEX_LOCKING;
130 if (lp_clustering()) {
131 const char *sockname;
133 sockname = lp_ctdbd_socket();
134 if (!socket_exist(sockname)) {
135 DBG_WARNING("ctdb socket does %s not exist - "
136 "is ctdb not running?\n",
141 /* allow ctdb for individual databases to be disabled */
142 if (lp_parm_bool(-1, "ctdb", base, true)) {
143 struct messaging_context *msg_ctx;
144 struct ctdbd_connection *conn;
147 * Initialize messaging before getting the ctdb
148 * connection, as the ctdb connection requires messaging
151 msg_ctx = global_messaging_context();
153 conn = messaging_ctdb_connection();
155 DBG_WARNING("No ctdb connection\n");
160 result = db_open_ctdb(mem_ctx, msg_ctx, base,
162 tdb_flags, open_flags, mode,
163 lock_order, dbwrap_flags);
164 if (result == NULL) {
165 DBG_ERR("failed to attach to ctdb %s\n", base);
176 lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
178 if (hash_size == 0) {
179 hash_size = lpcfg_tdb_hash_size(lp_ctx, name);
181 tdb_flags = lpcfg_tdb_flags(lp_ctx, tdb_flags);
183 result = dbwrap_local_open(mem_ctx,
191 talloc_unlink(mem_ctx, lp_ctx);