ctdbd_conn: move CTDB_CONTROL_ENABLE_SEQNUM control to db_open_ctdb
[vlendec/samba-autobuild/.git] / source3 / lib / dbwrap / dbwrap_open.c
1 /*
2    Unix SMB/CIFS implementation.
3    Database interface wrapper
4
5    Copyright (C) Volker Lendecke 2005-2007
6
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.
11
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.
16
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/>.
19 */
20
21 #include "includes.h"
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_ctdbd.h"
30 #include "util_tdb.h"
31 #include "ctdbd_conn.h"
32 #include "messages.h"
33
34 bool db_is_local(const char *name)
35 {
36         const char *sockname = lp_ctdbd_socket();
37
38         if (lp_clustering() && socket_exist(sockname)) {
39                 const char *partname;
40                 /* ctdb only wants the file part of the name */
41                 partname = strrchr(name, '/');
42                 if (partname) {
43                         partname++;
44                 } else {
45                         partname = name;
46                 }
47                 /* allow ctdb for individual databases to be disabled */
48                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
49                         return false;
50                 }
51         }
52
53         return true;
54 }
55
56 /**
57  * open a database
58  */
59 struct db_context *db_open(TALLOC_CTX *mem_ctx,
60                            const char *name,
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)
65 {
66         struct db_context *result = NULL;
67         const char *sockname;
68
69         if (!DBWRAP_LOCK_ORDER_VALID(lock_order)) {
70                 errno = EINVAL;
71                 return NULL;
72         }
73
74         if (tdb_flags & TDB_CLEAR_IF_FIRST) {
75                 const char *base;
76                 bool try_readonly = false;
77
78                 base = strrchr_m(name, '/');
79                 if (base != NULL) {
80                         base += 1;
81                 } else {
82                         base = name;
83                 }
84
85                 if (dbwrap_flags & DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS) {
86                         try_readonly = true;
87                 }
88
89                 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", "*", try_readonly);
90                 try_readonly = lp_parm_bool(-1, "dbwrap_optimize_readonly", base, try_readonly);
91
92                 if (try_readonly) {
93                         dbwrap_flags |= DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
94                 } else {
95                         dbwrap_flags &= ~DBWRAP_FLAG_OPTIMIZE_READONLY_ACCESS;
96                 }
97         }
98
99         if (tdb_flags & TDB_CLEAR_IF_FIRST) {
100                 const char *base;
101                 bool try_mutex = true;
102                 bool require_mutex = false;
103
104                 base = strrchr_m(name, '/');
105                 if (base != NULL) {
106                         base += 1;
107                 } else {
108                         base = name;
109                 }
110
111                 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", "*", try_mutex);
112                 try_mutex = lp_parm_bool(-1, "dbwrap_tdb_mutexes", base, try_mutex);
113
114                 if (try_mutex && tdb_runtime_check_for_robust_mutexes()) {
115                         tdb_flags |= TDB_MUTEX_LOCKING;
116                 }
117
118                 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
119                                            "*", require_mutex);
120                 require_mutex = lp_parm_bool(-1, "dbwrap_tdb_require_mutexes",
121                                            base, require_mutex);
122
123                 if (require_mutex) {
124                         tdb_flags |= TDB_MUTEX_LOCKING;
125                 }
126         }
127
128         sockname = lp_ctdbd_socket();
129
130         if (lp_clustering()) {
131                 const char *partname;
132
133                 if (!socket_exist(sockname)) {
134                         DEBUG(1, ("ctdb socket does not exist - is ctdb not "
135                                   "running?\n"));
136                         return NULL;
137                 }
138
139                 /* ctdb only wants the file part of the name */
140                 partname = strrchr(name, '/');
141                 if (partname) {
142                         partname++;
143                 } else {
144                         partname = name;
145                 }
146                 /* allow ctdb for individual databases to be disabled */
147                 if (lp_parm_bool(-1, "ctdb", partname, True)) {
148                         struct messaging_context *msg_ctx;
149                         struct ctdbd_connection *conn;
150
151                         conn = messaging_ctdbd_connection();
152                         if (conn == NULL) {
153                                 DBG_WARNING("No ctdb connection\n");
154                                 errno = EIO;
155                                 return NULL;
156                         }
157                         msg_ctx = server_messaging_context();
158
159                         result = db_open_ctdb(mem_ctx, msg_ctx, conn, partname,
160                                               hash_size,
161                                               tdb_flags, open_flags, mode,
162                                               lock_order, dbwrap_flags);
163                         if (result == NULL) {
164                                 DEBUG(0,("failed to attach to ctdb %s\n",
165                                          partname));
166                                 if (errno == 0) {
167                                         errno = EIO;
168                                 }
169                                 return NULL;
170                         }
171                 }
172         }
173
174         if (result == NULL) {
175                 struct loadparm_context *lp_ctx = loadparm_init_s3(mem_ctx, loadparm_s3_helpers());
176                 result = dbwrap_local_open(mem_ctx, lp_ctx, name, hash_size,
177                                            tdb_flags, open_flags, mode,
178                                            lock_order, dbwrap_flags);
179                 talloc_unlink(mem_ctx, lp_ctx);
180         }
181         return result;
182 }