Remove unused extended_dn_out_{dereference,ldb}.c files
[abartlet/samba.git/.git] / source3 / smbd / dmapi.c
1 /* 
2    Unix SMB/CIFS implementation.
3    DMAPI Support routines
4
5    Copyright (C) James Peach 2006
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
23 #undef DBGC_CLASS
24 #define DBGC_CLASS DBGC_DMAPI
25
26 #ifndef USE_DMAPI
27
28 uint32 dmapi_file_flags(const char * const path) { return 0; }
29 bool dmapi_have_session(void) { return False; }
30 const void * dmapi_get_current_session(void) { return NULL; }
31
32 #else /* USE_DMAPI */
33
34 #ifdef HAVE_XFS_DMAPI_H
35 #include <xfs/dmapi.h>
36 #elif defined(HAVE_SYS_DMI_H)
37 #include <sys/dmi.h>
38 #elif defined(HAVE_SYS_JFSDMAPI_H)
39 #include <sys/jfsdmapi.h>
40 #elif defined(HAVE_SYS_DMAPI_H)
41 #include <sys/dmapi.h>
42 #elif defined(HAVE_DMAPI_H)
43 #include <dmapi.h>
44 #endif
45
46 #define DMAPI_SESSION_NAME "samba"
47 #define DMAPI_TRACE 10
48
49 static dm_sessid_t samba_dmapi_session = DM_NO_SESSION;
50 static unsigned session_num;
51
52 /* 
53    Initialise DMAPI session. The session is persistant kernel state, 
54    so it might already exist, in which case we merely want to 
55    reconnect to it. This function should be called as root.
56 */
57 static int dmapi_init_session(void)
58 {
59         char    buf[DM_SESSION_INFO_LEN];
60         size_t  buflen;
61         uint        nsessions = 5;
62         dm_sessid_t *sessions = NULL;
63         char    *version;
64         char    *session_name;
65         TALLOC_CTX *tmp_ctx = talloc_new(NULL);
66
67         int i, err;
68
69         if (session_num == 0) {
70                 session_name = DMAPI_SESSION_NAME;
71         } else {
72                 session_name = talloc_asprintf(tmp_ctx, "%s%u", DMAPI_SESSION_NAME,
73                                                session_num);
74         }
75
76         if (session_name == NULL) {
77                 DEBUG(0,("Out of memory in dmapi_init_session\n"));
78                 talloc_free(tmp_ctx);
79                 return -1;
80         }
81  
82
83         if (dm_init_service(&version) < 0) {
84                 DEBUG(0, ("dm_init_service failed - disabling DMAPI\n"));
85                 talloc_free(tmp_ctx);
86                 return -1;
87         }
88
89         ZERO_STRUCT(buf);
90
91         /* Fetch kernel DMAPI sessions until we get any of them */
92         do {
93                 dm_sessid_t *new_sessions;
94                 nsessions *= 2;
95                 new_sessions = TALLOC_REALLOC_ARRAY(tmp_ctx, sessions, 
96                                                     dm_sessid_t, nsessions);
97                 if (new_sessions == NULL) {
98                         talloc_free(tmp_ctx);
99                         return -1;
100                 }
101
102                 sessions = new_sessions;
103                 err = dm_getall_sessions(nsessions, sessions, &nsessions);
104         } while (err == -1 && errno == E2BIG);
105
106         if (err == -1) {
107                 DEBUGADD(DMAPI_TRACE,
108                         ("failed to retrieve DMAPI sessions: %s\n",
109                         strerror(errno)));
110                 talloc_free(tmp_ctx);
111                 return -1;
112         }
113
114         /* Look through existing kernel DMAPI sessions to find out ours */
115         for (i = 0; i < nsessions; ++i) {
116                 err = dm_query_session(sessions[i], sizeof(buf), buf, &buflen);
117                 buf[sizeof(buf) - 1] = '\0';
118                 if (err == 0 && strcmp(session_name, buf) == 0) {
119                         samba_dmapi_session = sessions[i];
120                         DEBUGADD(DMAPI_TRACE,
121                                 ("attached to existing DMAPI session "
122                                  "named '%s'\n", buf));
123                         break;
124                 }
125         }
126
127         /* No session already defined. */
128         if (samba_dmapi_session == DM_NO_SESSION) {
129                 err = dm_create_session(DM_NO_SESSION, 
130                                         session_name,
131                                         &samba_dmapi_session);
132                 if (err < 0) {
133                         DEBUGADD(DMAPI_TRACE,
134                                 ("failed to create new DMAPI session: %s\n",
135                                 strerror(errno)));
136                         samba_dmapi_session = DM_NO_SESSION;
137                         talloc_free(tmp_ctx);
138                         return -1;
139                 }
140
141                 DEBUG(0, ("created new DMAPI session named '%s' for %s\n",
142                           session_name, version));
143         }
144
145         if (samba_dmapi_session != DM_NO_SESSION) {
146                 set_effective_capability(DMAPI_ACCESS_CAPABILITY);
147         }
148
149         /* 
150            Note that we never end the DMAPI session. It gets re-used if possiblie. 
151            DMAPI session is a kernel resource that is usually lives until server reboot
152            and doesn't get destroed when an application finishes.
153
154            However, we free list of references to DMAPI sessions we've got from the kernel
155            as it is not needed anymore once we have found (or created) our session.
156          */
157
158         talloc_free(tmp_ctx);
159         return 0;
160 }
161
162 /*
163   Return a pointer to our DMAPI session, if available.
164   This assumes that you have called dmapi_have_session() first.
165 */
166 const void *dmapi_get_current_session(void)
167 {
168         if (samba_dmapi_session == DM_NO_SESSION) {
169                 return NULL;
170         }
171
172         return (void *)&samba_dmapi_session;
173 }
174         
175 /*
176   dmapi_have_session() must be the first DMAPI call you make in Samba. It will
177   initialize DMAPI, if available, and tell you if you can get a DMAPI session.
178   This should be called in the client-specific child process.
179 */
180
181 bool dmapi_have_session(void)
182 {
183         static bool initialized;
184         if (!initialized) {
185                 initialized = true;
186
187                 become_root();
188                 dmapi_init_session();
189                 unbecome_root();
190
191         }
192
193         return samba_dmapi_session != DM_NO_SESSION;
194 }
195
196 /*
197   only call this when you get back an EINVAL error indicating that the
198   session you are using is invalid. This destroys the existing session
199   and creates a new one.
200  */
201 bool dmapi_new_session(void)
202 {
203         if (dmapi_have_session()) {
204                 /* try to destroy the old one - this may not succeed */
205                 dm_destroy_session(samba_dmapi_session);
206         }
207         samba_dmapi_session = DM_NO_SESSION;
208         become_root();
209         session_num++;
210         dmapi_init_session();
211         unbecome_root();
212         return samba_dmapi_session != DM_NO_SESSION;    
213 }
214
215 /* 
216     only call this when exiting from master smbd process. DMAPI sessions
217     are long-lived kernel resources we ought to share across smbd processes.
218     However, we must free them when all smbd processes are finished to
219     allow other subsystems clean up properly. Not freeing DMAPI session
220     blocks certain HSM implementations from proper shutdown.
221 */
222 bool dmapi_destroy_session(void)
223 {
224         if (samba_dmapi_session != DM_NO_SESSION) {
225                 become_root();
226                 if (0 == dm_destroy_session(samba_dmapi_session)) {
227                         session_num--;
228                         samba_dmapi_session = DM_NO_SESSION;
229                 } else {
230                         DEBUG(0,("Couldn't destroy DMAPI session: %s\n",
231                                  strerror(errno)));
232                 }
233                 unbecome_root();
234         }
235         return samba_dmapi_session == DM_NO_SESSION;
236 }
237
238
239 /* 
240    This is default implementation of dmapi_file_flags() that is 
241    called from VFS is_offline() call to know whether file is offline.
242    For GPFS-specific version see modules/vfs_tsmsm.c. It might be
243    that approach on quering existence of a specific attribute that
244    is used in vfs_tsmsm.c will work with other DMAPI-based HSM 
245    implementations as well.
246 */
247 uint32 dmapi_file_flags(const char * const path)
248 {
249         int             err;
250         dm_eventset_t   events = {0};
251         uint            nevents;
252
253         dm_sessid_t     dmapi_session;
254         const void      *dmapi_session_ptr;
255         void            *dm_handle = NULL;
256         size_t          dm_handle_len = 0;
257
258         uint32          flags = 0;
259
260         dmapi_session_ptr = dmapi_get_current_session();
261         if (dmapi_session_ptr == NULL) {
262                 return 0;
263         }
264
265         dmapi_session = *(dm_sessid_t *)dmapi_session_ptr;
266         if (dmapi_session == DM_NO_SESSION) {
267                 return 0;
268         }
269
270         /* AIX has DMAPI but no POSIX capablities support. In this case,
271          * we need to be root to do DMAPI manipulations.
272          */
273 #ifndef HAVE_POSIX_CAPABILITIES
274         become_root();
275 #endif
276
277         err = dm_path_to_handle(CONST_DISCARD(char *, path),
278                 &dm_handle, &dm_handle_len);
279         if (err < 0) {
280                 DEBUG(DMAPI_TRACE, ("dm_path_to_handle(%s): %s\n",
281                             path, strerror(errno)));
282
283                 if (errno != EPERM) {
284                         goto done;
285                 }
286
287                 /* Linux capabilities are broken in that changing our
288                  * user ID will clobber out effective capabilities irrespective
289                  * of whether we have set PR_SET_KEEPCAPS. Fortunately, the
290                  * capabilities are not removed from our permitted set, so we
291                  * can re-acquire them if necessary.
292                  */
293
294                 set_effective_capability(DMAPI_ACCESS_CAPABILITY);
295
296                 err = dm_path_to_handle(CONST_DISCARD(char *, path),
297                         &dm_handle, &dm_handle_len);
298                 if (err < 0) {
299                         DEBUG(DMAPI_TRACE,
300                             ("retrying dm_path_to_handle(%s): %s\n",
301                             path, strerror(errno)));
302                         goto done;
303                 }
304         }
305
306         err = dm_get_eventlist(dmapi_session, dm_handle, dm_handle_len,
307                 DM_NO_TOKEN, DM_EVENT_MAX, &events, &nevents);
308         if (err < 0) {
309                 DEBUG(DMAPI_TRACE, ("dm_get_eventlist(%s): %s\n",
310                             path, strerror(errno)));
311                 dm_handle_free(dm_handle, dm_handle_len);
312                 goto done;
313         }
314
315         /* We figure that the only reason a DMAPI application would be
316          * interested in trapping read events is that part of the file is
317          * offline.
318          */
319         DEBUG(DMAPI_TRACE, ("DMAPI event list for %s\n", path));
320         if (DMEV_ISSET(DM_EVENT_READ, events)) {
321                 flags = FILE_ATTRIBUTE_OFFLINE;
322         }
323
324         dm_handle_free(dm_handle, dm_handle_len);
325
326         if (flags & FILE_ATTRIBUTE_OFFLINE) {
327                 DEBUG(DMAPI_TRACE, ("%s is OFFLINE\n", path));
328         }
329
330 done:
331
332 #ifndef HAVE_POSIX_CAPABILITIES
333         unbecome_root();
334 #endif
335
336         return flags;
337 }
338
339
340 #endif /* USE_DMAPI */